#include <bits/stdc++.h>
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define ll long long
#define ldb long double
#define db double
#define fin(x) freopen(x,"r",stdin)
#define fout(x) freopen(x,"w",stdout)
#define fo(i,l,r) for(int i=(l);i<=(r);i++)
#define foi(i,l,r) for(int i=(l);i>=(r);i--)
#define el cout<<'\n';
#define cel cerr<<'\n';
#define all(x) x.begin(),x.end()
#define ii pair<int,int>
#define iii pair<int,ii>
#define gb(x,i) (((x)>>(i))&1)
#define mask(i) (1LL<<(i))
using namespace std;
const int N=3e5+5;
const int bl=60;
const ll base=1e9+7;
const ll inf=1e9;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ll Rand(ll l,ll r)
{
return l+(1ll*rng()*rng()%(r-l+1)+(r-l+1))%(r-l+1);
}
template<class X,class Y>bool maximize(X &a,Y b)
{
if(a<b) return a=b,true;
return false;
}
template<class X,class Y>bool minimize(X &a, Y b)
{
if(a>b) return a=b,true;
return false;
}
void add(auto &a,auto b)
{
a+=b;
if(a>=base) a-=base;
if(a<0) a+=base;
}
int n,ans[N],q;
vector<ii>cc;
vector<ii>num;
vector<int>more;
struct query
{
char ch;
int x,y;
};
vector<query>eg;
struct node
{
node *ch[2];
int cnt;
node()
{
ch[0]=ch[1]=NULL;
cnt=0;
}
};
struct trie
{
node *root=new node();
void add(int x,int d)
{
x+=1e9;
node *tmp=root;
foi(i,30,0)
{
int c=gb(x,i);
if(tmp->ch[c]==NULL) tmp->ch[c]=new node();
tmp=tmp->ch[c],tmp->cnt+=d;
}
}
int get(int x)
{
x+=1e9;
int kq=0;
node *tmp=root;
foi(i,30,0)
{
int c=gb(x,i);
if(c==1)
{
if(tmp->ch[0]!=NULL) kq+=tmp->ch[0]->cnt;
if(tmp->ch[1]==NULL) return kq;
tmp=tmp->ch[1];
}
else
{
if(tmp->ch[0]==NULL) return kq;
tmp=tmp->ch[0];
}
}
return kq;
}
void reset()
{
root=new node();
}
}tl,tr;
void add(int l,int r,int c)
{
if(l>r) swap(l,r);
tl.add(l,c),tr.add(r,c);
}
void calc()
{
int nx=1;
more.clear(),num.clear();
tl.reset(),tr.reset();
for(auto [x,y]:cc) num.pb({nx,nx+x}),nx+=x,more.pb(x);
for(auto [l,r]:num) add(l,r,1);
int now=0,change=0,pos=0;
fo(i,0,q-1)
{
auto [ch,x,y]=eg[i];
if(ch=='C')
{
int d=x-more[pos];
int l=num[pos].f,r=num[pos].s;
add(l,r,-1);
more[pos]=x,num[pos].f-=d;
l=num[pos].f,r=num[pos].s;
add(l,r,1),change+=d;
}
else if(ch=='Q') ans[i]+=now+tl.get(-change)-tr.get(-change);
else if(ch=='F')
{
if(pos==n-1) continue;
auto &[l,r]=num[pos];
add(l,r,-1);
l+=change,r+=change;
now+=((l<0 and r>0) or (l>0 and r<0)),pos++;
}
else if(ch=='B')
{
if(!pos) continue;
pos--;
auto &[l,r]=num[pos];
if((l<0 and r>0) or (l>0 and r<0)) now--;
l-=change,r-=change,add(l,r,1);
}
}
}
int main()
{
#define task "o"
if(fopen(task".inp","r"))
{
fin(task".inp");
//fout(task".out");
}
srand(time(NULL));
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin>>n,cc.resize(n);
for(auto &[x,y]:cc) cin>>x>>y;
cin>>q;
fo(i,1,q)
{
char ch;
cin>>ch;
int x=-1,y=-1;
if(ch=='C') cin>>x>>y;
eg.pb({ch,x,y});
}
calc();
for(auto &[x,y]:cc) swap(x,y);
for(auto &[ch,x,y]:eg) swap(x,y);
calc();
fo(i,0,q-1) if(eg[i].ch=='Q') cout<<ans[i],el
}
Compilation message
ruka.cpp:40:10: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
40 | void add(auto &a,auto b)
| ^~~~
ruka.cpp:40:18: warning: use of 'auto' in parameter declaration only available with '-fconcepts-ts'
40 | void add(auto &a,auto b)
| ^~~~
ruka.cpp: In function 'void calc()':
ruka.cpp:117:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
117 | for(auto [x,y]:cc) num.pb({nx,nx+x}),nx+=x,more.pb(x);
| ^
ruka.cpp:118:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
118 | for(auto [l,r]:num) add(l,r,1);
| ^
ruka.cpp:122:14: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
122 | auto [ch,x,y]=eg[i];
| ^
ruka.cpp:136:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
136 | auto &[l,r]=num[pos];
| ^
ruka.cpp:145:19: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
145 | auto &[l,r]=num[pos];
| ^
ruka.cpp: In function 'int main()':
ruka.cpp:162:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
162 | for(auto &[x,y]:cc) cin>>x>>y;
| ^
ruka.cpp:173:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
173 | for(auto &[x,y]:cc) swap(x,y);
| ^
ruka.cpp:174:15: warning: structured bindings only available with '-std=c++17' or '-std=gnu++17'
174 | for(auto &[ch,x,y]:eg) swap(x,y);
| ^
ruka.cpp:9:23: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
9 | #define fin(x) freopen(x,"r",stdin)
| ~~~~~~~^~~~~~~~~~~~~
ruka.cpp:156:9: note: in expansion of macro 'fin'
156 | fin(task".inp");
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
53 ms |
6288 KB |
Output is correct |
6 |
Correct |
52 ms |
8392 KB |
Output is correct |
7 |
Correct |
55 ms |
3348 KB |
Output is correct |
8 |
Correct |
55 ms |
3280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
604 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
1 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
604 KB |
Output is correct |
5 |
Correct |
53 ms |
6288 KB |
Output is correct |
6 |
Correct |
52 ms |
8392 KB |
Output is correct |
7 |
Correct |
55 ms |
3348 KB |
Output is correct |
8 |
Correct |
55 ms |
3280 KB |
Output is correct |
9 |
Correct |
148 ms |
21628 KB |
Output is correct |
10 |
Correct |
173 ms |
25420 KB |
Output is correct |
11 |
Correct |
132 ms |
16188 KB |
Output is correct |
12 |
Correct |
145 ms |
19144 KB |
Output is correct |