#pragma GCC optimize("O3,unroll-loops")
#pragma GCC target("avx2")
#include<bits/stdc++.h>
using namespace std;
const int maxn=500000+10;
int n,q,all[maxn],res[maxn],now,qu[maxn];
set<int>st;
int fn[maxn],kaf=(1<<19);
struct offt2fen{
vector<pair<int,int>>ady[maxn];
void upd(int l,int r,int w){
l++;
r++;
while(l<maxn){
if(l<0){
return ;
}
ady[l].push_back(make_pair(-r,w));
l+=((-l)&l);
}
}
void pors(int l,int r,int w){
l++;
r++;
while(l>0){
ady[l].push_back(make_pair(r,w));
l-=((-l)&l);
}
}
void cal(int i){
for(auto x:ady[i]){
if(x.first<0){
x.first=-x.first;
while(x.first<maxn){
fn[x.first]+=x.second;
x.first+=((-x.first)&x.first);
}
}
else{
while(x.first>0){
res[x.second]+=fn[x.first];
x.first-=((-x.first)&x.first);
}
}
}
for(auto x:ady[i]){
if(x.first<0){
x.first=-x.first;
while(x.first<maxn){
fn[x.first]=0;
x.first+=((-x.first)&x.first);
}
}
}
}
void build(){
for(int i=0;i<maxn;i++){
cal(i);
}
}
}fen;
void calu(int l,int r,int w){
if(l>r){
return ;
}
fen.upd(l,l,w);
fen.upd(l,r+1,-w);
fen.upd(r+1,l,-w);
fen.upd(r+1,r+1,w);
}
void upd(int ind){
st.erase(ind);
if(ind>=(*st.rbegin())||(ind<=(*st.begin()))){
return ;
}
auto y=st.lower_bound(ind);
int nx=*y;
y--;
int ps=*y;
if(all[ind]){
calu(ps+1,ind-1,-(maxn-now));
calu(ind+1,nx-1,-(maxn-now));
calu(ps+1,nx-1,maxn-now);
}
else{
calu(ps+1,nx-1,-(maxn-now));
calu(ps+1,ind-1,(maxn-now));
calu(ind+1,nx-1,(maxn-now));
st.insert(ind);
}
all[ind]^=1;
}
void pors(int l,int r){
auto x=*st.lower_bound(l);
if(x>r){
res[now]-=maxn-now;
}
fen.pors(l,r,now);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cin>>n>>q;
st.insert(0);
st.insert(n+1);
calu(1,n,maxn);
for(int i=1;i<=n;i++){
char c;
cin>>c;
all[i]=c-'0';
all[i]^=1;
if(all[i]==1){
all[i]=0;
upd(i);
}
}
for(int i=1;i<=q;i++){
now=i;
string s;
int l,r;
cin>>s>>l;
if(s[0]=='q'){
cin>>r;
r--;
pors(l,r);
qu[now]=1;
}
else{
upd(l);
}
}
fen.build();
for(int i=1;i<=q;i++){
if(qu[i]==1){
cout<<res[i]<<"\n";
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16988 KB |
Output is correct |
2 |
Correct |
7 ms |
16988 KB |
Output is correct |
3 |
Correct |
5 ms |
16988 KB |
Output is correct |
4 |
Correct |
6 ms |
16988 KB |
Output is correct |
5 |
Correct |
5 ms |
16988 KB |
Output is correct |
6 |
Correct |
6 ms |
17240 KB |
Output is correct |
7 |
Correct |
5 ms |
19096 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1085 ms |
197380 KB |
Output is correct |
2 |
Correct |
1000 ms |
201692 KB |
Output is correct |
3 |
Correct |
868 ms |
191976 KB |
Output is correct |
4 |
Correct |
1687 ms |
301276 KB |
Output is correct |
5 |
Correct |
1551 ms |
276068 KB |
Output is correct |
6 |
Correct |
1787 ms |
353748 KB |
Output is correct |
7 |
Correct |
1701 ms |
309336 KB |
Output is correct |
8 |
Correct |
255 ms |
53040 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
19032 KB |
Output is correct |
2 |
Correct |
15 ms |
18780 KB |
Output is correct |
3 |
Correct |
11 ms |
18524 KB |
Output is correct |
4 |
Correct |
5 ms |
19036 KB |
Output is correct |
5 |
Correct |
2614 ms |
483856 KB |
Output is correct |
6 |
Correct |
2209 ms |
428132 KB |
Output is correct |
7 |
Correct |
1506 ms |
272576 KB |
Output is correct |
8 |
Correct |
271 ms |
49852 KB |
Output is correct |
9 |
Correct |
59 ms |
26308 KB |
Output is correct |
10 |
Correct |
61 ms |
26772 KB |
Output is correct |
11 |
Correct |
63 ms |
26928 KB |
Output is correct |
12 |
Correct |
1668 ms |
299204 KB |
Output is correct |
13 |
Correct |
289 ms |
49916 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
8 ms |
17756 KB |
Output is correct |
2 |
Correct |
11 ms |
18012 KB |
Output is correct |
3 |
Correct |
12 ms |
18776 KB |
Output is correct |
4 |
Correct |
13 ms |
18780 KB |
Output is correct |
5 |
Correct |
1138 ms |
205276 KB |
Output is correct |
6 |
Correct |
1453 ms |
260564 KB |
Output is correct |
7 |
Correct |
1744 ms |
341152 KB |
Output is correct |
8 |
Correct |
2150 ms |
416328 KB |
Output is correct |
9 |
Correct |
1326 ms |
258016 KB |
Output is correct |
10 |
Correct |
1925 ms |
337236 KB |
Output is correct |
11 |
Correct |
1364 ms |
262984 KB |
Output is correct |
12 |
Correct |
1949 ms |
341920 KB |
Output is correct |
13 |
Correct |
1321 ms |
268768 KB |
Output is correct |
14 |
Correct |
1944 ms |
335044 KB |
Output is correct |
15 |
Correct |
1634 ms |
299148 KB |
Output is correct |
16 |
Correct |
251 ms |
49976 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
16988 KB |
Output is correct |
2 |
Correct |
7 ms |
16988 KB |
Output is correct |
3 |
Correct |
5 ms |
16988 KB |
Output is correct |
4 |
Correct |
6 ms |
16988 KB |
Output is correct |
5 |
Correct |
5 ms |
16988 KB |
Output is correct |
6 |
Correct |
6 ms |
17240 KB |
Output is correct |
7 |
Correct |
5 ms |
19096 KB |
Output is correct |
8 |
Correct |
1085 ms |
197380 KB |
Output is correct |
9 |
Correct |
1000 ms |
201692 KB |
Output is correct |
10 |
Correct |
868 ms |
191976 KB |
Output is correct |
11 |
Correct |
1687 ms |
301276 KB |
Output is correct |
12 |
Correct |
1551 ms |
276068 KB |
Output is correct |
13 |
Correct |
1787 ms |
353748 KB |
Output is correct |
14 |
Correct |
1701 ms |
309336 KB |
Output is correct |
15 |
Correct |
255 ms |
53040 KB |
Output is correct |
16 |
Correct |
17 ms |
19032 KB |
Output is correct |
17 |
Correct |
15 ms |
18780 KB |
Output is correct |
18 |
Correct |
11 ms |
18524 KB |
Output is correct |
19 |
Correct |
5 ms |
19036 KB |
Output is correct |
20 |
Correct |
2614 ms |
483856 KB |
Output is correct |
21 |
Correct |
2209 ms |
428132 KB |
Output is correct |
22 |
Correct |
1506 ms |
272576 KB |
Output is correct |
23 |
Correct |
271 ms |
49852 KB |
Output is correct |
24 |
Correct |
59 ms |
26308 KB |
Output is correct |
25 |
Correct |
61 ms |
26772 KB |
Output is correct |
26 |
Correct |
63 ms |
26928 KB |
Output is correct |
27 |
Correct |
1668 ms |
299204 KB |
Output is correct |
28 |
Correct |
289 ms |
49916 KB |
Output is correct |
29 |
Correct |
8 ms |
17756 KB |
Output is correct |
30 |
Correct |
11 ms |
18012 KB |
Output is correct |
31 |
Correct |
12 ms |
18776 KB |
Output is correct |
32 |
Correct |
13 ms |
18780 KB |
Output is correct |
33 |
Correct |
1138 ms |
205276 KB |
Output is correct |
34 |
Correct |
1453 ms |
260564 KB |
Output is correct |
35 |
Correct |
1744 ms |
341152 KB |
Output is correct |
36 |
Correct |
2150 ms |
416328 KB |
Output is correct |
37 |
Correct |
1326 ms |
258016 KB |
Output is correct |
38 |
Correct |
1925 ms |
337236 KB |
Output is correct |
39 |
Correct |
1364 ms |
262984 KB |
Output is correct |
40 |
Correct |
1949 ms |
341920 KB |
Output is correct |
41 |
Correct |
1321 ms |
268768 KB |
Output is correct |
42 |
Correct |
1944 ms |
335044 KB |
Output is correct |
43 |
Correct |
1634 ms |
299148 KB |
Output is correct |
44 |
Correct |
251 ms |
49976 KB |
Output is correct |
45 |
Correct |
896 ms |
136496 KB |
Output is correct |
46 |
Correct |
698 ms |
148144 KB |
Output is correct |
47 |
Correct |
809 ms |
167872 KB |
Output is correct |
48 |
Correct |
1655 ms |
290876 KB |
Output is correct |
49 |
Correct |
78 ms |
27608 KB |
Output is correct |
50 |
Correct |
71 ms |
27196 KB |
Output is correct |
51 |
Correct |
72 ms |
28084 KB |
Output is correct |
52 |
Correct |
80 ms |
30524 KB |
Output is correct |
53 |
Correct |
76 ms |
30780 KB |
Output is correct |
54 |
Correct |
76 ms |
30916 KB |
Output is correct |