/*input
5 7
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 1 6
*/
#include<bits/stdc++.h>
using namespace std;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
typedef tree<long long,null_type,less_equal<long long>,rb_tree_tag,tree_order_statistics_node_update> indexed_set;
#pragma GCC optimize("Ofast")
//#pragma GCC target("avx2")
//order_of_key #of elements less than x
// find_by_order kth element
using ll=long long;
using ld=long double;
using pii=pair<int,int>;
#define f first
#define s second
#define pb push_back
#define REP(i,n) for(int i=0;i<n;i++)
#define REP1(i,n) for(int i=1;i<=n;i++)
#define FILL(n,x) memset(n,x,sizeof(n))
#define ALL(_a) _a.begin(),_a.end()
#define sz(x) (int)x.size()
#define SORT_UNIQUE(c) (sort(c.begin(),c.end()),c.resize(distance(c.begin(),unique(c.begin(),c.end()))))
const ll maxn=3e5+5;
const ll maxlg=__lg(maxn)+2;
const ll INF64=4e18;
const int INF=0x3f3f3f3f;
int MOD=998244353;
const ld PI=acos(-1);
const ld eps=1e-9;
#define lowb(x) x&(-x)
#define MNTO(x,y) x=min(x,(__typeof__(x))y)
#define MXTO(x,y) x=max(x,(__typeof__(x))y)
template<typename T1,typename T2>
ostream& operator<<(ostream& out,pair<T1,T2> P){
out<<P.f<<' '<<P.s;
return out;
}
template<typename T>
ostream& operator<<(ostream& out,vector<T> V){
REP(i,sz(V)) out<<V[i]<<((i!=sz(V)-1)?" ":"\n");
return out;
}
ll mult(ll a,ll b){
return a*b%MOD;
}
ll mult(ll a,ll b,ll mod){
ll res=0;
while(b){
if(b&1) res=(res+a)%mod;
a=(a+a)%mod;
b>>=1;
}
return res;
}
ll mypow(ll a,ll b,ll mod){
if(b<=0) return 1;
a%=mod;
ll res=1LL;
while(b){
if(b&1) res=(res*a)%mod;
a=(a*a)%mod;
b>>=1;
}
return res;
}
ll mypow(ll a,ll b){
a%=MOD;
if(a==0) return 0;
if(b<=0) return 1;
ll res=1LL;
while(b){
if(b&1) res=(res*a)%MOD;
a=(a*a)%MOD;
b>>=1;
}
return res;
}
set<pii> s;
int arr[maxn];
vector<pair<pii,pii>> qq;
void add(pii x,int t){
qq.pb({{t,-t},x});
s.insert(x);
}
void del(pii x,int t){
qq.pb({{t,t},x});
s.erase(s.find(x));
}
int bit[maxn];
void upd(int p,int x){
++p;
while(p<maxn){
bit[p]+=x;
p+=lowb(p);
}
}
int query(int p){
++p;
int ans=0;
while(p){
ans+=bit[p];
p-=lowb(p);
}
return ans;
}
int ans[maxn];
void dnc(int l,int r,vector<pair<pii,pii>> &qq,vector<pair<int,pii>> &qr){
if(l==r){
return;
}
vector<pair<pii,pii>> lq,rq;
vector<pair<int,pii>> lqr,rqr;
int mid=(l+r)/2;
for(auto x:qq){
if(x.f.f<=mid) lq.pb(x);
else rq.pb(x);
}
for(auto x:qr){
if(x.f<=mid) lqr.pb(x);
else rqr.pb(x);
}
dnc(l,mid,lq,lqr),dnc(mid+1,r,rq,rqr);
vector<pair<pii,int>> zl,zr;
for(auto x:lq) zl.pb({x.s,x.f.s});
for(auto x:rqr) zr.pb({x.s,x.f});
sort(ALL(zr)),sort(ALL(zl));
int p=0;
for(auto x:zr){
while(p<sz(zl) and zl[p].f.f<=x.f.f) upd(zl[p].f.s,zl[p].s),++p;
ans[x.s]+=query(maxn-2)-query(x.f.s-1);
}
REP(j,p) upd(zl[j].f.s,-zl[j].s);
}
bool hv[maxn];
int main(){
ios::sync_with_stdio(false),cin.tie(0);
int n,q;
cin>>n>>q;
REP(i,n){
char c;
cin>>c;
arr[i]=c-'0';
}
REP(i,n){
if(!arr[i]) continue;
int p=i;
while(i<n and arr[i]) ++i;
add({p,i-1},0);
}
vector<pair<int,pii>> qr;
REP1(i,q){
string S;
int a;
cin>>S>>a;
--a;
if(S[0]=='t'){
if(arr[a]){
auto it=prev(s.upper_bound({a,INF}));
auto x=(*it);
del(x,i);
if(x.f<a) add({x.f,a-1},i);
if(a<x.s) add({a+1,x.s},i);
}
else{
auto it=(s.upper_bound({a,INF}));
int l=a,r=a;
if(it!=s.end()){
if((*it).f==a+1){
r=(*it).s;
del(*it,i);
}
}
it=(s.upper_bound({a,INF}));
if(it!=s.begin()){
--it;
if((*it).s==a-1){
l=(*it).f;
del(*it,i);
}
}
add({l,r},i);
}
arr[a]=1-arr[a];
}
else{
hv[i]=1;
int b;
cin>>b;
b-=2;
qr.pb({i,{a,b}});
auto it=(s.upper_bound({a,INF}));
if(it!=s.begin()){
--it;
if((*it).s>=b) ans[i]=i;
}
}
}
dnc(0,q,qq,qr);
REP1(i,q) if(hv[i]) cout<<ans[i]<<'\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
276 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
667 ms |
29920 KB |
Output is correct |
2 |
Correct |
662 ms |
30348 KB |
Output is correct |
3 |
Correct |
647 ms |
30280 KB |
Output is correct |
4 |
Correct |
867 ms |
63404 KB |
Output is correct |
5 |
Correct |
938 ms |
59432 KB |
Output is correct |
6 |
Correct |
880 ms |
63180 KB |
Output is correct |
7 |
Correct |
434 ms |
23120 KB |
Output is correct |
8 |
Correct |
409 ms |
25496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
468 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
842 ms |
44428 KB |
Output is correct |
6 |
Correct |
959 ms |
58012 KB |
Output is correct |
7 |
Correct |
868 ms |
59116 KB |
Output is correct |
8 |
Correct |
382 ms |
25560 KB |
Output is correct |
9 |
Correct |
231 ms |
15384 KB |
Output is correct |
10 |
Correct |
251 ms |
17900 KB |
Output is correct |
11 |
Correct |
246 ms |
17916 KB |
Output is correct |
12 |
Correct |
375 ms |
23236 KB |
Output is correct |
13 |
Correct |
391 ms |
25512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
468 KB |
Output is correct |
2 |
Correct |
2 ms |
468 KB |
Output is correct |
3 |
Correct |
2 ms |
596 KB |
Output is correct |
4 |
Correct |
2 ms |
468 KB |
Output is correct |
5 |
Correct |
608 ms |
51172 KB |
Output is correct |
6 |
Correct |
702 ms |
58024 KB |
Output is correct |
7 |
Correct |
769 ms |
62632 KB |
Output is correct |
8 |
Correct |
826 ms |
67156 KB |
Output is correct |
9 |
Correct |
633 ms |
38516 KB |
Output is correct |
10 |
Correct |
668 ms |
36076 KB |
Output is correct |
11 |
Correct |
641 ms |
34660 KB |
Output is correct |
12 |
Correct |
674 ms |
36080 KB |
Output is correct |
13 |
Correct |
638 ms |
38824 KB |
Output is correct |
14 |
Correct |
670 ms |
36128 KB |
Output is correct |
15 |
Correct |
384 ms |
23212 KB |
Output is correct |
16 |
Correct |
372 ms |
25516 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
276 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
667 ms |
29920 KB |
Output is correct |
9 |
Correct |
662 ms |
30348 KB |
Output is correct |
10 |
Correct |
647 ms |
30280 KB |
Output is correct |
11 |
Correct |
867 ms |
63404 KB |
Output is correct |
12 |
Correct |
938 ms |
59432 KB |
Output is correct |
13 |
Correct |
880 ms |
63180 KB |
Output is correct |
14 |
Correct |
434 ms |
23120 KB |
Output is correct |
15 |
Correct |
409 ms |
25496 KB |
Output is correct |
16 |
Correct |
2 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
340 KB |
Output is correct |
20 |
Correct |
842 ms |
44428 KB |
Output is correct |
21 |
Correct |
959 ms |
58012 KB |
Output is correct |
22 |
Correct |
868 ms |
59116 KB |
Output is correct |
23 |
Correct |
382 ms |
25560 KB |
Output is correct |
24 |
Correct |
231 ms |
15384 KB |
Output is correct |
25 |
Correct |
251 ms |
17900 KB |
Output is correct |
26 |
Correct |
246 ms |
17916 KB |
Output is correct |
27 |
Correct |
375 ms |
23236 KB |
Output is correct |
28 |
Correct |
391 ms |
25512 KB |
Output is correct |
29 |
Correct |
2 ms |
468 KB |
Output is correct |
30 |
Correct |
2 ms |
468 KB |
Output is correct |
31 |
Correct |
2 ms |
596 KB |
Output is correct |
32 |
Correct |
2 ms |
468 KB |
Output is correct |
33 |
Correct |
608 ms |
51172 KB |
Output is correct |
34 |
Correct |
702 ms |
58024 KB |
Output is correct |
35 |
Correct |
769 ms |
62632 KB |
Output is correct |
36 |
Correct |
826 ms |
67156 KB |
Output is correct |
37 |
Correct |
633 ms |
38516 KB |
Output is correct |
38 |
Correct |
668 ms |
36076 KB |
Output is correct |
39 |
Correct |
641 ms |
34660 KB |
Output is correct |
40 |
Correct |
674 ms |
36080 KB |
Output is correct |
41 |
Correct |
638 ms |
38824 KB |
Output is correct |
42 |
Correct |
670 ms |
36128 KB |
Output is correct |
43 |
Correct |
384 ms |
23212 KB |
Output is correct |
44 |
Correct |
372 ms |
25516 KB |
Output is correct |
45 |
Correct |
425 ms |
17812 KB |
Output is correct |
46 |
Correct |
436 ms |
18072 KB |
Output is correct |
47 |
Correct |
473 ms |
28064 KB |
Output is correct |
48 |
Correct |
838 ms |
61696 KB |
Output is correct |
49 |
Correct |
294 ms |
20300 KB |
Output is correct |
50 |
Correct |
299 ms |
20672 KB |
Output is correct |
51 |
Correct |
289 ms |
20580 KB |
Output is correct |
52 |
Correct |
265 ms |
21048 KB |
Output is correct |
53 |
Correct |
271 ms |
21260 KB |
Output is correct |
54 |
Correct |
271 ms |
21048 KB |
Output is correct |