#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vl vector<ll>
using namespace std;
const int mxn=3e5+5;
int a[mxn]{0},b[mxn]{0},ans[mxn]{0},n;
vector<pii>qr[mxn];
multiset<int>ms,ms2;
vector<ll>bit[mxn];
vector<int>bval[mxn];
vector<int>val[mxn];
void build(){
for(int i=1;i<=n;i++){
if(val[i].empty())continue;
sort(val[i].begin(),val[i].end());
for(auto it : val[i]){
for(int j=i;j<=n;j+=j&-j){
if(bval[j].empty()||bval[j].back()!=it)bval[j].pb(it);
}
}
}
for(int i=1;i<=n;i++)bit[i].resize((int)bval[i].size()+1,0);
}
int ub(int x,int y){
return upper_bound(bval[x].begin(),bval[x].end(),y)-bval[x].begin();
}
void upd(int i,int j,int amt){
for(int x=i;x<=n;x+=x&-x)for(int y=ub(x,j);y<bit[x].size();y+=y&-y)bit[x][y]+=amt;
}
int query(int a,int b,int res=0){
for(int x=a;x>0;x-=x&-x)for(int y=ub(x,b);y>0;y-=y&-y)res+=bit[x][y];
return res;
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int q;cin>>n>>q;ms.insert(0);ms.insert(n+1);ms2.insert(0);ms2.insert(n+1);
for(int i=1;i<=n;i++){
char x;cin>>x;a[i]=x-'0';b[i]=a[i];
if(!a[i])ms.insert(i),ms2.insert(i);
}vector<pair<int,pii>>qq;
for(int i=1;i<=q;i++){
string s;cin>>s;ans[i]=-1;
if(s=="toggle"){
int x;cin>>x;qq.pb({1,{x,0}});
if(a[x]){
int l = 1+*prev(ms.lower_bound(x));
int r = -1+*ms.upper_bound(x);
val[l].pb(x);val[l].pb(r+1);val[x+1].pb(x);val[x+1].pb(r+1);
a[x]=0;ms.insert(x);
}
else {
int l = 1+*prev(ms.lower_bound(x));
int r = -1+*ms.upper_bound(x);
val[l].pb(x);val[l].pb(r+1);val[x+1].pb(x);val[x+1].pb(r+1);
a[x]=1;ms.erase(x);
}
}
else {
int l,r;cin>>l>>r;qq.pb({2,{l,r}});
}
}build();
for(int i=0;i<qq.size();i++){
auto it=qq[i];
if(it.f==1){int x=it.s.f;
if(b[x]){
int l = 1+*prev(ms2.lower_bound(x));
int r = -1+*ms2.upper_bound(x);
upd(l,x,i+1);upd(l,r+1,-i-1);upd(x+1,x,-i-1);upd(x+1,r+1,i+1);
b[x]=0;ms2.insert(x);
}
else {
int l = 1+*prev(ms2.lower_bound(x));
int r = -1+*ms2.upper_bound(x);
upd(l,x,-i-1);upd(l,r+1,i+1);upd(x+1,x,i+1);upd(x+1,r+1,-i-1);
b[x]=1;ms2.erase(x);
}
}
else {
ans[i+1] = query(it.s.f,it.s.s-1);
int ij = *ms2.lower_bound(it.s.f);
if(ij>it.s.s-1)ans[i+1]+=i+1;
}
}
for(int i=1;i<=q;i++)if(ans[i]!=-1)cout<<ans[i]<<'\n';
}
Compilation message
street_lamps.cpp: In function 'void upd(int, int, int)':
street_lamps.cpp:38:49: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
38 | for(int x=i;x<=n;x+=x&-x)for(int y=ub(x,j);y<bit[x].size();y+=y&-y)bit[x][y]+=amt;
| ~^~~~~~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:72:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
72 | for(int i=0;i<qq.size();i++){
| ~^~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31324 KB |
Output is correct |
2 |
Correct |
4 ms |
31320 KB |
Output is correct |
3 |
Incorrect |
4 ms |
31324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Incorrect |
164 ms |
42212 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
31580 KB |
Output is correct |
2 |
Incorrect |
6 ms |
31436 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
31324 KB |
Output is correct |
2 |
Correct |
7 ms |
31408 KB |
Output is correct |
3 |
Correct |
6 ms |
31576 KB |
Output is correct |
4 |
Correct |
6 ms |
31580 KB |
Output is correct |
5 |
Correct |
273 ms |
62380 KB |
Output is correct |
6 |
Correct |
760 ms |
98172 KB |
Output is correct |
7 |
Correct |
1288 ms |
125516 KB |
Output is correct |
8 |
Correct |
1900 ms |
152580 KB |
Output is correct |
9 |
Incorrect |
289 ms |
45056 KB |
Output isn't correct |
10 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
31324 KB |
Output is correct |
2 |
Correct |
4 ms |
31320 KB |
Output is correct |
3 |
Incorrect |
4 ms |
31324 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |