#include <bits/stdc++.h>
using namespace std;
const int N=300050;
const int M=18*18*N;
int state[N];
struct Seg
{
int l,r,tme;
Seg(){}
Seg(int a, int b, int c):l(a),r(b),tme(c){}
bool operator < (Seg b) const { return l<b.l;}
};
set<Seg> all;
Seg Get(int x)
{
auto it=all.lower_bound(Seg(x+1,0,0));
it--;
return *it;
}
int root[N],ls[M],rs[M],tsz,sum[M];
void Set(int &c, int ss, int se, int qs, int qe, int x)
{
if(qs>qe || qs>se || ss>qe) return;
if(!c) c=++tsz;
if(qs<=ss && qe>=se){ sum[c]+=x;return;}
int mid=ss+se>>1;
Set(ls[c],ss,mid,qs,qe,x);
Set(rs[c],mid+1,se,qs,qe,x);
}
int Get(int c, int ss, int se, int qi)
{
if(ss==se) return sum[c];
int mid=ss+se>>1;
if(qi<=mid) return sum[c]+Get(ls[c],ss,mid,qi);
else return sum[c]+Get(rs[c],mid+1,se,qi);
}
void Set(int qi, int l, int r, int x)
{
for(int i=qi;i<N;i+=i&-i) Set(root[i],1,N,l,r,x);
}
void Set(int l, int r, int x)
{
Set(l,l,r,x);
Set(r+1,l,r,-x);
}
int Get(int x, int y)
{
int ans=0;
for(int i=x;i;i-=i&-i) ans+=Get(root[i],1,N,y);
return ans;
}
void Set(Seg sg, int tme)
{
Set(sg.l,sg.r,tme-sg.tme);
}
void Update(int x, int tme)
{
if(state[x])
{
Seg tmp=Get(x);
Set(tmp,tme);
all.erase(tmp);
all.insert(Seg(tmp.l,x,tme));
all.insert(Seg(x+1,tmp.r,tme));
}
else
{
Seg L=Get(x),R=Get(x+1);
Set(L,tme);
Set(R,tme);
all.erase(L);
all.erase(R);
all.insert(Seg(L.l,R.r,tme));
}
state[x]^=1;
}
char s[N];
int main()
{
int n,q;
scanf("%i %i",&n,&q);
scanf("%s",s+1);
for(int i=1;i<=n;i++) state[i]=s[i]=='1';
for(int i=1,j=1;i<=n+1;i=j+1)
{
for(j=i;j<=n && state[j];j++);
all.insert(Seg(i,j,0));
}
char t[10];
for(int i=1;i<=q;i++)
{
scanf("%s",t);
if(t[0]=='t')
{
int x;
scanf("%i",&x);
Update(x,i);
}
else
{
int a,b;
scanf("%i %i",&a,&b);
int ans=Get(a,b);
Seg tmp=Get(a);
if(tmp.r>=b) ans+=i-tmp.tme;
printf("%i\n",ans);
}
}
return 0;
}
Compilation message
street_lamps.cpp: In function 'void Set(int&, int, int, int, int, int)':
street_lamps.cpp:26:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
26 | int mid=ss+se>>1;
| ~~^~~
street_lamps.cpp: In function 'int Get(int, int, int, int)':
street_lamps.cpp:33:16: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
33 | int mid=ss+se>>1;
| ~~^~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:81:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
81 | scanf("%i %i",&n,&q);
| ~~~~~^~~~~~~~~~~~~~~
street_lamps.cpp:82:11: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
82 | scanf("%s",s+1);
| ~~~~~^~~~~~~~~~
street_lamps.cpp:92:12: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | scanf("%s",t);
| ~~~~~^~~~~~~~
street_lamps.cpp:96:13: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
96 | scanf("%i",&x);
| ~~~~~^~~~~~~~~
street_lamps.cpp:102:13: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
102 | scanf("%i %i",&a,&b);
| ~~~~~^~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1230 ms |
4584 KB |
Output is correct |
2 |
Correct |
1173 ms |
5124 KB |
Output is correct |
3 |
Correct |
1146 ms |
9384 KB |
Output is correct |
4 |
Correct |
1871 ms |
121648 KB |
Output is correct |
5 |
Correct |
2043 ms |
132948 KB |
Output is correct |
6 |
Correct |
1966 ms |
128460 KB |
Output is correct |
7 |
Correct |
699 ms |
26980 KB |
Output is correct |
8 |
Correct |
311 ms |
9600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
10 ms |
1100 KB |
Output is correct |
2 |
Correct |
8 ms |
972 KB |
Output is correct |
3 |
Correct |
6 ms |
844 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
3263 ms |
199240 KB |
Output is correct |
6 |
Correct |
2654 ms |
171164 KB |
Output is correct |
7 |
Correct |
1834 ms |
132420 KB |
Output is correct |
8 |
Correct |
299 ms |
9636 KB |
Output is correct |
9 |
Correct |
124 ms |
4012 KB |
Output is correct |
10 |
Correct |
137 ms |
4256 KB |
Output is correct |
11 |
Correct |
135 ms |
4492 KB |
Output is correct |
12 |
Correct |
672 ms |
27080 KB |
Output is correct |
13 |
Correct |
292 ms |
9668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
4 ms |
716 KB |
Output is correct |
3 |
Correct |
6 ms |
844 KB |
Output is correct |
4 |
Correct |
8 ms |
844 KB |
Output is correct |
5 |
Correct |
627 ms |
25508 KB |
Output is correct |
6 |
Correct |
1295 ms |
101736 KB |
Output is correct |
7 |
Correct |
1860 ms |
127996 KB |
Output is correct |
8 |
Correct |
2556 ms |
146100 KB |
Output is correct |
9 |
Correct |
1572 ms |
4328 KB |
Output is correct |
10 |
Correct |
2204 ms |
3524 KB |
Output is correct |
11 |
Correct |
1641 ms |
4572 KB |
Output is correct |
12 |
Correct |
2197 ms |
3388 KB |
Output is correct |
13 |
Correct |
1571 ms |
4232 KB |
Output is correct |
14 |
Correct |
2233 ms |
3520 KB |
Output is correct |
15 |
Correct |
654 ms |
27080 KB |
Output is correct |
16 |
Correct |
293 ms |
9668 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
316 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1230 ms |
4584 KB |
Output is correct |
9 |
Correct |
1173 ms |
5124 KB |
Output is correct |
10 |
Correct |
1146 ms |
9384 KB |
Output is correct |
11 |
Correct |
1871 ms |
121648 KB |
Output is correct |
12 |
Correct |
2043 ms |
132948 KB |
Output is correct |
13 |
Correct |
1966 ms |
128460 KB |
Output is correct |
14 |
Correct |
699 ms |
26980 KB |
Output is correct |
15 |
Correct |
311 ms |
9600 KB |
Output is correct |
16 |
Correct |
10 ms |
1100 KB |
Output is correct |
17 |
Correct |
8 ms |
972 KB |
Output is correct |
18 |
Correct |
6 ms |
844 KB |
Output is correct |
19 |
Correct |
2 ms |
332 KB |
Output is correct |
20 |
Correct |
3263 ms |
199240 KB |
Output is correct |
21 |
Correct |
2654 ms |
171164 KB |
Output is correct |
22 |
Correct |
1834 ms |
132420 KB |
Output is correct |
23 |
Correct |
299 ms |
9636 KB |
Output is correct |
24 |
Correct |
124 ms |
4012 KB |
Output is correct |
25 |
Correct |
137 ms |
4256 KB |
Output is correct |
26 |
Correct |
135 ms |
4492 KB |
Output is correct |
27 |
Correct |
672 ms |
27080 KB |
Output is correct |
28 |
Correct |
292 ms |
9668 KB |
Output is correct |
29 |
Correct |
2 ms |
332 KB |
Output is correct |
30 |
Correct |
4 ms |
716 KB |
Output is correct |
31 |
Correct |
6 ms |
844 KB |
Output is correct |
32 |
Correct |
8 ms |
844 KB |
Output is correct |
33 |
Correct |
627 ms |
25508 KB |
Output is correct |
34 |
Correct |
1295 ms |
101736 KB |
Output is correct |
35 |
Correct |
1860 ms |
127996 KB |
Output is correct |
36 |
Correct |
2556 ms |
146100 KB |
Output is correct |
37 |
Correct |
1572 ms |
4328 KB |
Output is correct |
38 |
Correct |
2204 ms |
3524 KB |
Output is correct |
39 |
Correct |
1641 ms |
4572 KB |
Output is correct |
40 |
Correct |
2197 ms |
3388 KB |
Output is correct |
41 |
Correct |
1571 ms |
4232 KB |
Output is correct |
42 |
Correct |
2233 ms |
3520 KB |
Output is correct |
43 |
Correct |
654 ms |
27080 KB |
Output is correct |
44 |
Correct |
293 ms |
9668 KB |
Output is correct |
45 |
Correct |
876 ms |
2900 KB |
Output is correct |
46 |
Correct |
818 ms |
2868 KB |
Output is correct |
47 |
Correct |
1029 ms |
52868 KB |
Output is correct |
48 |
Correct |
1686 ms |
121396 KB |
Output is correct |
49 |
Correct |
150 ms |
4420 KB |
Output is correct |
50 |
Correct |
151 ms |
4440 KB |
Output is correct |
51 |
Correct |
157 ms |
4676 KB |
Output is correct |
52 |
Correct |
167 ms |
4840 KB |
Output is correct |
53 |
Correct |
169 ms |
4932 KB |
Output is correct |
54 |
Correct |
168 ms |
4940 KB |
Output is correct |