#include<bits/stdc++.h>
using namespace std;
const int MAXN = 300005;
const int SIZ = 120;
int n, q, ofs = 1, cnt;
string s;
map <int, int> mp, rst;
set <int> st;
set <int> :: iterator it;
int t[MAXN * SIZ], lef[MAXN * SIZ], rig[MAXN * SIZ];
struct tournament {
int root;
tournament () {
root = ++cnt;
}
void update (int x, int pos, int lo, int hi, int val) {
if (lo == hi) {
t[x] += val;
return;
}
int mid = (lo + hi) / 2;
if (pos <= mid) {
if (lef[x] == 0) lef[x] = ++cnt;
update(lef[x], pos, lo, mid, val);
} else {
if (rig[x] == 0) rig[x] = ++cnt;
update(rig[x], pos, mid+1, hi, val);
}
t[x] = t[lef[x]] + t[rig[x]];
}
int upit (int x, int from, int to, int lo, int hi) {
if (x == 0) return 0;
if (from <= lo && hi <= to) return t[x];
if (to < lo || hi < from) return 0;
return upit(lef[x], from, to, lo, (lo + hi)/2) + upit(rig[x], from, to, (lo + hi)/2+1, hi);
}
} fen[MAXN];
void ubaci (int x, int y, int val) {
for (; x < MAXN; x += x&-x) {
fen[x].update(fen[x].root, y, 0, ofs-1, val);
}
}
int upit (int x, int y) {
int res = 0;
for (; x > 0; x -= x&-x) {
res += fen[x].upit(fen[x].root, y, ofs-1, 0, ofs-1);
}
return res;
}
void dodaj (int a, int b, int t) {
st.insert(a);
mp[a] = t;
rst[a] = b;
}
void brisi (int a, int b, int t) {
ubaci(a, b, t - mp[a]);
st.erase(a);
mp.erase(a);
rst.erase(a);
}
void precompute () {
int lim = -1;
for (int i=1; i<=n; i++) {
if (s[i] == '1' && (i == 1 || s[i-1] == '0')) lim = i;
if (s[i] == '1' && (i == n || s[i+1] == '0')) {
dodaj(lim, i, 0);
}
}
}
void ispis () {
cout << "ispis" << endl;
for (auto x : st) {
cout << "bla " << x << " " << rig[x] << " " << mp[x] << endl;
}
}
int main () {
ios_base::sync_with_stdio(false);
cin.tie(0);
cin >> n >> q >> s;
s = '.' + s;
while (ofs < n+1) ofs *= 2;
precompute();
for (int i=1; i<=q; i++) {
string t;
cin >> t;
if (t == "query") {
int a, b;
cin >> a >> b; b--;
int res = upit(a, b);
it = st.upper_bound(a);
if (it != st.begin()) {
it--;
if (b <= rst[*it]) {
res += i - mp[*it];
}
}
cout << res << '\n';
} else {
int x;
cin >> x;
if (s[x] == '0') {
s[x] = '1';
int ll = x, rr = x;
it = st.upper_bound(x);
if (it != st.end() && *it == x+1) {
rr = rst[*it];
brisi(*it, rst[*it], i);
}
it = st.upper_bound(x);
if (it != st.begin()) {
it--;
if (rst[*it] == x-1) {
ll = *it;
brisi(*it, rst[*it], i);
}
}
dodaj(ll, rr, i);
} else {
s[x] = '0';
it = st.upper_bound(x); it--;
int ll = *it;
int rr = rst[*it];
brisi(ll, rr, i);
if (ll <= x-1) dodaj(ll, x-1, i);
if (x+1 <= rr) dodaj(x+1, rr, i);
}
}
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1656 KB |
Output is correct |
2 |
Correct |
3 ms |
1656 KB |
Output is correct |
3 |
Correct |
3 ms |
1656 KB |
Output is correct |
4 |
Correct |
3 ms |
1656 KB |
Output is correct |
5 |
Correct |
4 ms |
1656 KB |
Output is correct |
6 |
Correct |
3 ms |
1528 KB |
Output is correct |
7 |
Correct |
3 ms |
1528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
371 ms |
2660 KB |
Output is correct |
2 |
Correct |
450 ms |
3196 KB |
Output is correct |
3 |
Correct |
743 ms |
5880 KB |
Output is correct |
4 |
Correct |
1973 ms |
83168 KB |
Output is correct |
5 |
Correct |
2021 ms |
93672 KB |
Output is correct |
6 |
Correct |
2113 ms |
88828 KB |
Output is correct |
7 |
Correct |
167 ms |
2616 KB |
Output is correct |
8 |
Correct |
189 ms |
3912 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
2040 KB |
Output is correct |
2 |
Correct |
6 ms |
2040 KB |
Output is correct |
3 |
Correct |
5 ms |
2040 KB |
Output is correct |
4 |
Correct |
3 ms |
1532 KB |
Output is correct |
5 |
Correct |
2494 ms |
117484 KB |
Output is correct |
6 |
Correct |
2384 ms |
112352 KB |
Output is correct |
7 |
Correct |
1727 ms |
92836 KB |
Output is correct |
8 |
Correct |
174 ms |
4064 KB |
Output is correct |
9 |
Correct |
103 ms |
2552 KB |
Output is correct |
10 |
Correct |
112 ms |
2576 KB |
Output is correct |
11 |
Correct |
116 ms |
2672 KB |
Output is correct |
12 |
Correct |
161 ms |
2692 KB |
Output is correct |
13 |
Correct |
172 ms |
3940 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
1784 KB |
Output is correct |
2 |
Correct |
5 ms |
1912 KB |
Output is correct |
3 |
Correct |
5 ms |
1912 KB |
Output is correct |
4 |
Correct |
6 ms |
1912 KB |
Output is correct |
5 |
Correct |
685 ms |
19244 KB |
Output is correct |
6 |
Correct |
1276 ms |
66404 KB |
Output is correct |
7 |
Correct |
1904 ms |
88600 KB |
Output is correct |
8 |
Correct |
2634 ms |
106276 KB |
Output is correct |
9 |
Correct |
506 ms |
5624 KB |
Output is correct |
10 |
Correct |
486 ms |
4728 KB |
Output is correct |
11 |
Correct |
508 ms |
5764 KB |
Output is correct |
12 |
Correct |
482 ms |
4600 KB |
Output is correct |
13 |
Correct |
528 ms |
5552 KB |
Output is correct |
14 |
Correct |
485 ms |
4728 KB |
Output is correct |
15 |
Correct |
162 ms |
8420 KB |
Output is correct |
16 |
Correct |
173 ms |
9828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
1656 KB |
Output is correct |
2 |
Correct |
3 ms |
1656 KB |
Output is correct |
3 |
Correct |
3 ms |
1656 KB |
Output is correct |
4 |
Correct |
3 ms |
1656 KB |
Output is correct |
5 |
Correct |
4 ms |
1656 KB |
Output is correct |
6 |
Correct |
3 ms |
1528 KB |
Output is correct |
7 |
Correct |
3 ms |
1528 KB |
Output is correct |
8 |
Correct |
371 ms |
2660 KB |
Output is correct |
9 |
Correct |
450 ms |
3196 KB |
Output is correct |
10 |
Correct |
743 ms |
5880 KB |
Output is correct |
11 |
Correct |
1973 ms |
83168 KB |
Output is correct |
12 |
Correct |
2021 ms |
93672 KB |
Output is correct |
13 |
Correct |
2113 ms |
88828 KB |
Output is correct |
14 |
Correct |
167 ms |
2616 KB |
Output is correct |
15 |
Correct |
189 ms |
3912 KB |
Output is correct |
16 |
Correct |
6 ms |
2040 KB |
Output is correct |
17 |
Correct |
6 ms |
2040 KB |
Output is correct |
18 |
Correct |
5 ms |
2040 KB |
Output is correct |
19 |
Correct |
3 ms |
1532 KB |
Output is correct |
20 |
Correct |
2494 ms |
117484 KB |
Output is correct |
21 |
Correct |
2384 ms |
112352 KB |
Output is correct |
22 |
Correct |
1727 ms |
92836 KB |
Output is correct |
23 |
Correct |
174 ms |
4064 KB |
Output is correct |
24 |
Correct |
103 ms |
2552 KB |
Output is correct |
25 |
Correct |
112 ms |
2576 KB |
Output is correct |
26 |
Correct |
116 ms |
2672 KB |
Output is correct |
27 |
Correct |
161 ms |
2692 KB |
Output is correct |
28 |
Correct |
172 ms |
3940 KB |
Output is correct |
29 |
Correct |
4 ms |
1784 KB |
Output is correct |
30 |
Correct |
5 ms |
1912 KB |
Output is correct |
31 |
Correct |
5 ms |
1912 KB |
Output is correct |
32 |
Correct |
6 ms |
1912 KB |
Output is correct |
33 |
Correct |
685 ms |
19244 KB |
Output is correct |
34 |
Correct |
1276 ms |
66404 KB |
Output is correct |
35 |
Correct |
1904 ms |
88600 KB |
Output is correct |
36 |
Correct |
2634 ms |
106276 KB |
Output is correct |
37 |
Correct |
506 ms |
5624 KB |
Output is correct |
38 |
Correct |
486 ms |
4728 KB |
Output is correct |
39 |
Correct |
508 ms |
5764 KB |
Output is correct |
40 |
Correct |
482 ms |
4600 KB |
Output is correct |
41 |
Correct |
528 ms |
5552 KB |
Output is correct |
42 |
Correct |
485 ms |
4728 KB |
Output is correct |
43 |
Correct |
162 ms |
8420 KB |
Output is correct |
44 |
Correct |
173 ms |
9828 KB |
Output is correct |
45 |
Correct |
130 ms |
4156 KB |
Output is correct |
46 |
Correct |
229 ms |
4188 KB |
Output is correct |
47 |
Correct |
988 ms |
39984 KB |
Output is correct |
48 |
Correct |
1800 ms |
87520 KB |
Output is correct |
49 |
Correct |
121 ms |
5796 KB |
Output is correct |
50 |
Correct |
126 ms |
5752 KB |
Output is correct |
51 |
Correct |
131 ms |
6052 KB |
Output is correct |
52 |
Correct |
123 ms |
6308 KB |
Output is correct |
53 |
Correct |
123 ms |
6216 KB |
Output is correct |
54 |
Correct |
130 ms |
6236 KB |
Output is correct |