#include<bits/stdc++.h>
using namespace std;
#define X first
#define Y second
#define pb emplace_back
#define sz(x) x.size()
#define all(x) x.begin(),x.end()
#define ops tuple<int,int,int>
const int N = 3e5 + 5;
typedef pair<int,int> ii;
namespace BIT2D {
vector<int> val[N];
vector<int> bit[N];
void ini() {
for(int i = 1 ; i < N ; ++i) {
sort(all(val[i]));
val[i].resize(unique(all(val[i])) - val[i].begin());
bit[i].resize(sz(val[i]) + 5);
}
}
void add(int x,int y) {
for(; x < N ; x += x & -x)
val[x].push_back(y);
}
void upd(int x,int y,int v) {
for(; x < N ; x += x & -x) {
int p = lower_bound(all(val[x]),y) - val[x].begin() + 1;
for(; p > 0 ; p -= p & -p)
bit[x][p] += v;
}
}
int get(int x,int y) {
int ans = 0;
for(; x > 0 ; x -= x & -x) {
int p = lower_bound(all(val[x]),y) - val[x].begin() + 1;
int K = sz(bit[x]);
for(; p < K ; p += p & -p)
ans += bit[x][p];
}
return ans;
}
}
using BIT2D::upd;
using BIT2D::get;
bool on[N];
set<ii> S;
vector<ops> Q[N];
int l[N];
int r[N];
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int n; cin >> n;
int q; cin >> q; ++q;
for(int i = 1 ; i <= n ; ++i) {
char c; cin >> c;
if (c == '1') {
if (on[i - 1]) {
ii seg = (*--S.end());
S.erase (seg); seg.Y = i;
S.insert(seg);
}
else
S.insert(ii(i,i));
on[i] = 1;
}
}
for(ii e : S) Q[1].pb(1,e.X,e.Y);
string t;
for(int i = 2 ; i <= q ; ++i) {
int x, y; cin >> t >> x;
if (t == "query") {
cin >> y;
l[i] = x;
r[i] = y - 1;
continue;
}
if (on[x]) {
auto it = S.upper_bound(ii(x,n));
ii range = (*--it);
int L = range.X;
int R = range.Y;
if (L < x) S.insert(ii(L,x - 1)), Q[i].pb(1,L,x - 1);
if (R > x) S.insert(ii(x + 1,R)), Q[i].pb(1,x + 1,R);
S.erase(range); Q[i].pb(0,L,R);
}
else {
int l = x;
int r = x;
if (on[x + 1]) {
auto it = S.upper_bound(ii(x,x));
r = (*it).Y;
Q[i].pb(0,x + 1,r);
S.erase(it);
}
if (on[x - 1]) {
auto it = S.upper_bound(ii(x,x)); --it;
l = (*it).X;
Q[i].pb(0,l,x - 1);
S.erase(it);
}
S.insert({l,r});
Q[i].pb(1,l,r);
}
on[x] ^= 1;
}
for(int i = 1 ; i <= q ; ++i)
for(ops _ : Q[i]) {
int v, x, y;
tie(v, x, y) = _;
BIT2D::add(x,y);
}
BIT2D::ini(); Q[1].pb(1,N,0);
for(int i = 1 ; i <= q ; ++i) {
if (Q[i].empty()) {
int ans = get(l[i],r[i]);
if (ans < 0)
ans += i;
cout << ans << "\n";
}
for(ops _ : Q[i]) {
int v, x, y;
tie(v, x, y) = _;
if (v) upd(x,y,-i);
else upd(x,y, i);
}
}
}
/*
5 7
11011
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 1 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
30968 KB |
Output is correct |
2 |
Correct |
45 ms |
30840 KB |
Output is correct |
3 |
Correct |
46 ms |
30840 KB |
Output is correct |
4 |
Correct |
48 ms |
30840 KB |
Output is correct |
5 |
Correct |
45 ms |
30840 KB |
Output is correct |
6 |
Correct |
49 ms |
30840 KB |
Output is correct |
7 |
Correct |
45 ms |
30812 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
686 ms |
62696 KB |
Output is correct |
2 |
Correct |
738 ms |
61880 KB |
Output is correct |
3 |
Correct |
892 ms |
61500 KB |
Output is correct |
4 |
Correct |
1450 ms |
78680 KB |
Output is correct |
5 |
Correct |
1397 ms |
74440 KB |
Output is correct |
6 |
Correct |
1756 ms |
80740 KB |
Output is correct |
7 |
Correct |
310 ms |
39800 KB |
Output is correct |
8 |
Correct |
312 ms |
41464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
31160 KB |
Output is correct |
2 |
Correct |
48 ms |
31096 KB |
Output is correct |
3 |
Correct |
48 ms |
31068 KB |
Output is correct |
4 |
Correct |
45 ms |
30840 KB |
Output is correct |
5 |
Correct |
2094 ms |
86304 KB |
Output is correct |
6 |
Correct |
1863 ms |
78752 KB |
Output is correct |
7 |
Correct |
1393 ms |
68892 KB |
Output is correct |
8 |
Correct |
320 ms |
36216 KB |
Output is correct |
9 |
Correct |
136 ms |
34296 KB |
Output is correct |
10 |
Correct |
150 ms |
34524 KB |
Output is correct |
11 |
Correct |
146 ms |
34680 KB |
Output is correct |
12 |
Correct |
294 ms |
34424 KB |
Output is correct |
13 |
Correct |
305 ms |
36084 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
47 ms |
30968 KB |
Output is correct |
2 |
Correct |
50 ms |
31096 KB |
Output is correct |
3 |
Correct |
51 ms |
31096 KB |
Output is correct |
4 |
Correct |
57 ms |
31224 KB |
Output is correct |
5 |
Correct |
581 ms |
55332 KB |
Output is correct |
6 |
Correct |
1059 ms |
66784 KB |
Output is correct |
7 |
Correct |
1546 ms |
80072 KB |
Output is correct |
8 |
Correct |
2300 ms |
99656 KB |
Output is correct |
9 |
Correct |
951 ms |
72528 KB |
Output is correct |
10 |
Correct |
1095 ms |
83632 KB |
Output is correct |
11 |
Correct |
948 ms |
72468 KB |
Output is correct |
12 |
Correct |
1122 ms |
83552 KB |
Output is correct |
13 |
Correct |
968 ms |
72536 KB |
Output is correct |
14 |
Correct |
1133 ms |
83320 KB |
Output is correct |
15 |
Correct |
300 ms |
39732 KB |
Output is correct |
16 |
Correct |
320 ms |
41524 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
30968 KB |
Output is correct |
2 |
Correct |
45 ms |
30840 KB |
Output is correct |
3 |
Correct |
46 ms |
30840 KB |
Output is correct |
4 |
Correct |
48 ms |
30840 KB |
Output is correct |
5 |
Correct |
45 ms |
30840 KB |
Output is correct |
6 |
Correct |
49 ms |
30840 KB |
Output is correct |
7 |
Correct |
45 ms |
30812 KB |
Output is correct |
8 |
Correct |
686 ms |
62696 KB |
Output is correct |
9 |
Correct |
738 ms |
61880 KB |
Output is correct |
10 |
Correct |
892 ms |
61500 KB |
Output is correct |
11 |
Correct |
1450 ms |
78680 KB |
Output is correct |
12 |
Correct |
1397 ms |
74440 KB |
Output is correct |
13 |
Correct |
1756 ms |
80740 KB |
Output is correct |
14 |
Correct |
310 ms |
39800 KB |
Output is correct |
15 |
Correct |
312 ms |
41464 KB |
Output is correct |
16 |
Correct |
53 ms |
31160 KB |
Output is correct |
17 |
Correct |
48 ms |
31096 KB |
Output is correct |
18 |
Correct |
48 ms |
31068 KB |
Output is correct |
19 |
Correct |
45 ms |
30840 KB |
Output is correct |
20 |
Correct |
2094 ms |
86304 KB |
Output is correct |
21 |
Correct |
1863 ms |
78752 KB |
Output is correct |
22 |
Correct |
1393 ms |
68892 KB |
Output is correct |
23 |
Correct |
320 ms |
36216 KB |
Output is correct |
24 |
Correct |
136 ms |
34296 KB |
Output is correct |
25 |
Correct |
150 ms |
34524 KB |
Output is correct |
26 |
Correct |
146 ms |
34680 KB |
Output is correct |
27 |
Correct |
294 ms |
34424 KB |
Output is correct |
28 |
Correct |
305 ms |
36084 KB |
Output is correct |
29 |
Correct |
47 ms |
30968 KB |
Output is correct |
30 |
Correct |
50 ms |
31096 KB |
Output is correct |
31 |
Correct |
51 ms |
31096 KB |
Output is correct |
32 |
Correct |
57 ms |
31224 KB |
Output is correct |
33 |
Correct |
581 ms |
55332 KB |
Output is correct |
34 |
Correct |
1059 ms |
66784 KB |
Output is correct |
35 |
Correct |
1546 ms |
80072 KB |
Output is correct |
36 |
Correct |
2300 ms |
99656 KB |
Output is correct |
37 |
Correct |
951 ms |
72528 KB |
Output is correct |
38 |
Correct |
1095 ms |
83632 KB |
Output is correct |
39 |
Correct |
948 ms |
72468 KB |
Output is correct |
40 |
Correct |
1122 ms |
83552 KB |
Output is correct |
41 |
Correct |
968 ms |
72536 KB |
Output is correct |
42 |
Correct |
1133 ms |
83320 KB |
Output is correct |
43 |
Correct |
300 ms |
39732 KB |
Output is correct |
44 |
Correct |
320 ms |
41524 KB |
Output is correct |
45 |
Correct |
363 ms |
52408 KB |
Output is correct |
46 |
Correct |
450 ms |
51496 KB |
Output is correct |
47 |
Correct |
897 ms |
58256 KB |
Output is correct |
48 |
Correct |
1539 ms |
78392 KB |
Output is correct |
49 |
Correct |
154 ms |
37368 KB |
Output is correct |
50 |
Correct |
157 ms |
37448 KB |
Output is correct |
51 |
Correct |
155 ms |
37624 KB |
Output is correct |
52 |
Correct |
161 ms |
37880 KB |
Output is correct |
53 |
Correct |
159 ms |
37920 KB |
Output is correct |
54 |
Correct |
162 ms |
37880 KB |
Output is correct |