#include <stdio.h>
#include <stdlib.h>
#define N 300000
#define L 18
struct T {
int x;
T *l, *r;
};
struct T *node() {
T *t = (T *) calloc(1, sizeof *t);
t->x = 0;
t->l = t->r = NULL;
return t;
}
void st_update(struct T *t, int l, int r, int i, int x) {
int m;
if (l == r) {
t->x += x;
return;
}
m = (l + r) / 2;
if (i <= m) {
if (t->l == NULL)
t->l = node();
st_update(t->l, l, m, i, x);
} else {
if (t->r == NULL)
t->r = node();
st_update(t->r, m + 1, r, i, x);
}
t->x = (t->l == NULL ? 0 : t->l->x) + (t->r == NULL ? 0 : t->r->x);
}
int st_query(struct T *t, int l, int r, int ql, int qr) {
int m;
if (t == NULL)
return 0;
if (ql <= l && r <= qr)
return t->x;
if (qr < l || ql > r)
return 0;
m = (l + r) / 2;
return st_query(t->l, l, m, ql, qr) + st_query(t->r, m + 1, r, ql, qr);
}
struct T *ft1[N + 1];
int ft2[N], n;
void ft1_update(int i, int j, int x) {
if (j > n)
return;
while (i <= n) {
st_update(ft1[i], 0, n, j, x);
i |= i + 1;
}
}
int ft1_query(int i, int j) {
int sum = 0;
while (i >= 0) {
sum += st_query(ft1[i], 0, n, 0, j);
i &= i + 1, i--;
}
return sum;
}
void ft2_update(int i, int x) {
while (i < n) {
ft2[i] += x;
i |= i + 1;
}
}
int ft2_query(int i) {
int sum = 0;
while (i >= 0) {
sum += ft2[i];
i &= i + 1, i--;
}
return sum;
}
int find_left(int i) {
int i_ = i, x = ft2_query(i - 1);
for (int m = 1 << L; m >= 1; m >>= 1)
if (i_ - m >= 0 && x - ft2_query(i_ - m - 1) == i - (i_ - m))
i_ -= m;
return i_;
}
int find_right(int i) {
int i_ = i, x = ft2_query(i - 1);
for (int m = 1 << L; m >= 1; m >>= 1)
if (i_ + m <= n && ft2_query(i_ + m - 1) - x == (i_ + m) - i)
i_ += m;
return i_;
}
void update(int l, int l_, int r, int r_, int x) {
ft1_update(l, r, x);
ft1_update(l, r_ + 1, -x);
ft1_update(l_ + 1, r, -x);
ft1_update(l_ + 1, r_ + 1, x);
}
int main() {
static char cc[N];
int q;
scanf("%d%d", &n, &q);
for (int i = 0; i <= n; i++)
ft1[i] = node();
scanf("%s", cc);
for (int i = 0; i < n; i++)
if (cc[i] == '1')
ft2_update(i, +1);
for (int h = 1; h <= q; h++) {
static char s[8];
scanf("%s", s);
if (s[0] == 'q') {
int l, r, sum;
scanf("%d%d", &l, &r), l--, r--;
sum = ft1_query(l, r);
if (ft2_query(r - 1) - ft2_query(l - 1) == r - l)
sum += h;
printf("%d\n", sum);
} else {
int i;
scanf("%d", &i), i--;
if (cc[i] == '1') {
update(find_left(i), i, i + 1, find_right(i + 1), +h);
ft2_update(i, -1);
cc[i] = '0';
} else {
update(find_left(i), i, i + 1, find_right(i + 1), -h);
ft2_update(i, +1);
cc[i] = '1';
}
}
}
return 0;
}
Compilation message
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:121:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
121 | scanf("%d%d", &n, &q);
| ~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:124:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
124 | scanf("%s", cc);
| ~~~~~^~~~~~~~~~
street_lamps.cpp:131:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
131 | scanf("%s", s);
| ~~~~~^~~~~~~~~
street_lamps.cpp:135:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | scanf("%d%d", &l, &r), l--, r--;
| ~~~~~^~~~~~~~~~~~~~~~
street_lamps.cpp:143:9: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
143 | scanf("%d", &i), i--;
| ~~~~~^~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
272 ms |
1344 KB |
Output is correct |
2 |
Correct |
380 ms |
5040 KB |
Output is correct |
3 |
Correct |
750 ms |
11576 KB |
Output is correct |
4 |
Correct |
2285 ms |
282128 KB |
Output is correct |
5 |
Correct |
2130 ms |
267548 KB |
Output is correct |
6 |
Correct |
2603 ms |
301188 KB |
Output is correct |
7 |
Correct |
223 ms |
18852 KB |
Output is correct |
8 |
Correct |
260 ms |
21356 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
3 ms |
1108 KB |
Output is correct |
2 |
Correct |
4 ms |
920 KB |
Output is correct |
3 |
Correct |
2 ms |
724 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
3769 ms |
424004 KB |
Output is correct |
6 |
Correct |
2781 ms |
358692 KB |
Output is correct |
7 |
Correct |
1877 ms |
266916 KB |
Output is correct |
8 |
Correct |
258 ms |
21384 KB |
Output is correct |
9 |
Correct |
93 ms |
3988 KB |
Output is correct |
10 |
Correct |
111 ms |
4232 KB |
Output is correct |
11 |
Correct |
112 ms |
4436 KB |
Output is correct |
12 |
Correct |
205 ms |
18880 KB |
Output is correct |
13 |
Correct |
256 ms |
21412 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
2 ms |
688 KB |
Output is correct |
3 |
Correct |
3 ms |
852 KB |
Output is correct |
4 |
Correct |
3 ms |
944 KB |
Output is correct |
5 |
Correct |
303 ms |
35140 KB |
Output is correct |
6 |
Correct |
1360 ms |
227324 KB |
Output is correct |
7 |
Correct |
2274 ms |
300820 KB |
Output is correct |
8 |
Correct |
3439 ms |
347680 KB |
Output is correct |
9 |
Correct |
428 ms |
4268 KB |
Output is correct |
10 |
Correct |
341 ms |
3312 KB |
Output is correct |
11 |
Correct |
444 ms |
4340 KB |
Output is correct |
12 |
Correct |
318 ms |
3328 KB |
Output is correct |
13 |
Correct |
410 ms |
4292 KB |
Output is correct |
14 |
Correct |
343 ms |
3332 KB |
Output is correct |
15 |
Correct |
204 ms |
18856 KB |
Output is correct |
16 |
Correct |
241 ms |
21360 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
272 ms |
1344 KB |
Output is correct |
9 |
Correct |
380 ms |
5040 KB |
Output is correct |
10 |
Correct |
750 ms |
11576 KB |
Output is correct |
11 |
Correct |
2285 ms |
282128 KB |
Output is correct |
12 |
Correct |
2130 ms |
267548 KB |
Output is correct |
13 |
Correct |
2603 ms |
301188 KB |
Output is correct |
14 |
Correct |
223 ms |
18852 KB |
Output is correct |
15 |
Correct |
260 ms |
21356 KB |
Output is correct |
16 |
Correct |
3 ms |
1108 KB |
Output is correct |
17 |
Correct |
4 ms |
920 KB |
Output is correct |
18 |
Correct |
2 ms |
724 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
3769 ms |
424004 KB |
Output is correct |
21 |
Correct |
2781 ms |
358692 KB |
Output is correct |
22 |
Correct |
1877 ms |
266916 KB |
Output is correct |
23 |
Correct |
258 ms |
21384 KB |
Output is correct |
24 |
Correct |
93 ms |
3988 KB |
Output is correct |
25 |
Correct |
111 ms |
4232 KB |
Output is correct |
26 |
Correct |
112 ms |
4436 KB |
Output is correct |
27 |
Correct |
205 ms |
18880 KB |
Output is correct |
28 |
Correct |
256 ms |
21412 KB |
Output is correct |
29 |
Correct |
1 ms |
340 KB |
Output is correct |
30 |
Correct |
2 ms |
688 KB |
Output is correct |
31 |
Correct |
3 ms |
852 KB |
Output is correct |
32 |
Correct |
3 ms |
944 KB |
Output is correct |
33 |
Correct |
303 ms |
35140 KB |
Output is correct |
34 |
Correct |
1360 ms |
227324 KB |
Output is correct |
35 |
Correct |
2274 ms |
300820 KB |
Output is correct |
36 |
Correct |
3439 ms |
347680 KB |
Output is correct |
37 |
Correct |
428 ms |
4268 KB |
Output is correct |
38 |
Correct |
341 ms |
3312 KB |
Output is correct |
39 |
Correct |
444 ms |
4340 KB |
Output is correct |
40 |
Correct |
318 ms |
3328 KB |
Output is correct |
41 |
Correct |
410 ms |
4292 KB |
Output is correct |
42 |
Correct |
343 ms |
3332 KB |
Output is correct |
43 |
Correct |
204 ms |
18856 KB |
Output is correct |
44 |
Correct |
241 ms |
21360 KB |
Output is correct |
45 |
Correct |
87 ms |
2680 KB |
Output is correct |
46 |
Correct |
156 ms |
2780 KB |
Output is correct |
47 |
Correct |
1004 ms |
108552 KB |
Output is correct |
48 |
Correct |
2031 ms |
282356 KB |
Output is correct |
49 |
Correct |
116 ms |
4380 KB |
Output is correct |
50 |
Correct |
110 ms |
4360 KB |
Output is correct |
51 |
Correct |
108 ms |
4492 KB |
Output is correct |
52 |
Correct |
127 ms |
4788 KB |
Output is correct |
53 |
Correct |
118 ms |
4864 KB |
Output is correct |
54 |
Correct |
122 ms |
4760 KB |
Output is correct |