#include <bits/stdc++.h>
using namespace std;
//build a segtree on time
//left-update-right dnc on right
#ifdef DEBUG
#define dbg(...) printf(__VA_ARGS__);
#else
#define dbg(...)
#endif
#define sf scanf
#define pf printf
#define fi first
#define se second
#define pb push_back
#define sz(x) (int)x.size()
#define mnto(x,y) x=min(x,(__typeof__(x))y)
#define mxto(x,y) x=max(x,(__typeof__(x))y)
#define INF 1023456789
#define LINF 1023456789123456789
#define all(x) x.begin(), x.end()
typedef long long ll;
typedef long double ld;
typedef pair<int, int> ii;
typedef pair<ll, ll> pll;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef vector<int> vi;
typedef vector<ii> vii;
typedef vector<pll> vll;
mt19937 rng(time(0));
#define maxn 300005
struct upd{
int l,st,et,v;
bool operator< (const upd &u){
return this->l<u.l;
}
};
struct qry{
int l,t;
bool operator< (const qry &q){
return this->l<q.l;
}
};
int n,q,state[maxn],ans[maxn],f1[maxn+5],f2[maxn+5];
char c,s[6];
void up(int x,int v,int* f){
while(x<=q+5)f[x]+=v,x+=x&-x;
}
void up(int l,int r,int v){
++l;++r;
up(l,v,f1);up(r+1,-v,f1);
up(l,-v*(l-1),f2);up(r+1,v*r,f2);
}
int sum(int x,int* f){
int res=0;
while(x)res+=f[x],x-=x&-x;
return res;
}
int sum(int x){
return x*sum(x,f1)+sum(x,f2);
}
int sum(int l,int r){
++l;++r;
return sum(r)-sum(l-1);
}
int stime[maxn];
set<ii> ranges;
vector<upd> updates[maxn],tmpu;
vector<qry> queries[maxn],tmpq;
void add_upd(int l,int r,int st,int et){
updates[l].push_back({l,st,et,1});
updates[r+1].push_back({l,st,et,-1});
}
void dnc(int l,int r){
if(l==r){
int ptr=0;
for(qry &i:queries[l]){
while(ptr<sz(updates[l])&&updates[l][ptr].l<=i.l){
upd &u=updates[l][ptr];
up(u.st,u.et,u.v);
++ptr;
}
ans[i.t+1]+=sum(0,i.t);
}
for(int i=0;i<ptr;++i){
upd &u=updates[l][i];
up(u.st,u.et,-u.v);
}
return;
}
int m=(l+r)/2;
dnc(l,m);dnc(m+1,r);
vector<upd> &lupd=updates[l],&rupd=updates[m+1];
vector<qry> &lqry=queries[l],&rqry=queries[m+1];
int ptr=0;
for(qry &i:rqry){
while(ptr<sz(lupd)&&lupd[ptr].l<=i.l){
up(lupd[ptr].st,lupd[ptr].et,lupd[ptr].v);
++ptr;
}
ans[i.t+1]+=sum(0,i.t);
}
for(int i=0;i<ptr;++i){
up(lupd[i].st,lupd[i].et,-lupd[i].v);
}
merge(all(lupd),all(rupd),back_inserter(tmpu));
merge(all(lqry),all(rqry),back_inserter(tmpq));
swap(tmpu,updates[l]);
swap(tmpq,queries[l]);
tmpq.clear();
tmpu.clear();
}
int main(){
sf("%d%d",&n,&q);
for(int i=1;i<=n;++i){
sf(" %c",&c);
state[i]=c-'0';
}
int pv=-1;
for(int i=1;i<=n;++i){
if(state[i]==1&&state[i-1]==0)pv=i;
if(state[i]==1&&state[i+1]==0){
ranges.insert({pv,i});
stime[pv]=0;
}
}
memset(ans,-1,sizeof ans);
for(int i=1;i<=q;++i){
sf(" %s",&s);
if(s[0]=='t'){
int x;sf("%d",&x);
if(state[x]==1){
state[x]=0;
auto it=--ranges.upper_bound({x,INF});
int l=it->fi,r=it->se;
ranges.erase(it);
add_upd(l,r,stime[l],i-1);
if(l<x)stime[l]=i,ranges.insert({l,x-1});
if(x<r)stime[x+1]=i,ranges.insert({x+1,r});
}
else{
state[x]=1;
int nl=x,nr=x;
auto it=ranges.upper_bound({x,INF});
if(it!=ranges.begin()){
--it;
int l=it->fi,r=it->se;
if(r==x-1){
ranges.erase(it);
add_upd(l,r,stime[l],i-1);
nl=l;
}
}
it=ranges.upper_bound({x,INF});
if(it!=ranges.end()){
int l=it->fi,r=it->se;
if(x+1==l){
ranges.erase(it);
add_upd(l,r,stime[l],i-1);
nr=r;
}
}
stime[nl]=i;
ranges.insert({nl,nr});
}
}
else{
int l,r;sf("%d%d",&l,&r);
queries[r-1].pb({l,i-1});
ans[i]=0;
}
}
auto it=ranges.begin();
while(it!=ranges.end()){
add_upd(it->fi,it->se,stime[it->fi],q);
++it;
}
for(int i=1;i<=n+1;++i){
sort(all(queries[i]));
sort(all(updates[i]));
}
dnc(1,n+1);
for(int i=1;i<=q;++i){
if(ans[i]!=-1)pf("%d\n",ans[i]);
}
}
/*
5 7
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 1 6
*/
Compilation message
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:148:9: warning: format '%s' expects argument of type 'char*', but argument 2 has type 'char (*)[6]' [-Wformat=]
148 | sf(" %s",&s);
| ~^ ~~
| | |
| | char (*)[6]
| char*
street_lamps.cpp:132:4: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | sf("%d%d",&n,&q);
| ^
street_lamps.cpp:134:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | sf(" %c",&c);
| ^
street_lamps.cpp:148:5: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | sf(" %s",&s);
| ^
street_lamps.cpp:150:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | int x;sf("%d",&x);
| ^
street_lamps.cpp:187:14: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
187 | int l,r;sf("%d%d",&l,&r);
| ^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15548 KB |
Output is correct |
2 |
Correct |
8 ms |
15564 KB |
Output is correct |
3 |
Correct |
8 ms |
15552 KB |
Output is correct |
4 |
Correct |
8 ms |
15548 KB |
Output is correct |
5 |
Correct |
8 ms |
15552 KB |
Output is correct |
6 |
Correct |
9 ms |
15544 KB |
Output is correct |
7 |
Correct |
9 ms |
15552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
506 ms |
61668 KB |
Output is correct |
2 |
Correct |
522 ms |
81016 KB |
Output is correct |
3 |
Correct |
665 ms |
109884 KB |
Output is correct |
4 |
Correct |
1378 ms |
184104 KB |
Output is correct |
5 |
Correct |
1290 ms |
170024 KB |
Output is correct |
6 |
Correct |
1295 ms |
190920 KB |
Output is correct |
7 |
Correct |
364 ms |
77180 KB |
Output is correct |
8 |
Correct |
373 ms |
77592 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15892 KB |
Output is correct |
2 |
Correct |
10 ms |
15820 KB |
Output is correct |
3 |
Correct |
10 ms |
15872 KB |
Output is correct |
4 |
Correct |
10 ms |
15768 KB |
Output is correct |
5 |
Correct |
888 ms |
231220 KB |
Output is correct |
6 |
Correct |
982 ms |
227832 KB |
Output is correct |
7 |
Correct |
866 ms |
169540 KB |
Output is correct |
8 |
Correct |
390 ms |
72864 KB |
Output is correct |
9 |
Correct |
125 ms |
32544 KB |
Output is correct |
10 |
Correct |
135 ms |
35752 KB |
Output is correct |
11 |
Correct |
140 ms |
35884 KB |
Output is correct |
12 |
Correct |
366 ms |
72624 KB |
Output is correct |
13 |
Correct |
377 ms |
73224 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
9 ms |
15692 KB |
Output is correct |
2 |
Correct |
9 ms |
15820 KB |
Output is correct |
3 |
Correct |
9 ms |
15824 KB |
Output is correct |
4 |
Correct |
9 ms |
16064 KB |
Output is correct |
5 |
Correct |
558 ms |
128296 KB |
Output is correct |
6 |
Correct |
770 ms |
166120 KB |
Output is correct |
7 |
Correct |
994 ms |
191216 KB |
Output is correct |
8 |
Correct |
1122 ms |
256460 KB |
Output is correct |
9 |
Correct |
708 ms |
77392 KB |
Output is correct |
10 |
Correct |
674 ms |
89980 KB |
Output is correct |
11 |
Correct |
622 ms |
78340 KB |
Output is correct |
12 |
Correct |
572 ms |
89676 KB |
Output is correct |
13 |
Correct |
545 ms |
77208 KB |
Output is correct |
14 |
Correct |
556 ms |
89556 KB |
Output is correct |
15 |
Correct |
368 ms |
72024 KB |
Output is correct |
16 |
Correct |
385 ms |
72944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
15548 KB |
Output is correct |
2 |
Correct |
8 ms |
15564 KB |
Output is correct |
3 |
Correct |
8 ms |
15552 KB |
Output is correct |
4 |
Correct |
8 ms |
15548 KB |
Output is correct |
5 |
Correct |
8 ms |
15552 KB |
Output is correct |
6 |
Correct |
9 ms |
15544 KB |
Output is correct |
7 |
Correct |
9 ms |
15552 KB |
Output is correct |
8 |
Correct |
506 ms |
61668 KB |
Output is correct |
9 |
Correct |
522 ms |
81016 KB |
Output is correct |
10 |
Correct |
665 ms |
109884 KB |
Output is correct |
11 |
Correct |
1378 ms |
184104 KB |
Output is correct |
12 |
Correct |
1290 ms |
170024 KB |
Output is correct |
13 |
Correct |
1295 ms |
190920 KB |
Output is correct |
14 |
Correct |
364 ms |
77180 KB |
Output is correct |
15 |
Correct |
373 ms |
77592 KB |
Output is correct |
16 |
Correct |
9 ms |
15892 KB |
Output is correct |
17 |
Correct |
10 ms |
15820 KB |
Output is correct |
18 |
Correct |
10 ms |
15872 KB |
Output is correct |
19 |
Correct |
10 ms |
15768 KB |
Output is correct |
20 |
Correct |
888 ms |
231220 KB |
Output is correct |
21 |
Correct |
982 ms |
227832 KB |
Output is correct |
22 |
Correct |
866 ms |
169540 KB |
Output is correct |
23 |
Correct |
390 ms |
72864 KB |
Output is correct |
24 |
Correct |
125 ms |
32544 KB |
Output is correct |
25 |
Correct |
135 ms |
35752 KB |
Output is correct |
26 |
Correct |
140 ms |
35884 KB |
Output is correct |
27 |
Correct |
366 ms |
72624 KB |
Output is correct |
28 |
Correct |
377 ms |
73224 KB |
Output is correct |
29 |
Correct |
9 ms |
15692 KB |
Output is correct |
30 |
Correct |
9 ms |
15820 KB |
Output is correct |
31 |
Correct |
9 ms |
15824 KB |
Output is correct |
32 |
Correct |
9 ms |
16064 KB |
Output is correct |
33 |
Correct |
558 ms |
128296 KB |
Output is correct |
34 |
Correct |
770 ms |
166120 KB |
Output is correct |
35 |
Correct |
994 ms |
191216 KB |
Output is correct |
36 |
Correct |
1122 ms |
256460 KB |
Output is correct |
37 |
Correct |
708 ms |
77392 KB |
Output is correct |
38 |
Correct |
674 ms |
89980 KB |
Output is correct |
39 |
Correct |
622 ms |
78340 KB |
Output is correct |
40 |
Correct |
572 ms |
89676 KB |
Output is correct |
41 |
Correct |
545 ms |
77208 KB |
Output is correct |
42 |
Correct |
556 ms |
89556 KB |
Output is correct |
43 |
Correct |
368 ms |
72024 KB |
Output is correct |
44 |
Correct |
385 ms |
72944 KB |
Output is correct |
45 |
Correct |
169 ms |
35260 KB |
Output is correct |
46 |
Correct |
247 ms |
43468 KB |
Output is correct |
47 |
Correct |
460 ms |
82732 KB |
Output is correct |
48 |
Correct |
879 ms |
180640 KB |
Output is correct |
49 |
Correct |
157 ms |
37360 KB |
Output is correct |
50 |
Correct |
154 ms |
37496 KB |
Output is correct |
51 |
Correct |
156 ms |
38044 KB |
Output is correct |
52 |
Correct |
165 ms |
40672 KB |
Output is correct |
53 |
Correct |
162 ms |
40748 KB |
Output is correct |
54 |
Correct |
169 ms |
40776 KB |
Output is correct |