#include <bits/stdc++.h>
#define fi first
#define se second
#define ryan bear
#define all(V) ((V).begin()), ((V).end())
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef long double ld;
typedef vector<int> vim;
typedef vector<ll> vlm;
int R, C;
int dty_cnt=1;
struct dty {
int l, r, dt, Key;
}st2[(1<<24)];
int new_dty(int k) { st2[dty_cnt].Key=k; return dty_cnt++; }
inline void upd2(int now, int y, int val, int ys, int ye) {
int md=(ys+ye)/2;
if (st2[now].Key) {
if (st2[now].Key==y) { st2[now].dt+=val; return ;}
if (st2[now].Key<=md) { st2[now].l=new_dty(st2[now].Key); st2[st2[now].l].dt=st2[now].dt; }
else { st2[now].r=new_dty(st2[now].Key); st2[st2[now].r].dt=st2[now].dt; }
st2[now].Key=0;
}
if (y<=md) {
if (!st2[now].l) st2[now].l=new_dty(y);
upd2(st2[now].l, y, val, ys, md);
}
else {
if (!st2[now].r) st2[now].r=new_dty(y);
upd2(st2[now].r, y, val, md+1, ye);
}
int L, R;
L=(st2[now].l?st2[st2[now].l].dt:0);
R=(st2[now].r?st2[st2[now].r].dt:0);
st2[now].dt=L+R;
}
inline int get2(int now, int y1, int y2, int ys, int ye) {
int md=(ys+ye)/2;
if (st2[now].Key) return (y1<=st2[now].Key&&st2[now].Key<=y2)?st2[now].dt:0;
if (y1<=ys&&ye<=y2) return st2[now].dt;
int L, R; L=R=0;
if (!(md<y1)) L=(st2[now].l?(get2(st2[now].l, y1, y2, ys, md)):0);
if (md+1<=y2&&md+1<=ye) R=(st2[now].r?get2(st2[now].r, y1, y2, md+1, ye):0);
return L+R;
}
struct dtx {
int seg, l, r;
}st1[(1<<19)];
int dtx_cnt=1; int new_dtx() { return dtx_cnt++; }
inline void upd1(int now, int x, int y, int val, int xs, int xe) {
if (!st1[now].seg) st1[now].seg=new_dty(y);
if (xs==xe) { upd2(st1[now].seg, y, val, 1, C); return ; }
int md=(xs+xe)/2;
if (x<=md) {
if (!st1[now].l) st1[now].l=new_dtx();
upd1(st1[now].l, x, y, val, xs, md);
}
else {
if (!st1[now].r) st1[now].r=new_dtx();
upd1(st1[now].r, x, y, val, md+1, xe);
}
upd2(st1[now].seg, y, val, 1, C);
}
inline int get1(int now, int x1, int y1, int x2, int y2, int xs, int xe) {
if (!st1[now].seg) return 0;
if (x1<=xs&&xe<=x2) return get2(st1[now].seg, y1, y2, 1, C);
int md=(xs+xe)/2, L, R; L=R=0;
if (x1<=md) L=(st1[now].l?get1(st1[now].l, x1, y1, x2, y2, xs, md):0);
if (md+1<=x2&&md+1<=xe) R=(st1[now].r?get1(st1[now].r, x1, y1, x2, y2, md+1, xe):0);
return L+R;
}
int root;
int n, q;
char s[10]; int I, a, b;
int ar[300010];
set<pair<pii, int> > S;
int main() {
root=new_dtx();
scanf("%d %d", &n, &q);
R=C=n;
for (int i=1; i<=n; i++) scanf("%1d", &ar[i]);
for (int i=1; i<=n; i++) {
if (!ar[i]) continue;
int j;
for (j=i; j<=n; j++) if (!ar[j]) break;
S.insert({make_pair(i, j-1), 0});
i=j-1;
}
set<pair<pii, int> >::iterator it;
pair<pii, int> pi, pi1, pi2;
int ans;
for (int i=1; i<=q; i++) {
scanf("%s", s);
if (s[0]=='t') {
scanf("%d", &I);
if (ar[I]) {
it=S.upper_bound({make_pair(I, (1<<30)), (1<<30)});
it--; pi=(*it); S.erase(it);
upd1(root, pi.fi.fi, pi.fi.se, i-pi.se, 1, R);
if (pi.fi.fi<I) S.insert({make_pair(pi.fi.fi, I-1), i});
if (pi.fi.se>I) S.insert({make_pair(I+1, pi.fi.se), i});
}
else {
if (ar[I-1]) {
it=S.upper_bound({make_pair(I-1, (1<<30)), (1<<30)});
it--; pi1=(*it); S.erase(it);
upd1(root, pi1.fi.fi, pi1.fi.se, i-pi1.se, 1, R);
}
else pi1={make_pair(I, I), 0};
if (ar[I+1]) {
it=S.upper_bound({make_pair(I+1, (1<<30)), (1<<30)});
it--; pi2=(*it); S.erase(it);
upd1(root, pi2.fi.fi, pi2.fi.se, i-pi2.se, 1, R);
}
else pi2={make_pair(I, I), 0};
S.insert({make_pair(pi1.fi.fi, pi2.fi.se), i});
}
ar[I]=1-ar[I];
}
else {
scanf("%d %d", &a, &b); b--;
if (!S.size()) ans=0;
else {
it=S.upper_bound({make_pair(a, (1<<30)), (1<<30)});
if (it==S.begin()) ans=0;
else {
it--;
if ((*it).fi.fi<=a&&(*it).fi.se>=b) ans=i-(*it).se;
else ans=0;
}
}
ans+=get1(root, 1, b, a, C, 1, R);
printf("%d\n", ans);
}
}
return 0;
}
Compilation message
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:87:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &n, &q);
~~~~~^~~~~~~~~~~~~~~~~
street_lamps.cpp:89:32: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for (int i=1; i<=n; i++) scanf("%1d", &ar[i]);
~~~~~^~~~~~~~~~~~~~~
street_lamps.cpp:101:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", s);
~~~~~^~~~~~~~~
street_lamps.cpp:103:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &I);
~~~~~^~~~~~~~~~
street_lamps.cpp:129:9: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d", &a, &b); b--;
~~~~~^~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
281 ms |
1400 KB |
Output is correct |
2 |
Correct |
369 ms |
1784 KB |
Output is correct |
3 |
Correct |
784 ms |
6780 KB |
Output is correct |
4 |
Correct |
1938 ms |
112684 KB |
Output is correct |
5 |
Correct |
2333 ms |
128504 KB |
Output is correct |
6 |
Correct |
2093 ms |
127352 KB |
Output is correct |
7 |
Correct |
189 ms |
2168 KB |
Output is correct |
8 |
Correct |
201 ms |
3552 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
632 KB |
Output is correct |
2 |
Correct |
7 ms |
632 KB |
Output is correct |
3 |
Correct |
7 ms |
504 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
2463 ms |
184568 KB |
Output is correct |
6 |
Correct |
2456 ms |
175608 KB |
Output is correct |
7 |
Correct |
1934 ms |
133240 KB |
Output is correct |
8 |
Correct |
198 ms |
9464 KB |
Output is correct |
9 |
Correct |
122 ms |
4088 KB |
Output is correct |
10 |
Correct |
145 ms |
4344 KB |
Output is correct |
11 |
Correct |
133 ms |
4708 KB |
Output is correct |
12 |
Correct |
188 ms |
8184 KB |
Output is correct |
13 |
Correct |
196 ms |
9464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
504 KB |
Output is correct |
3 |
Correct |
7 ms |
504 KB |
Output is correct |
4 |
Correct |
7 ms |
632 KB |
Output is correct |
5 |
Correct |
590 ms |
9848 KB |
Output is correct |
6 |
Correct |
1217 ms |
77560 KB |
Output is correct |
7 |
Correct |
1797 ms |
126608 KB |
Output is correct |
8 |
Correct |
2535 ms |
175072 KB |
Output is correct |
9 |
Correct |
378 ms |
4324 KB |
Output is correct |
10 |
Correct |
293 ms |
3448 KB |
Output is correct |
11 |
Correct |
377 ms |
4344 KB |
Output is correct |
12 |
Correct |
299 ms |
3576 KB |
Output is correct |
13 |
Correct |
376 ms |
4344 KB |
Output is correct |
14 |
Correct |
294 ms |
3448 KB |
Output is correct |
15 |
Correct |
180 ms |
8056 KB |
Output is correct |
16 |
Correct |
195 ms |
9468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
5 ms |
376 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
281 ms |
1400 KB |
Output is correct |
9 |
Correct |
369 ms |
1784 KB |
Output is correct |
10 |
Correct |
784 ms |
6780 KB |
Output is correct |
11 |
Correct |
1938 ms |
112684 KB |
Output is correct |
12 |
Correct |
2333 ms |
128504 KB |
Output is correct |
13 |
Correct |
2093 ms |
127352 KB |
Output is correct |
14 |
Correct |
189 ms |
2168 KB |
Output is correct |
15 |
Correct |
201 ms |
3552 KB |
Output is correct |
16 |
Correct |
7 ms |
632 KB |
Output is correct |
17 |
Correct |
7 ms |
632 KB |
Output is correct |
18 |
Correct |
7 ms |
504 KB |
Output is correct |
19 |
Correct |
5 ms |
376 KB |
Output is correct |
20 |
Correct |
2463 ms |
184568 KB |
Output is correct |
21 |
Correct |
2456 ms |
175608 KB |
Output is correct |
22 |
Correct |
1934 ms |
133240 KB |
Output is correct |
23 |
Correct |
198 ms |
9464 KB |
Output is correct |
24 |
Correct |
122 ms |
4088 KB |
Output is correct |
25 |
Correct |
145 ms |
4344 KB |
Output is correct |
26 |
Correct |
133 ms |
4708 KB |
Output is correct |
27 |
Correct |
188 ms |
8184 KB |
Output is correct |
28 |
Correct |
196 ms |
9464 KB |
Output is correct |
29 |
Correct |
6 ms |
376 KB |
Output is correct |
30 |
Correct |
6 ms |
504 KB |
Output is correct |
31 |
Correct |
7 ms |
504 KB |
Output is correct |
32 |
Correct |
7 ms |
632 KB |
Output is correct |
33 |
Correct |
590 ms |
9848 KB |
Output is correct |
34 |
Correct |
1217 ms |
77560 KB |
Output is correct |
35 |
Correct |
1797 ms |
126608 KB |
Output is correct |
36 |
Correct |
2535 ms |
175072 KB |
Output is correct |
37 |
Correct |
378 ms |
4324 KB |
Output is correct |
38 |
Correct |
293 ms |
3448 KB |
Output is correct |
39 |
Correct |
377 ms |
4344 KB |
Output is correct |
40 |
Correct |
299 ms |
3576 KB |
Output is correct |
41 |
Correct |
376 ms |
4344 KB |
Output is correct |
42 |
Correct |
294 ms |
3448 KB |
Output is correct |
43 |
Correct |
180 ms |
8056 KB |
Output is correct |
44 |
Correct |
195 ms |
9468 KB |
Output is correct |
45 |
Correct |
112 ms |
2808 KB |
Output is correct |
46 |
Correct |
178 ms |
2828 KB |
Output is correct |
47 |
Correct |
878 ms |
57592 KB |
Output is correct |
48 |
Correct |
1593 ms |
117240 KB |
Output is correct |
49 |
Correct |
144 ms |
4472 KB |
Output is correct |
50 |
Correct |
152 ms |
4472 KB |
Output is correct |
51 |
Correct |
148 ms |
4856 KB |
Output is correct |
52 |
Correct |
141 ms |
5112 KB |
Output is correct |
53 |
Correct |
142 ms |
4856 KB |
Output is correct |
54 |
Correct |
140 ms |
4856 KB |
Output is correct |