#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
//#include<bits/extc++.h>
//__gnu_pbds
struct Q{
int l;
int r;
int t;
bool type;
int ans;
Q(int _l=0,int _r=0,int _t=0,bool _type=0,int _ans=0):l(_l),r(_r),t(_t),type(_type),ans(_ans){}
};
struct BIT{
int n;
vector<ll> arr;
void init(int _n){
n = _n;
arr.resize(n+1,0);
}
void add(int x,int v){
while(x<=n){
arr[x]+=v;
x+=(x&-x);
}
}
ll query(int x){
ll ans = 0;
while(x){
ans+=arr[x];
x-=(x&-x);
}
return ans;
}
};
vector<Q> qvec;
BIT Lsum;
bool ch(int l,int r,int llim,int rlim){
if(l>=llim) return 1;
if(r>=rlim) return 0;
return qvec[r].l<qvec[l].l;
}
void solve(int l,int r){
if(r-l<=1) return;
int M = (l+r)/2;
solve(l,M);
solve(M,r);
int lhead = l;
int rhead = M;
vector<Q> temparr;
vector<pair<int,ll> > aq;
for(int tt=0;tt<r-l;tt++){
if(ch(lhead,rhead,M,r)){
if(qvec[rhead].type){
qvec[rhead].ans+=Lsum.query(qvec[rhead].r);
}
temparr.push_back(qvec[rhead]);
rhead++;
}else{
if(!qvec[lhead].type){
Lsum.add(qvec[lhead].r,qvec[lhead].ans);
aq.emplace_back(qvec[lhead].r,qvec[lhead].ans);
}
temparr.push_back(qvec[lhead]);
lhead++;
}
}
for(auto i : aq){
Lsum.add(i.first,-i.second);
}
for(int tt=0;tt<r-l;tt++){
qvec[l+tt] = temparr[tt];
}
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n,q;cin>>n>>q;
BIT arr;
arr.init(n);
string s;cin>>s;
for(int i=0;i<n;i++){
if(s[i]=='1') arr.add(i+1,1);
}
for(int i=1;i<=q;i++){
string ts;cin>>ts;
if(ts[0]=='q'){
Q temp;
cin>>temp.l>>temp.r;
temp.r--;
temp.t=i;
temp.type=1;
temp.ans=0;
if(arr.query(temp.r)-arr.query(temp.l-1)==temp.r-temp.l+1) temp.ans=i;
qvec.push_back(temp);
}else{
int x;cin>>x;
int m = (arr.query(x)-arr.query(x-1))?(1):(-1);
int ql=0;int qr=x;int pl=x;int pr=n+1;
while(qr-ql>1){
int mid = (qr+ql)/2;
if(arr.query(x-1)-arr.query(mid-1)==x-mid) qr=mid;
else ql=mid;
}
while(pr-pl>1){
int mid = (pr+pl)/2;
if(arr.query(mid)-arr.query(x)==mid-x) pl=mid;
else pr=mid;
}
qvec.emplace_back(qr,x,i,0,i*m);
qvec.emplace_back(x+1,x,i,0,-1*i*m);
qvec.emplace_back(qr,pl+1,i,0,-1*i*m);
qvec.emplace_back(x+1,pl+1,i,0,i*m);
arr.add(x,-m);
}
}
Lsum.init(n+1);
int N = qvec.size();
solve(0,N);
sort(qvec.begin(),qvec.end(),[](const Q &a,const Q &b){return a.t<b.t;});
for(auto i : qvec){
if(i.type) cout<<i.ans<<"\n";
}
return 0;
}
/*
5 5
00011
query 3 6
toggle 5
query 4 5
toggle 5
query 5 6
0
3
3
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
500 ms |
55720 KB |
Output is correct |
2 |
Correct |
498 ms |
54888 KB |
Output is correct |
3 |
Correct |
586 ms |
57904 KB |
Output is correct |
4 |
Correct |
731 ms |
61396 KB |
Output is correct |
5 |
Correct |
640 ms |
58492 KB |
Output is correct |
6 |
Correct |
769 ms |
71868 KB |
Output is correct |
7 |
Correct |
231 ms |
34824 KB |
Output is correct |
8 |
Correct |
237 ms |
33808 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
600 KB |
Output is correct |
2 |
Correct |
2 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
1177 ms |
99424 KB |
Output is correct |
6 |
Correct |
911 ms |
67984 KB |
Output is correct |
7 |
Correct |
636 ms |
57656 KB |
Output is correct |
8 |
Correct |
238 ms |
35432 KB |
Output is correct |
9 |
Correct |
104 ms |
16140 KB |
Output is correct |
10 |
Correct |
123 ms |
27440 KB |
Output is correct |
11 |
Correct |
122 ms |
26300 KB |
Output is correct |
12 |
Correct |
256 ms |
34076 KB |
Output is correct |
13 |
Correct |
240 ms |
34280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
1 ms |
604 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
2 ms |
604 KB |
Output is correct |
5 |
Correct |
253 ms |
36596 KB |
Output is correct |
6 |
Correct |
570 ms |
55556 KB |
Output is correct |
7 |
Correct |
776 ms |
67192 KB |
Output is correct |
8 |
Correct |
1148 ms |
99720 KB |
Output is correct |
9 |
Correct |
573 ms |
64036 KB |
Output is correct |
10 |
Correct |
709 ms |
98676 KB |
Output is correct |
11 |
Correct |
620 ms |
63460 KB |
Output is correct |
12 |
Correct |
681 ms |
105992 KB |
Output is correct |
13 |
Correct |
590 ms |
62968 KB |
Output is correct |
14 |
Correct |
674 ms |
100772 KB |
Output is correct |
15 |
Correct |
231 ms |
34396 KB |
Output is correct |
16 |
Correct |
238 ms |
34992 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
500 ms |
55720 KB |
Output is correct |
9 |
Correct |
498 ms |
54888 KB |
Output is correct |
10 |
Correct |
586 ms |
57904 KB |
Output is correct |
11 |
Correct |
731 ms |
61396 KB |
Output is correct |
12 |
Correct |
640 ms |
58492 KB |
Output is correct |
13 |
Correct |
769 ms |
71868 KB |
Output is correct |
14 |
Correct |
231 ms |
34824 KB |
Output is correct |
15 |
Correct |
237 ms |
33808 KB |
Output is correct |
16 |
Correct |
3 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
604 KB |
Output is correct |
18 |
Correct |
2 ms |
604 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
1177 ms |
99424 KB |
Output is correct |
21 |
Correct |
911 ms |
67984 KB |
Output is correct |
22 |
Correct |
636 ms |
57656 KB |
Output is correct |
23 |
Correct |
238 ms |
35432 KB |
Output is correct |
24 |
Correct |
104 ms |
16140 KB |
Output is correct |
25 |
Correct |
123 ms |
27440 KB |
Output is correct |
26 |
Correct |
122 ms |
26300 KB |
Output is correct |
27 |
Correct |
256 ms |
34076 KB |
Output is correct |
28 |
Correct |
240 ms |
34280 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
1 ms |
604 KB |
Output is correct |
31 |
Correct |
2 ms |
604 KB |
Output is correct |
32 |
Correct |
2 ms |
604 KB |
Output is correct |
33 |
Correct |
253 ms |
36596 KB |
Output is correct |
34 |
Correct |
570 ms |
55556 KB |
Output is correct |
35 |
Correct |
776 ms |
67192 KB |
Output is correct |
36 |
Correct |
1148 ms |
99720 KB |
Output is correct |
37 |
Correct |
573 ms |
64036 KB |
Output is correct |
38 |
Correct |
709 ms |
98676 KB |
Output is correct |
39 |
Correct |
620 ms |
63460 KB |
Output is correct |
40 |
Correct |
681 ms |
105992 KB |
Output is correct |
41 |
Correct |
590 ms |
62968 KB |
Output is correct |
42 |
Correct |
674 ms |
100772 KB |
Output is correct |
43 |
Correct |
231 ms |
34396 KB |
Output is correct |
44 |
Correct |
238 ms |
34992 KB |
Output is correct |
45 |
Correct |
243 ms |
32284 KB |
Output is correct |
46 |
Correct |
288 ms |
32472 KB |
Output is correct |
47 |
Correct |
428 ms |
35572 KB |
Output is correct |
48 |
Correct |
713 ms |
64208 KB |
Output is correct |
49 |
Correct |
146 ms |
26176 KB |
Output is correct |
50 |
Correct |
137 ms |
27820 KB |
Output is correct |
51 |
Correct |
139 ms |
26104 KB |
Output is correct |
52 |
Correct |
146 ms |
28004 KB |
Output is correct |
53 |
Correct |
145 ms |
28080 KB |
Output is correct |
54 |
Correct |
144 ms |
26616 KB |
Output is correct |