/*input
5 7
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 1 6
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <map>
using namespace std;
const int MAXN = 300007;
struct Event
{
int id, x, l, r, val;
Event(int _id = 0, int _x = 0, int _l = 0, int _r = 0, int _val = 0): id(_id), x(_x), l(_l), r(_r), val(_val) {};
bool operator< (const Event &e) const { return (x == e.x ? id < e.id : x < e.x); }
} events[MAXN * 2], tmp[MAXN * 2];
int N, M;
string S;
map<int, int> segs;
int ans[MAXN];
int bit[MAXN];
void add(int l, int r, int val)
{
for (; l <= N + 1; l += l & (-l)) bit[l] += val;
for (++r; r <= N + 1; r += r & (-r)) bit[r] -= val;
}
int get(int i)
{
int val = 0;
for (; i > 0; i -= i & (-i)) val += bit[i];
return val;
}
void solve(int l, int r)
{
if (l == r) return;
int m = (l + r) >> 1;
solve(l, m), solve(m + 1, r);
for (int i = l, j = m + 1, k = 0; i <= m || j <= r; ++k) {
if (i <= m && (j > r || events[i] < events[j])) {
tmp[k] = events[i];
if (events[i].id == 0) add(events[i].l, events[i].r, events[i].val);
++i;
} else {
tmp[k] = events[j];
if (events[j].id == 1) ans[events[j].val] += get(events[j].r);
++j;
}
}
for (int i = l; i <= m; ++i) if (events[i].id == 0) {
add(events[i].l, events[i].r, -events[i].val);
}
copy(tmp, tmp + (r - l + 1), events + l);
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> N >> M >> S;
int last = 1;
for (int i = 1; i <= N; ++i) {
if (S[i - 1] == '0') segs[last] = i, last = i + 1;
}
segs[last] = N + 1;
int numEvents = 0, numQueries = 0;
for (int i = 1; i <= M; ++i) {
string type;
cin >> type;
if (type == "toggle") {
int pos;
cin >> pos;
auto it = prev(segs.upper_bound(pos));
if (it->second == pos) { // merge
int l = it -> first, r = next(it) -> second;
events[numEvents++] = Event(0, l, pos + 1, r, -i);
events[numEvents++] = Event(0, pos + 1, pos + 1, r, i);
segs.erase(it);
segs.erase(segs.upper_bound(pos));
segs[l] = r;
} else { // split
int l = it -> first, r = it -> second;
events[numEvents++] = Event(0, l, pos + 1, r, i);
events[numEvents++] = Event(0, pos + 1, pos + 1, r, -i);
segs.erase(it);
segs[l] = pos;
segs[pos + 1] = r;
}
} else {
int l, r;
cin >> l >> r;
events[numEvents++] = Event(1, l, 0, r, ++numQueries);
auto it = prev(segs.upper_bound(l));
if (it->second >= r) ans[numQueries] += i;
}
}
solve(0, numEvents - 1);
for (int i = 1; i <= numQueries; ++i) cout << ans[i] << '\n';
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
18 ms |
23808 KB |
Output is correct |
3 |
Correct |
18 ms |
23808 KB |
Output is correct |
4 |
Correct |
18 ms |
23808 KB |
Output is correct |
5 |
Correct |
18 ms |
23808 KB |
Output is correct |
6 |
Correct |
20 ms |
23808 KB |
Output is correct |
7 |
Correct |
18 ms |
23680 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
370 ms |
28600 KB |
Output is correct |
2 |
Correct |
406 ms |
28920 KB |
Output is correct |
3 |
Correct |
503 ms |
29560 KB |
Output is correct |
4 |
Correct |
763 ms |
38792 KB |
Output is correct |
5 |
Correct |
654 ms |
37896 KB |
Output is correct |
6 |
Correct |
796 ms |
38540 KB |
Output is correct |
7 |
Correct |
541 ms |
46088 KB |
Output is correct |
8 |
Correct |
253 ms |
33416 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
20 ms |
23936 KB |
Output is correct |
2 |
Correct |
19 ms |
23936 KB |
Output is correct |
3 |
Correct |
18 ms |
23808 KB |
Output is correct |
4 |
Correct |
18 ms |
23808 KB |
Output is correct |
5 |
Correct |
1076 ms |
43660 KB |
Output is correct |
6 |
Correct |
878 ms |
40588 KB |
Output is correct |
7 |
Correct |
661 ms |
37336 KB |
Output is correct |
8 |
Correct |
257 ms |
33420 KB |
Output is correct |
9 |
Correct |
157 ms |
28528 KB |
Output is correct |
10 |
Correct |
144 ms |
28792 KB |
Output is correct |
11 |
Correct |
147 ms |
29048 KB |
Output is correct |
12 |
Correct |
544 ms |
45980 KB |
Output is correct |
13 |
Correct |
271 ms |
33420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
18 ms |
23936 KB |
Output is correct |
2 |
Correct |
18 ms |
23808 KB |
Output is correct |
3 |
Correct |
19 ms |
23936 KB |
Output is correct |
4 |
Correct |
19 ms |
23808 KB |
Output is correct |
5 |
Correct |
416 ms |
40076 KB |
Output is correct |
6 |
Correct |
602 ms |
39052 KB |
Output is correct |
7 |
Correct |
836 ms |
38248 KB |
Output is correct |
8 |
Correct |
1046 ms |
36748 KB |
Output is correct |
9 |
Correct |
487 ms |
27640 KB |
Output is correct |
10 |
Correct |
466 ms |
26872 KB |
Output is correct |
11 |
Correct |
459 ms |
27700 KB |
Output is correct |
12 |
Correct |
466 ms |
27156 KB |
Output is correct |
13 |
Correct |
446 ms |
27740 KB |
Output is correct |
14 |
Correct |
462 ms |
26744 KB |
Output is correct |
15 |
Correct |
526 ms |
46092 KB |
Output is correct |
16 |
Correct |
254 ms |
33420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
17 ms |
23808 KB |
Output is correct |
2 |
Correct |
18 ms |
23808 KB |
Output is correct |
3 |
Correct |
18 ms |
23808 KB |
Output is correct |
4 |
Correct |
18 ms |
23808 KB |
Output is correct |
5 |
Correct |
18 ms |
23808 KB |
Output is correct |
6 |
Correct |
20 ms |
23808 KB |
Output is correct |
7 |
Correct |
18 ms |
23680 KB |
Output is correct |
8 |
Correct |
370 ms |
28600 KB |
Output is correct |
9 |
Correct |
406 ms |
28920 KB |
Output is correct |
10 |
Correct |
503 ms |
29560 KB |
Output is correct |
11 |
Correct |
763 ms |
38792 KB |
Output is correct |
12 |
Correct |
654 ms |
37896 KB |
Output is correct |
13 |
Correct |
796 ms |
38540 KB |
Output is correct |
14 |
Correct |
541 ms |
46088 KB |
Output is correct |
15 |
Correct |
253 ms |
33416 KB |
Output is correct |
16 |
Correct |
20 ms |
23936 KB |
Output is correct |
17 |
Correct |
19 ms |
23936 KB |
Output is correct |
18 |
Correct |
18 ms |
23808 KB |
Output is correct |
19 |
Correct |
18 ms |
23808 KB |
Output is correct |
20 |
Correct |
1076 ms |
43660 KB |
Output is correct |
21 |
Correct |
878 ms |
40588 KB |
Output is correct |
22 |
Correct |
661 ms |
37336 KB |
Output is correct |
23 |
Correct |
257 ms |
33420 KB |
Output is correct |
24 |
Correct |
157 ms |
28528 KB |
Output is correct |
25 |
Correct |
144 ms |
28792 KB |
Output is correct |
26 |
Correct |
147 ms |
29048 KB |
Output is correct |
27 |
Correct |
544 ms |
45980 KB |
Output is correct |
28 |
Correct |
271 ms |
33420 KB |
Output is correct |
29 |
Correct |
18 ms |
23936 KB |
Output is correct |
30 |
Correct |
18 ms |
23808 KB |
Output is correct |
31 |
Correct |
19 ms |
23936 KB |
Output is correct |
32 |
Correct |
19 ms |
23808 KB |
Output is correct |
33 |
Correct |
416 ms |
40076 KB |
Output is correct |
34 |
Correct |
602 ms |
39052 KB |
Output is correct |
35 |
Correct |
836 ms |
38248 KB |
Output is correct |
36 |
Correct |
1046 ms |
36748 KB |
Output is correct |
37 |
Correct |
487 ms |
27640 KB |
Output is correct |
38 |
Correct |
466 ms |
26872 KB |
Output is correct |
39 |
Correct |
459 ms |
27700 KB |
Output is correct |
40 |
Correct |
466 ms |
27156 KB |
Output is correct |
41 |
Correct |
446 ms |
27740 KB |
Output is correct |
42 |
Correct |
462 ms |
26744 KB |
Output is correct |
43 |
Correct |
526 ms |
46092 KB |
Output is correct |
44 |
Correct |
254 ms |
33420 KB |
Output is correct |
45 |
Correct |
204 ms |
26720 KB |
Output is correct |
46 |
Correct |
248 ms |
26616 KB |
Output is correct |
47 |
Correct |
425 ms |
30456 KB |
Output is correct |
48 |
Correct |
759 ms |
38408 KB |
Output is correct |
49 |
Correct |
158 ms |
29176 KB |
Output is correct |
50 |
Correct |
157 ms |
29048 KB |
Output is correct |
51 |
Correct |
174 ms |
29304 KB |
Output is correct |
52 |
Correct |
167 ms |
29564 KB |
Output is correct |
53 |
Correct |
168 ms |
29560 KB |
Output is correct |
54 |
Correct |
173 ms |
29560 KB |
Output is correct |