#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*2],kaf=(1<<19);
struct offt2fen{
vector<pair<int,int>>ady[maxn*2];
void upd(int l,int r,int w){
////cout<<"updy: "<<l<<" "<<r<<" "<<w<<endl;
l++;
r++;
while(l<maxn){
if(l<0){
return ;
}
int fr=r;
while(fr<maxn){
ady[l].push_back(make_pair(-fr,w));
fr+=(fr&(-fr));
}
l+=((-l)&l);
}
}
void pors(int l,int r,int w){
////cout<<"porsi: "<<l<<" "<<r<<" "<<w<<endl;
l++;
r++;
while(l>0){
int fr=r;
while(fr>0){
ady[l].push_back(make_pair(fr,w));
fr-=(fr&(-fr));
}
l-=((-l)&l);
}
}
void cal(int i){
for(auto x:ady[i]){
if(x.first<0){
x.first=-x.first;
fn[x.first]+=x.second;
}
else{
res[x.second]+=fn[x.first];
}
}
for(auto x:ady[i]){
if(x.first<0){
x.first=-x.first;
}
fn[x.first]=0;
}
}
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(ind+1,nx-1);
//calu(ps+1,ind-1);
//seg.upd(ind+kaf,now);
calu(ps+1,ind-1,-(maxn-now));
calu(ind+1,nx-1,-(maxn-now));
calu(ps+1,nx-1,maxn-now);
}
else{
// int x=calu(ps+1,nx-1);
// calu(ps+1,ind-1,-1,x);
// calu(ind+1,nx-1,-1,x);
// seg.upd(ind+kaf,maxn+10);
// st.insert(ind);
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){
////cout<<"Wtf: "<<l<<" "<<r<<" "<<seg.pors(1,0,kaf-1,l,r)<<endl;
//res[now]=max(0,now-seg.pors(1,0,kaf-1,l,r));
auto x=*st.lower_bound(l);
//cout<<"wtf: "<<l<<" "<<x<<" "<<r<<endl;
if(x>r){
res[now]-=maxn-now;
}
fen.pors(l,r,now);
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
////cout.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){
// st.insert(i);
// seg.upd(i+kaf,maxn+10);
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";
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
30300 KB |
Output is correct |
2 |
Correct |
8 ms |
30300 KB |
Output is correct |
3 |
Correct |
9 ms |
30556 KB |
Output is correct |
4 |
Correct |
10 ms |
31360 KB |
Output is correct |
5 |
Correct |
10 ms |
31324 KB |
Output is correct |
6 |
Correct |
11 ms |
31064 KB |
Output is correct |
7 |
Correct |
7 ms |
31324 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Runtime error |
635 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
55 ms |
55072 KB |
Output is correct |
2 |
Correct |
47 ms |
52300 KB |
Output is correct |
3 |
Correct |
34 ms |
43256 KB |
Output is correct |
4 |
Correct |
8 ms |
31580 KB |
Output is correct |
5 |
Runtime error |
681 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
37388 KB |
Output is correct |
2 |
Correct |
29 ms |
41208 KB |
Output is correct |
3 |
Correct |
39 ms |
45684 KB |
Output is correct |
4 |
Correct |
51 ms |
50948 KB |
Output is correct |
5 |
Runtime error |
646 ms |
524288 KB |
Execution killed with signal 9 |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
10 ms |
30300 KB |
Output is correct |
2 |
Correct |
8 ms |
30300 KB |
Output is correct |
3 |
Correct |
9 ms |
30556 KB |
Output is correct |
4 |
Correct |
10 ms |
31360 KB |
Output is correct |
5 |
Correct |
10 ms |
31324 KB |
Output is correct |
6 |
Correct |
11 ms |
31064 KB |
Output is correct |
7 |
Correct |
7 ms |
31324 KB |
Output is correct |
8 |
Runtime error |
635 ms |
524288 KB |
Execution killed with signal 9 |
9 |
Halted |
0 ms |
0 KB |
- |