#include <bits/stdc++.h>
using namespace std;
#define vi vector<int>
#define all(X) begin(X), end(X)
const int Z = 3e5+1;
struct CompoundBIT {
int n;
vector<vi> a, b;
vi sum, sz;
CompoundBIT(int N) : n(N), a(N), b(N), sum(N), sz(N) {}
void addVal(int i, int v) {
for(++i; i < n; i += i&-i) a[i].push_back(v);
}
void build() {
for(int i = 1; i < n; i++) {
sort(all(a[i]));
b[i].resize(1+(sz[i] = size(a[i])));
}
}
void update(int i, int v, int w) {
for(++i; i < n; i += i&-i) {
sum[i] += w;
for(int j = lower_bound(all(a[i]), v) - begin(a[i]) + 1; j <= sz[i]; j += j&-j)
b[i][j] += w;
}
}
int query(int i, int v) {
int x = 0;
for(++i; i > 0; i -= i&-i) {
x += sum[i];
for(int j = lower_bound(all(a[i]), v) - begin(a[i]); j > 0; j -= j & -j)
x -= b[i][j];
}
return x;
}
} cb(Z);
int N, Q;
string _inp;
int qL[Z], qR[Z];
bitset<Z> qType, state;
set<array<int, 2>> intervals;
vector<array<int, 3>> iAt[Z];
array<int, 2> iFound[Z];
map<int, int> last[Z];
void addInterval(int l, int r, int i) {
if(r < l) return;
cb.addVal(l, r);
intervals.insert({l, r});
iAt[i].push_back({l, r, 1});
}
void remInterval(int l, int r, int i) {
if(r < l) return;
intervals.erase({l, r});
iAt[i].push_back({l, r, 0});
}
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> N >> Q;
cin >> _inp;
for(int i = 0; i < N; ++i)
state[i] = _inp[i] - '0';
for(int i = 0; i < N; ++i) if(state[i]) {
int l = i;
while(i < N && state[i]) ++i;
addInterval(l, i-1, 0);
}
for(int i = 1; i <= Q; ++i) {
cin >> _inp >> qL[i]; --qL[i];
if((qType[i] = _inp[0] == 'q')) {
cin >> qR[i];
qR[i] -= 2;
auto f = intervals.upper_bound({qL[i], Z});
if(!empty(intervals) && f != begin(intervals))
iFound[i] = *--f;
else
iFound[i][0] = Z;
} else {
auto f = intervals.upper_bound({qL[i], Z});
int v = qL[i];
if(state[v]) {
auto [l, r] = *--f;
remInterval(l, r, i);
addInterval(l, v-1, i);
addInterval(v+1, r, i);
} else {
int l = v, r = v;
if(v + 1 < N && state[v+1]) r = (*f)[1];
if(v + 0 > 0 && state[v-1]) l = (*--f)[0];
remInterval(v+1, r, i);
remInterval(l, v-1, i);
addInterval(l, r, i);
}
state[v] = !state[v];
}
}
cb.build();
for(int i = 0; i <= Q; ++i) {
for(auto &[l, r, toAdd] : iAt[i]) {
if(toAdd) last[l][r] = i;
else cb.update(l, r, i - last[l][r]);
}
if(qType[i]) {
int res = cb.query(qL[i], qR[i]);
auto [l, r] = iFound[i];
if(l <= qL[i] && qR[i] <= r)
res += i - last[l][r];
cout << res << '\n';
}
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
47276 KB |
Output is correct |
2 |
Correct |
37 ms |
47268 KB |
Output is correct |
3 |
Correct |
35 ms |
47316 KB |
Output is correct |
4 |
Correct |
34 ms |
47212 KB |
Output is correct |
5 |
Correct |
35 ms |
47308 KB |
Output is correct |
6 |
Correct |
34 ms |
47236 KB |
Output is correct |
7 |
Correct |
33 ms |
47308 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
757 ms |
77812 KB |
Output is correct |
2 |
Correct |
799 ms |
77080 KB |
Output is correct |
3 |
Correct |
883 ms |
76588 KB |
Output is correct |
4 |
Correct |
1141 ms |
94712 KB |
Output is correct |
5 |
Correct |
1156 ms |
89164 KB |
Output is correct |
6 |
Correct |
1378 ms |
96588 KB |
Output is correct |
7 |
Correct |
161 ms |
52964 KB |
Output is correct |
8 |
Correct |
168 ms |
54312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
47428 KB |
Output is correct |
2 |
Correct |
45 ms |
47408 KB |
Output is correct |
3 |
Correct |
49 ms |
47500 KB |
Output is correct |
4 |
Correct |
37 ms |
47344 KB |
Output is correct |
5 |
Correct |
1662 ms |
107636 KB |
Output is correct |
6 |
Correct |
1542 ms |
101012 KB |
Output is correct |
7 |
Correct |
1046 ms |
88392 KB |
Output is correct |
8 |
Correct |
151 ms |
54332 KB |
Output is correct |
9 |
Correct |
107 ms |
51992 KB |
Output is correct |
10 |
Correct |
140 ms |
52396 KB |
Output is correct |
11 |
Correct |
106 ms |
52472 KB |
Output is correct |
12 |
Correct |
143 ms |
52900 KB |
Output is correct |
13 |
Correct |
153 ms |
54364 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
47376 KB |
Output is correct |
2 |
Correct |
36 ms |
47392 KB |
Output is correct |
3 |
Correct |
37 ms |
47428 KB |
Output is correct |
4 |
Correct |
44 ms |
47508 KB |
Output is correct |
5 |
Correct |
453 ms |
70924 KB |
Output is correct |
6 |
Correct |
856 ms |
84860 KB |
Output is correct |
7 |
Correct |
1340 ms |
96212 KB |
Output is correct |
8 |
Correct |
1650 ms |
115320 KB |
Output is correct |
9 |
Correct |
1008 ms |
85604 KB |
Output is correct |
10 |
Correct |
1063 ms |
97364 KB |
Output is correct |
11 |
Correct |
960 ms |
85668 KB |
Output is correct |
12 |
Correct |
1149 ms |
97476 KB |
Output is correct |
13 |
Correct |
1076 ms |
85732 KB |
Output is correct |
14 |
Correct |
1285 ms |
97268 KB |
Output is correct |
15 |
Correct |
173 ms |
52964 KB |
Output is correct |
16 |
Correct |
160 ms |
54272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
47276 KB |
Output is correct |
2 |
Correct |
37 ms |
47268 KB |
Output is correct |
3 |
Correct |
35 ms |
47316 KB |
Output is correct |
4 |
Correct |
34 ms |
47212 KB |
Output is correct |
5 |
Correct |
35 ms |
47308 KB |
Output is correct |
6 |
Correct |
34 ms |
47236 KB |
Output is correct |
7 |
Correct |
33 ms |
47308 KB |
Output is correct |
8 |
Correct |
757 ms |
77812 KB |
Output is correct |
9 |
Correct |
799 ms |
77080 KB |
Output is correct |
10 |
Correct |
883 ms |
76588 KB |
Output is correct |
11 |
Correct |
1141 ms |
94712 KB |
Output is correct |
12 |
Correct |
1156 ms |
89164 KB |
Output is correct |
13 |
Correct |
1378 ms |
96588 KB |
Output is correct |
14 |
Correct |
161 ms |
52964 KB |
Output is correct |
15 |
Correct |
168 ms |
54312 KB |
Output is correct |
16 |
Correct |
37 ms |
47428 KB |
Output is correct |
17 |
Correct |
45 ms |
47408 KB |
Output is correct |
18 |
Correct |
49 ms |
47500 KB |
Output is correct |
19 |
Correct |
37 ms |
47344 KB |
Output is correct |
20 |
Correct |
1662 ms |
107636 KB |
Output is correct |
21 |
Correct |
1542 ms |
101012 KB |
Output is correct |
22 |
Correct |
1046 ms |
88392 KB |
Output is correct |
23 |
Correct |
151 ms |
54332 KB |
Output is correct |
24 |
Correct |
107 ms |
51992 KB |
Output is correct |
25 |
Correct |
140 ms |
52396 KB |
Output is correct |
26 |
Correct |
106 ms |
52472 KB |
Output is correct |
27 |
Correct |
143 ms |
52900 KB |
Output is correct |
28 |
Correct |
153 ms |
54364 KB |
Output is correct |
29 |
Correct |
37 ms |
47376 KB |
Output is correct |
30 |
Correct |
36 ms |
47392 KB |
Output is correct |
31 |
Correct |
37 ms |
47428 KB |
Output is correct |
32 |
Correct |
44 ms |
47508 KB |
Output is correct |
33 |
Correct |
453 ms |
70924 KB |
Output is correct |
34 |
Correct |
856 ms |
84860 KB |
Output is correct |
35 |
Correct |
1340 ms |
96212 KB |
Output is correct |
36 |
Correct |
1650 ms |
115320 KB |
Output is correct |
37 |
Correct |
1008 ms |
85604 KB |
Output is correct |
38 |
Correct |
1063 ms |
97364 KB |
Output is correct |
39 |
Correct |
960 ms |
85668 KB |
Output is correct |
40 |
Correct |
1149 ms |
97476 KB |
Output is correct |
41 |
Correct |
1076 ms |
85732 KB |
Output is correct |
42 |
Correct |
1285 ms |
97268 KB |
Output is correct |
43 |
Correct |
173 ms |
52964 KB |
Output is correct |
44 |
Correct |
160 ms |
54272 KB |
Output is correct |
45 |
Correct |
307 ms |
68308 KB |
Output is correct |
46 |
Correct |
376 ms |
67268 KB |
Output is correct |
47 |
Correct |
638 ms |
73876 KB |
Output is correct |
48 |
Correct |
1045 ms |
94340 KB |
Output is correct |
49 |
Correct |
117 ms |
52644 KB |
Output is correct |
50 |
Correct |
119 ms |
52660 KB |
Output is correct |
51 |
Correct |
117 ms |
52680 KB |
Output is correct |
52 |
Correct |
125 ms |
52588 KB |
Output is correct |
53 |
Correct |
133 ms |
52820 KB |
Output is correct |
54 |
Correct |
119 ms |
52676 KB |
Output is correct |