#include <bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
const int MAXQ = 300005;
struct Aib {
int n;
vector<int>aib;
Aib(int _n = 0) {
n = _n;
aib.resize(n + 5);
for (int i = 1; i <= n; ++i)
aib[i] = 0;
}
void rsz(int _n) {
n = _n;
aib.resize(n + 5);
for (int i = 1; i <= n; ++i)
aib[i] = 0;
}
void update(int pos, int val) {
if (pos < 0)
return ;
for (; pos <= n; pos += (pos & -pos))
aib[pos] += val;
}
int query(int pos) {
int ans = 0;
for (; pos; pos -= (pos & -pos))
ans += aib[pos];
return ans;
}
}gen;
struct Toggle {
int t, pos;
} q1[MAXQ];
struct Query {
int t, a, b;
} q2[MAXQ];
struct Node {
vector<int>qry;
Aib a;
} aint[4 * MAXN];
char s[MAXN], qt[10];
int v[MAXN];
int ans[MAXQ];
pair<int, int>bs(int pos, int n) {
int l = 1, r = pos - 1, last1 = pos;
while (l <= r) {
int mid = (l + r) / 2;
if (gen.query(pos - 1) - gen.query(mid - 1) == pos - mid) {
last1 = mid;
r = mid - 1;
} else {
l = mid + 1;
}
}
int last2 = pos;
l = pos + 1, r = n;
while (l <= r) {
int mid = (l + r) / 2;
if (gen.query(mid) - gen.query(pos) == mid - pos) {
last2 = mid;
l = mid + 1;
} else {
r = mid - 1;
}
}
return {last1, last2};
}
void add(int node, int l, int r, int k) {
aint[node].qry.push_back(q2[k].b);
if (l == r)
return ;
int mid = (l + r) / 2;
if (q2[k].a <= mid)
add(2 * node, l, mid, k);
else
add(2 * node + 1, mid + 1, r, k);
}
void build(int node, int l, int r) {
if ((int)(aint[node].qry.size()) == 0)
return ;
sort(aint[node].qry.begin(), aint[node].qry.end());
aint[node].a.rsz((int)(aint[node].qry.size()));
if (l == r)
return ;
int mid = (l + r) / 2;
build(2 * node, l, mid);
build(2 * node + 1, mid + 1, r);
}
void update(int node, int l, int r, int a, int b, int x, int val) {
if ((int)(aint[node].qry.size()) == 0)
return ;
if (a <= l && r <= b) {
int l1 = 0, r1 = (int)(aint[node].qry.size()) - 1;
int last1 = -1, last2 = -2;
while (l1 <= r1) {
int mid1 = (l1 + r1) / 2;
if (aint[node].qry[mid1] >= b) {
last1 = mid1;
r1 = mid1 - 1;
} else {
l1 = mid1 + 1;
}
}
l1 = 0, r1 = (int)(aint[node].qry.size()) - 1;
while (l1 <= r1) {
int mid1 = (l1 + r1) / 2;
if (aint[node].qry[mid1] <= x) {
last2 = mid1;
l1 = mid1 + 1;
} else {
r1 = mid1 - 1;
}
}
if (last1 != -1 && last2 != -2) {
aint[node].a.update(last1 + 1, val);
aint[node].a.update(last2 + 2, -val);
}
return ;
}
if (l == r)
return ;
int mid = (l + r) / 2;
if (a <= mid)
update(2 * node, l, mid, a, b, x, val);
if (b > mid)
update(2 * node + 1, mid + 1, r, a, b, x, val);
}
int query(int node, int l, int r, int k) {
if ((int)(aint[node].qry.size()) == 0)
return 0;
int l1 = 0, r1 = (int)(aint[node].qry.size()) - 1, last = -1;
while (l1 <= r1) {
int mid1 = (l1 + r1) / 2;
if (aint[node].qry[mid1] <= q2[k].b) {
last = mid1;
l1 = mid1 + 1;
} else {
r1 = mid1 - 1;
}
}
int ans = aint[node].a.query(last + 1);
if (l == r)
return ans;
int mid = (l + r) / 2;
if (q2[k].a <= mid)
ans += query(2 * node, l, mid, k);
else
ans += query(2 * node + 1, mid + 1, r, k);
return ans;
}
int main() {
//freopen("date.in", "r", stdin);
//freopen("date.out", "w", stdout);
int n, q;
scanf("%d%d ", &n, &q);
scanf("%s", s + 1);
int k1 = 0, k2 = 0;
for (int i = 1; i <= q; ++i) {
scanf("%s", qt);
if (qt[0] == 't') {
k1++;
scanf("%d ", &q1[k1].pos);
q1[k1].t = i;
} else {
k2++;
scanf("%d%d ", &q2[k2].a, &q2[k2].b);
q2[k2].b--;
q2[k2].t = i;
add(1, 1, n, k2);
}
}
build(1, 1, n);
gen.rsz(n);
for (int i = 1; i <= n; ++i) {
if (s[i] == '1') {
gen.update(i, 1);
v[i] = 1;
}
}
for (int i = 1, j = 1; i <= k1 || j <= k2; ) {
if (i <= k1 && (j > k2 || q1[i].t < q2[j].t)) {
int val;
if (v[q1[i].pos] == 1) {
val = i + j - 1;
v[q1[i].pos] = 0;
gen.update(q1[i].pos, -1);
} else {
val = -(i + j - 1);
v[q1[i].pos] = 1;
gen.update(q1[i].pos, 1);
}
pair<int, int>interval = bs(q1[i].pos, n);
update(1, 1, n, interval.first, q1[i].pos, interval.second, val);
i++;
} else {
ans[j] = query(1, 1, n, j);
int x = gen.query(q2[j].b) - gen.query(q2[j].a - 1);
if (gen.query(q2[j].b) - gen.query(q2[j].a - 1) == q2[j].b - q2[j].a + 1)
ans[j] += i + j - 1;
j++;
}
}
for (int i = 1; i <= k2; ++i)
printf("%d\n", ans[i]);
return 0;
}
Compilation message
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:219:11: warning: unused variable 'x' [-Wunused-variable]
int x = gen.query(q2[j].b) - gen.query(q2[j].a - 1);
^
street_lamps.cpp:175:8: 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:176:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", s + 1);
~~~~~^~~~~~~~~~~~~
street_lamps.cpp:179:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%s", qt);
~~~~~^~~~~~~~~~
street_lamps.cpp:182:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d ", &q1[k1].pos);
~~~~~^~~~~~~~~~~~~~~~~~~~
street_lamps.cpp:186:12: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d%d ", &q2[k2].a, &q2[k2].b);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
103672 KB |
Output is correct |
2 |
Correct |
152 ms |
103724 KB |
Output is correct |
3 |
Correct |
148 ms |
103800 KB |
Output is correct |
4 |
Correct |
148 ms |
103780 KB |
Output is correct |
5 |
Correct |
151 ms |
103644 KB |
Output is correct |
6 |
Correct |
171 ms |
103752 KB |
Output is correct |
7 |
Correct |
165 ms |
103800 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
603 ms |
121852 KB |
Output is correct |
2 |
Correct |
733 ms |
124336 KB |
Output is correct |
3 |
Correct |
1184 ms |
131728 KB |
Output is correct |
4 |
Correct |
2243 ms |
156408 KB |
Output is correct |
5 |
Correct |
2360 ms |
162000 KB |
Output is correct |
6 |
Correct |
1663 ms |
147792 KB |
Output is correct |
7 |
Correct |
3255 ms |
185936 KB |
Output is correct |
8 |
Correct |
3274 ms |
188368 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
164 ms |
103652 KB |
Output is correct |
2 |
Correct |
148 ms |
103800 KB |
Output is correct |
3 |
Correct |
150 ms |
103856 KB |
Output is correct |
4 |
Correct |
151 ms |
103920 KB |
Output is correct |
5 |
Correct |
689 ms |
114936 KB |
Output is correct |
6 |
Correct |
1519 ms |
140476 KB |
Output is correct |
7 |
Correct |
2269 ms |
159512 KB |
Output is correct |
8 |
Correct |
3188 ms |
183704 KB |
Output is correct |
9 |
Correct |
720 ms |
127328 KB |
Output is correct |
10 |
Correct |
837 ms |
129152 KB |
Output is correct |
11 |
Correct |
791 ms |
130000 KB |
Output is correct |
12 |
Correct |
3197 ms |
181184 KB |
Output is correct |
13 |
Correct |
3365 ms |
183980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
139 ms |
103892 KB |
Output is correct |
2 |
Correct |
148 ms |
103928 KB |
Output is correct |
3 |
Correct |
148 ms |
103816 KB |
Output is correct |
4 |
Correct |
176 ms |
103796 KB |
Output is correct |
5 |
Correct |
3155 ms |
181588 KB |
Output is correct |
6 |
Correct |
2492 ms |
165192 KB |
Output is correct |
7 |
Correct |
1640 ms |
146960 KB |
Output is correct |
8 |
Correct |
677 ms |
114908 KB |
Output is correct |
9 |
Correct |
599 ms |
117220 KB |
Output is correct |
10 |
Correct |
351 ms |
109304 KB |
Output is correct |
11 |
Correct |
614 ms |
117096 KB |
Output is correct |
12 |
Correct |
345 ms |
109524 KB |
Output is correct |
13 |
Correct |
582 ms |
117172 KB |
Output is correct |
14 |
Correct |
344 ms |
109432 KB |
Output is correct |
15 |
Correct |
3205 ms |
181184 KB |
Output is correct |
16 |
Correct |
3192 ms |
183640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
103672 KB |
Output is correct |
2 |
Correct |
152 ms |
103724 KB |
Output is correct |
3 |
Correct |
148 ms |
103800 KB |
Output is correct |
4 |
Correct |
148 ms |
103780 KB |
Output is correct |
5 |
Correct |
151 ms |
103644 KB |
Output is correct |
6 |
Correct |
171 ms |
103752 KB |
Output is correct |
7 |
Correct |
165 ms |
103800 KB |
Output is correct |
8 |
Correct |
603 ms |
121852 KB |
Output is correct |
9 |
Correct |
733 ms |
124336 KB |
Output is correct |
10 |
Correct |
1184 ms |
131728 KB |
Output is correct |
11 |
Correct |
2243 ms |
156408 KB |
Output is correct |
12 |
Correct |
2360 ms |
162000 KB |
Output is correct |
13 |
Correct |
1663 ms |
147792 KB |
Output is correct |
14 |
Correct |
3255 ms |
185936 KB |
Output is correct |
15 |
Correct |
3274 ms |
188368 KB |
Output is correct |
16 |
Correct |
164 ms |
103652 KB |
Output is correct |
17 |
Correct |
148 ms |
103800 KB |
Output is correct |
18 |
Correct |
150 ms |
103856 KB |
Output is correct |
19 |
Correct |
151 ms |
103920 KB |
Output is correct |
20 |
Correct |
689 ms |
114936 KB |
Output is correct |
21 |
Correct |
1519 ms |
140476 KB |
Output is correct |
22 |
Correct |
2269 ms |
159512 KB |
Output is correct |
23 |
Correct |
3188 ms |
183704 KB |
Output is correct |
24 |
Correct |
720 ms |
127328 KB |
Output is correct |
25 |
Correct |
837 ms |
129152 KB |
Output is correct |
26 |
Correct |
791 ms |
130000 KB |
Output is correct |
27 |
Correct |
3197 ms |
181184 KB |
Output is correct |
28 |
Correct |
3365 ms |
183980 KB |
Output is correct |
29 |
Correct |
139 ms |
103892 KB |
Output is correct |
30 |
Correct |
148 ms |
103928 KB |
Output is correct |
31 |
Correct |
148 ms |
103816 KB |
Output is correct |
32 |
Correct |
176 ms |
103796 KB |
Output is correct |
33 |
Correct |
3155 ms |
181588 KB |
Output is correct |
34 |
Correct |
2492 ms |
165192 KB |
Output is correct |
35 |
Correct |
1640 ms |
146960 KB |
Output is correct |
36 |
Correct |
677 ms |
114908 KB |
Output is correct |
37 |
Correct |
599 ms |
117220 KB |
Output is correct |
38 |
Correct |
351 ms |
109304 KB |
Output is correct |
39 |
Correct |
614 ms |
117096 KB |
Output is correct |
40 |
Correct |
345 ms |
109524 KB |
Output is correct |
41 |
Correct |
582 ms |
117172 KB |
Output is correct |
42 |
Correct |
344 ms |
109432 KB |
Output is correct |
43 |
Correct |
3205 ms |
181184 KB |
Output is correct |
44 |
Correct |
3192 ms |
183640 KB |
Output is correct |
45 |
Correct |
306 ms |
112280 KB |
Output is correct |
46 |
Correct |
499 ms |
115136 KB |
Output is correct |
47 |
Correct |
1106 ms |
130800 KB |
Output is correct |
48 |
Correct |
1952 ms |
153352 KB |
Output is correct |
49 |
Correct |
894 ms |
132316 KB |
Output is correct |
50 |
Correct |
875 ms |
132216 KB |
Output is correct |
51 |
Correct |
886 ms |
133456 KB |
Output is correct |
52 |
Correct |
875 ms |
139088 KB |
Output is correct |
53 |
Correct |
873 ms |
139220 KB |
Output is correct |
54 |
Correct |
868 ms |
138964 KB |
Output is correct |