#include <bits/stdc++.h>
using namespace std;
#define rep(i, a, b) for (auto i = (a); i < (b); ++i)
const int N = 1 << 19;
struct E { bool ask; int l, r, t, del; } a[3 * N], b[3 * N];
int q, ans[N];
void cdq2(int l, int r) {
if (r - l == 1) return;
int m = (l + r) >> 1;
cdq2(l, m); cdq2(m, r);
rep(i, l, r) b[i].r = i < m;
inplace_merge(b + l, b + m, b + r, [&](E a, E b) { return a.t < b.t; });
int s = 0;
rep(i, l, r) {
if (!b[i].ask && b[i].l && b[i].r) s += b[i].del * b[i].t;
if (b[i].ask && !b[i].l && !b[i].r) ans[b[i].t] += s;
}
}
void cdq1(int l, int r) {
if (r - l == 1) return;
int m = (l + r) >> 1;
cdq1(l, m); cdq1(m, r);
rep(i, l, r) a[i].l = i < m;
merge(a + l, a + m, a + m, a + r, b + l, [&](E a, E b) { return pair(-a.r, a.ask) < pair(-b.r, b.ask); });
rep(i, l, r) a[i] = b[i];
cdq2(l, r);
}
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n, p = 0; cin >> n >> q; int pre = -1;
set<int> z = {-1};
rep(i, 0, n) {
char c; cin >> c;
if (c != '0') {
if (p && a[p - 1].r == i) a[p - 1].r = i + 1;
else a[p++] = {0, pre + 1, i + 1, -1, -1};
} else z.insert(pre = i);
}
z.insert(n);
rep(t, 0, q) {
string s; cin >> s;
if (s == "toggle") {
int i; cin >> i; --i;
auto [it, f] = z.insert(i);
int d = f ? +1 : -1;
a[p++] = {0, *prev(it) + 1, *next(it), t, +d};
a[p++] = {0, *prev(it) + 1, i, t, -d};
a[p++] = {0, i + 1, *next(it), t, -d};
if (d < 0) z.erase(it);
ans[t] = -1;
} else {
int l, r; cin >> l >> r; --l; --r;
a[p++] = {1, l, r, t, 0};
ans[t] = *z.lower_bound(l) < r ? 0 : t;
}
}
sort(a, a + p, [&](E a, E b) { return pair(a.l, a.ask) < pair(b.l, b.ask); });
cdq1(0, p);
rep(t, 0, q) if (0 <= ans[t]) cout << ans[t] << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2543 ms |
33792 KB |
Output is correct |
2 |
Correct |
2740 ms |
33804 KB |
Output is correct |
3 |
Correct |
2804 ms |
34080 KB |
Output is correct |
4 |
Correct |
3224 ms |
44876 KB |
Output is correct |
5 |
Correct |
3055 ms |
40228 KB |
Output is correct |
6 |
Correct |
2997 ms |
48108 KB |
Output is correct |
7 |
Correct |
1487 ms |
31760 KB |
Output is correct |
8 |
Correct |
1251 ms |
17772 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
512 KB |
Output is correct |
2 |
Correct |
7 ms |
512 KB |
Output is correct |
3 |
Correct |
6 ms |
512 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
3826 ms |
59348 KB |
Output is correct |
6 |
Correct |
3396 ms |
50020 KB |
Output is correct |
7 |
Correct |
2887 ms |
40084 KB |
Output is correct |
8 |
Correct |
1480 ms |
17748 KB |
Output is correct |
9 |
Correct |
809 ms |
14432 KB |
Output is correct |
10 |
Correct |
899 ms |
15700 KB |
Output is correct |
11 |
Correct |
863 ms |
15592 KB |
Output is correct |
12 |
Correct |
1761 ms |
31764 KB |
Output is correct |
13 |
Correct |
1451 ms |
17688 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
512 KB |
Output is correct |
2 |
Correct |
4 ms |
512 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
6 ms |
512 KB |
Output is correct |
5 |
Correct |
1918 ms |
29148 KB |
Output is correct |
6 |
Correct |
2588 ms |
38548 KB |
Output is correct |
7 |
Correct |
3024 ms |
48216 KB |
Output is correct |
8 |
Correct |
3818 ms |
60880 KB |
Output is correct |
9 |
Correct |
3066 ms |
41408 KB |
Output is correct |
10 |
Correct |
3657 ms |
49460 KB |
Output is correct |
11 |
Correct |
3050 ms |
41748 KB |
Output is correct |
12 |
Correct |
3567 ms |
49608 KB |
Output is correct |
13 |
Correct |
3050 ms |
41496 KB |
Output is correct |
14 |
Correct |
3574 ms |
49480 KB |
Output is correct |
15 |
Correct |
1676 ms |
31892 KB |
Output is correct |
16 |
Correct |
1476 ms |
18000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
2543 ms |
33792 KB |
Output is correct |
9 |
Correct |
2740 ms |
33804 KB |
Output is correct |
10 |
Correct |
2804 ms |
34080 KB |
Output is correct |
11 |
Correct |
3224 ms |
44876 KB |
Output is correct |
12 |
Correct |
3055 ms |
40228 KB |
Output is correct |
13 |
Correct |
2997 ms |
48108 KB |
Output is correct |
14 |
Correct |
1487 ms |
31760 KB |
Output is correct |
15 |
Correct |
1251 ms |
17772 KB |
Output is correct |
16 |
Correct |
8 ms |
512 KB |
Output is correct |
17 |
Correct |
7 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
512 KB |
Output is correct |
19 |
Correct |
3 ms |
384 KB |
Output is correct |
20 |
Correct |
3826 ms |
59348 KB |
Output is correct |
21 |
Correct |
3396 ms |
50020 KB |
Output is correct |
22 |
Correct |
2887 ms |
40084 KB |
Output is correct |
23 |
Correct |
1480 ms |
17748 KB |
Output is correct |
24 |
Correct |
809 ms |
14432 KB |
Output is correct |
25 |
Correct |
899 ms |
15700 KB |
Output is correct |
26 |
Correct |
863 ms |
15592 KB |
Output is correct |
27 |
Correct |
1761 ms |
31764 KB |
Output is correct |
28 |
Correct |
1451 ms |
17688 KB |
Output is correct |
29 |
Correct |
4 ms |
512 KB |
Output is correct |
30 |
Correct |
4 ms |
512 KB |
Output is correct |
31 |
Correct |
5 ms |
512 KB |
Output is correct |
32 |
Correct |
6 ms |
512 KB |
Output is correct |
33 |
Correct |
1918 ms |
29148 KB |
Output is correct |
34 |
Correct |
2588 ms |
38548 KB |
Output is correct |
35 |
Correct |
3024 ms |
48216 KB |
Output is correct |
36 |
Correct |
3818 ms |
60880 KB |
Output is correct |
37 |
Correct |
3066 ms |
41408 KB |
Output is correct |
38 |
Correct |
3657 ms |
49460 KB |
Output is correct |
39 |
Correct |
3050 ms |
41748 KB |
Output is correct |
40 |
Correct |
3567 ms |
49608 KB |
Output is correct |
41 |
Correct |
3050 ms |
41496 KB |
Output is correct |
42 |
Correct |
3574 ms |
49480 KB |
Output is correct |
43 |
Correct |
1676 ms |
31892 KB |
Output is correct |
44 |
Correct |
1476 ms |
18000 KB |
Output is correct |
45 |
Correct |
1442 ms |
22712 KB |
Output is correct |
46 |
Correct |
1600 ms |
22876 KB |
Output is correct |
47 |
Correct |
1711 ms |
26388 KB |
Output is correct |
48 |
Correct |
2901 ms |
45068 KB |
Output is correct |
49 |
Correct |
1031 ms |
17964 KB |
Output is correct |
50 |
Correct |
1077 ms |
17780 KB |
Output is correct |
51 |
Correct |
1004 ms |
17684 KB |
Output is correct |
52 |
Correct |
887 ms |
17704 KB |
Output is correct |
53 |
Correct |
892 ms |
17816 KB |
Output is correct |
54 |
Correct |
897 ms |
17816 KB |
Output is correct |