Educational Codeforces Round 23
A. Treasure Hunt
先判一下是否都是倍数,然后判一下倍数奇偶性
view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define pb push_back
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x) cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
void solve(){
____();
int x1, y1, x2, y2, x, y;
cin >> x1 >> y1 >> x2 >> y2 >> x >> y;
int dx = abs(x2 - x1), dy = abs(y2 - y1);
if(dx%x!=0 or dy%y!=0){
cout << "NO" << endl;
return;
}
if(((dx/x)&1) == ((dy/y)&1)) cout << "YES" << endl;
else cout << "NO" << endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
solve();
return 0;
}
B. Makes And The Product
从小到大排序之后先选前三个,如果后面有和第三个一样的话全部都选进来
然后分情况讨论即可
view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define pb push_back
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x) cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
void solve(){
int n; sci(n);
vi A(n);
for(int &x : A) sci(x);
sort(all(A));
vi vec;
for(int i = 0; i < 3; i++) vec.pb(A[i]);
for(int i = 3; i < (int) A.size(); i++) if(A[i]==vec.back()) vec.pb(A[i]);
set<int> S; for(int x : vec) S.insert(x);
if(S.size()==1){
cout << 1ll * vec.size() * (vec.size() - 1) * (vec.size() - 2) / 6 << endl;
return;
}else if(S.size()==2){
if(vec[0]==vec[1]) cout << vec.size() - 2 << endl;
else cout << 1ll * (vec.size() - 1) * (vec.size() - 2) / 2 << endl;
}else cout << vec.size() - 2 << endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
solve();
return 0;
}
C. Really Big Numbers
考虑计算差值小于\(s\)的
显然小于\(x\le s\)的\(x\)差值必然小于\(s\)
而且满足条件的\(x\)不会超过\(s\)很多,枚举一下\(s\)附近的\(x\)即可
view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define pb push_back
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x) cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
int dig(LL x){
int s = 0;
while(x) s += x % 10, x /= 10;
return s;
}
void solve(){
LL n, s;
cin >> n >> s;
LL top = min(s+20000, n);
LL num = min(s - 1, n);
for(LL i = s; i <= top; i++) if(i - dig(i) < s) num++;
cout << n - num << endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
solve();
return 0;
}
D. Imbalanced Array
考虑每个值对答案的贡献,枚举每个值作为最大值或者最小值在哪些区间出现
我们需要找到对于一个点左边第一个小于等于,右边第一个小于的位置
左边第一个大于等于,右边第一个大于的位置
这个用单调栈维护一下即可
view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define pb push_back
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x) cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 1e6+7;
int A[MAXN], ls[MAXN], rs[MAXN];
void solve(){
int n; sci(n);
for(int i = 1; i <= n; i++) sci(A[i]);
LL ret = 0;
stack<int> stk;
for(int i = 1; i <= n; i++){
ls[i] = i;
while(!stk.empty() and A[stk.top()]<=A[i]) ls[i] = ls[stk.top()], stk.pop();
stk.push(i);
}
while(!stk.empty()) stk.pop();
for(int i = n; i >= 1; i--){
rs[i] = i;
while(!stk.empty() and A[stk.top()]<A[i]) rs[i] = rs[stk.top()], stk.pop();
stk.push(i);
}
while(!stk.empty()) stk.pop();
for(int i = 1; i <= n; i++) ret += 1ll * (rs[i] - i + 1) * (i - ls[i] + 1) * A[i];
for(int i = 1; i <= n; i++){
ls[i] = i;
while(!stk.empty() and A[stk.top()]>=A[i]) ls[i] = ls[stk.top()], stk.pop();
stk.push(i);
}
while(!stk.empty()) stk.pop();
for(int i = n; i >= 1; i--){
rs[i] = i;
while(!stk.empty() and A[stk.top()]>A[i]) rs[i] = rs[stk.top()], stk.pop();
stk.push(i);
}
while(!stk.empty()) stk.pop();
for(int i = 1; i <= n; i++) ret -= 1ll * (rs[i] - i + 1) * (i - ls[i] + 1) * A[i];
cout << ret << endl;
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
solve();
return 0;
}
E. Choosing The Commander
简单的字典树维护
view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define pb push_back
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x) cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 1e5+7;
int q, tot, ch[MAXN<<4][2], root, sum[MAXN<<4];
void ins(int p, int &rt, int dep){
if(!rt) rt = ++tot;
sum[rt] += 1;
if(dep==-1) return;
ins(p,ch[rt][p>>dep&1],dep-1);
}
void del(int p, int rt, int dep){
sum[rt] -= 1;
if(dep==-1) return;
del(p,ch[rt][p>>dep&1],dep-1);
}
int query(int p, int l, int rt, int dep){
if(dep==-1) return 0;
if(l>>dep&1) return query(p,l,ch[rt][(p>>dep&1)^1],dep-1) + sum[ch[rt][p>>dep&1]];
else return query(p,l,ch[rt][(p>>dep&1)],dep-1);
}
void solve(){
sci(q);
while(q--){
int tp; sci(tp);
if(tp==1){
int p; sci(p);
ins(p,root,30);
}else if(tp==2){
int p; sci(p);
del(p,root,30);
}else{
int p, l;
sci(p); sci(l);
printf("%d\n",query(p,l,root,30));
}
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
solve();
return 0;
}
F. MEX Queries
\(l,r\)范围很大,离散化之后用线段树维护就好了
主要维护区间置\(1\),区间置\(0\)和区间翻转
view code
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast,no-stack-protector")
#include<bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long int
#define vi vector<int>
#define vl vector<LL>
#define pb push_back
#define all(V) V.begin(),V.end()
#define sci(x) scanf("%d",&x)
#define scl(x) scanf("%I64d",&x)
#define pii pair<int,int>
#define pll pair<LL,LL>
#define cmax(a,b) ((a) = (a) > (b) ? (a) : (b))
#define cmin(a,b) ((a) = (a) < (b) ? (a) : (b))
#define debug(x) cerr << #x << " = " << x << endl
function<void(void)> ____ = [](){ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);};
const int MAXN = 4e5+7;
struct SgmentTree{
int l[MAXN<<2], r[MAXN<<2], lm[2][MAXN<<2];
int tag[MAXN<<2]; // 1 reverse, 2 set zero, 3 set one
#define ls(rt) rt << 1
#define rs(rt) rt << 1 | 1
void pushup(int rt){
lm[0][rt] = min(lm[0][ls(rt)],lm[0][rs(rt)]);
lm[1][rt] = min(lm[1][ls(rt)],lm[1][rs(rt)]);
}
void down(int rt, int tg){
if(tg==1){
tag[rt] = 1;
lm[0][rt] = MAXN;
lm[1][rt] = l[rt];
}else if(tg==2){
tag[rt] = 2;
lm[0][rt] = l[rt];
lm[1][rt] = MAXN;
}else{
if(!tag[rt]){
tag[rt] = 3;
swap(lm[0][rt],lm[1][rt]);
}else if(tag[rt]==3){
tag[rt] = 0;
swap(lm[0][rt],lm[1][rt]);
}else if(tag[rt]==1){
tag[rt] = 2;
lm[0][rt] = l[rt];
lm[1][rt] = MAXN;
}else{
tag[rt] = 1;
lm[0][rt] = MAXN;
lm[1][rt] = l[rt];
}
}
}
void pushdown(int rt){
if(!tag[rt]) return;
down(ls(rt),tag[rt]);
down(rs(rt),tag[rt]);
tag[rt] = 0;
}
void build(int L, int R, int rt = 1){
l[rt] = L; r[rt] = R;
lm[0][rt] = l[rt]; lm[1][rt] = MAXN;
if(l[rt] + 1 == r[rt]) return;
int mid = (l[rt] + r[rt]) >> 1;
build(L,mid,ls(rt)); build(mid,R,rs(rt));
pushup(rt);
}
void set(int L, int R, int x, int rt = 1){
if(L>=r[rt] or l[rt]>=R) return;
if(L<=l[rt] and r[rt]<=R){
down(rt,x);
return;
}
pushdown(rt);
set(L,R,x,ls(rt)); set(L,R,x,rs(rt));
pushup(rt);
}
int query(){ return lm[0][1]; }
}ST;
vector<pair<int,pll> > Q;
int n;
vl vec;
void solve(){
sci(n);
Q.resize(n);
for(auto &p : Q) sci(p.first), scl(p.second.first), scl(p.second.second);
for(auto p : Q) vec.pb(p.second.first), vec.pb(p.second.second);
sort(all(vec));
for(int i = 0, l = vec.size(); i < l; i++){
vec.pb(vec[i]+1);
if(vec[i]-1) vec.pb(vec[i]-1);
}
vec.pb(1);
sort(all(vec));
vec.erase(unique(all(vec)),vec.end());
ST.build(1,MAXN);
for(auto &q : Q){
int l = lower_bound(all(vec),q.second.first) - vec.begin() + 1;
int r = lower_bound(all(vec),q.second.second) - vec.begin() + 1;
ST.set(l,r+1,q.first);
printf("%I64d\n",vec[ST.query()-1]);
}
}
int main(){
#ifndef ONLINE_JUDGE
freopen("Local.in","r",stdin);
freopen("ans.out","w",stdout);
#endif
solve();
return 0;
}