// Zende bad Shoma nasime faghat !
#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define debug(x) cerr << #x << " : " << x << '\n'
using namespace std;
typedef string str;
typedef pair<int, int> pii;
const int N = 3e5 + 10;
int n, q;
str s;
typedef pair<int, pii> Event;
vector<Event> E;
set<pii> st;
pii Find(int idx){
return *(--st.upper_bound({idx, n + n}));
}
vector< pair<int, pii> > F[N];
int Timer = 0;
void Add2(int i, int j, int d){
for(; i < N; i += i & (-i))
F[i].pb({Timer, {(d == 1 ? -2 : -1), j}});
}
void Ask2(int i, int j, int id){
for(; i; i -= (i & (-i)))
F[i].pb({Timer, {id, j}});
}
int fen[N], sh[N], ans[N];
void Add(int idx, int d){
for(; idx < N; idx += (idx & (-idx))){
if(d == -1) fen[idx] += Timer;
else fen[idx] -= Timer - 1;
sh[idx] += d;
}
}
void Reset(int idx){
for(; idx < N; idx += (idx & (-idx))){
fen[idx] = 0;
sh[idx] = 0;
}
}
int Get(int idx){
int res = 0;
for(; idx; idx -= (idx & (-idx)))
res += fen[idx] + sh[idx] * Timer;
return res;
}
int main(){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> n >> q;
cin >> s; s = "0" + s + "0";
vector<pii> V;
for(int i = 0; i + 1 < n + 2; i++){
cerr << i << ' ' << s[i] << ' ' << s[i + 1] << '\n';
if(s[i] == '0' && s[i + 1] == '1')
V.pb(pii(i + 1, -1));
if(s[i] == '1' && s[i + 1] == '0')
V[V.size() - 1].S = i;
}
for(auto x : V){
E.pb({+1, {x.F, n + 1 - x.S}});
st.insert(x);
}
//E.pb({2, {0, 0}});
str type;
int l, r, L, R;
pii bl;
for(int i = 0; i < q; i++){
cin >> type;
if(type == "query"){
cin >> l >> r; r--;
E.pb({0, {l, n + 1 - r}});
E.pb({2, {0, 0}});
continue;
}
cin >> l;
if(s[l] == '1'){
s[l] = '0';
bl = Find(l);
st.erase(bl);
E.pb({-1, {bl.F, n + 1 - bl.S}});
E.pb({2, {0, 0}});
if(bl.F < l){
st.insert({bl.F, l - 1});
E.pb({+1, {bl.F, n + 1 - (l - 1)}});
}
if(bl.S > l){
st.insert({l + 1, bl.S});
E.pb({+1, {l + 1, n + 1 - bl.S}});
}
//E.pb({2, {0, 0}});
continue;
}
s[l] = '1';
L = l, R = l;
if(s[l - 1] == '1'){
bl = Find(l - 1);
E.pb({-1, {bl.F, n + 1 - bl.S}});
st.erase(bl);
L = bl.F;
}
if(s[l + 1] == '1'){
bl = Find(l + 1);
E.pb({-1, {bl.F, n + 1 - bl.S}});
st.erase(bl);
R = bl.S;
}
E.pb({2, {0, 0}});
E.pb({+1, {L, n + 1 - R}});
st.insert({L, R});
//E.pb({2, {0, 0}});
}
int cnt = 0;
for(auto x : E){
if(x.F == 2){
Timer ++;
continue;
}
if(x.F == 0)
Ask2(x.S.F, x.S.S, cnt ++);
else
Add2(x.S.F, x.S.S, x.F);
}
for(int i = 1; i < N; i++){
for(auto &x : F[i]){
Timer = x.F;
if(x.S.F >= 0){
ans[x.S.F] += Get(x.S.S);
} else {
Add(x.S.S, (x.S.F == -2 ? 1 : -1));
}
}
//if(!F[i].empty()){
// memset(fen, 0, sizeof fen);
// memset(sh, 0, sizeof sh);
//}
for(auto &x : F[i]){
if(x.S.F < 0)
Reset(x.S.S);
}
}
for(int i = 0; i < cnt; i++) cout << ans[i] << '\n';
return 0;
}
/*
5 2
11111
toggle 3
query 1 6
query 1 2
query 1 2
query 1 6
query 3 4
toggle 3
query 3 4
query 1 6
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7552 KB |
Output is correct |
2 |
Correct |
6 ms |
7552 KB |
Output is correct |
3 |
Correct |
6 ms |
7552 KB |
Output is correct |
4 |
Correct |
6 ms |
7552 KB |
Output is correct |
5 |
Correct |
6 ms |
7552 KB |
Output is correct |
6 |
Correct |
6 ms |
7424 KB |
Output is correct |
7 |
Correct |
6 ms |
7424 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
689 ms |
104136 KB |
Output is correct |
2 |
Correct |
650 ms |
105092 KB |
Output is correct |
3 |
Correct |
636 ms |
98588 KB |
Output is correct |
4 |
Correct |
4130 ms |
102740 KB |
Output is correct |
5 |
Correct |
4191 ms |
108968 KB |
Output is correct |
6 |
Correct |
4116 ms |
110976 KB |
Output is correct |
7 |
Correct |
3767 ms |
69632 KB |
Output is correct |
8 |
Correct |
3764 ms |
71440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
7936 KB |
Output is correct |
2 |
Correct |
19 ms |
7936 KB |
Output is correct |
3 |
Correct |
19 ms |
7936 KB |
Output is correct |
4 |
Correct |
17 ms |
7552 KB |
Output is correct |
5 |
Correct |
4407 ms |
123912 KB |
Output is correct |
6 |
Correct |
4346 ms |
129332 KB |
Output is correct |
7 |
Correct |
4335 ms |
114560 KB |
Output is correct |
8 |
Correct |
3862 ms |
67932 KB |
Output is correct |
9 |
Correct |
103 ms |
29020 KB |
Output is correct |
10 |
Correct |
116 ms |
30920 KB |
Output is correct |
11 |
Correct |
119 ms |
31652 KB |
Output is correct |
12 |
Correct |
3954 ms |
66300 KB |
Output is correct |
13 |
Correct |
3820 ms |
67816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
18 ms |
7672 KB |
Output is correct |
2 |
Correct |
19 ms |
7800 KB |
Output is correct |
3 |
Correct |
20 ms |
7936 KB |
Output is correct |
4 |
Correct |
22 ms |
8232 KB |
Output is correct |
5 |
Correct |
3932 ms |
80492 KB |
Output is correct |
6 |
Correct |
4072 ms |
98776 KB |
Output is correct |
7 |
Correct |
4489 ms |
114112 KB |
Output is correct |
8 |
Correct |
4502 ms |
146288 KB |
Output is correct |
9 |
Correct |
849 ms |
117032 KB |
Output is correct |
10 |
Correct |
1155 ms |
185924 KB |
Output is correct |
11 |
Correct |
843 ms |
116844 KB |
Output is correct |
12 |
Correct |
1167 ms |
186736 KB |
Output is correct |
13 |
Correct |
827 ms |
116996 KB |
Output is correct |
14 |
Correct |
1125 ms |
187080 KB |
Output is correct |
15 |
Correct |
3997 ms |
65208 KB |
Output is correct |
16 |
Correct |
3871 ms |
66620 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
6 ms |
7552 KB |
Output is correct |
2 |
Correct |
6 ms |
7552 KB |
Output is correct |
3 |
Correct |
6 ms |
7552 KB |
Output is correct |
4 |
Correct |
6 ms |
7552 KB |
Output is correct |
5 |
Correct |
6 ms |
7552 KB |
Output is correct |
6 |
Correct |
6 ms |
7424 KB |
Output is correct |
7 |
Correct |
6 ms |
7424 KB |
Output is correct |
8 |
Correct |
689 ms |
104136 KB |
Output is correct |
9 |
Correct |
650 ms |
105092 KB |
Output is correct |
10 |
Correct |
636 ms |
98588 KB |
Output is correct |
11 |
Correct |
4130 ms |
102740 KB |
Output is correct |
12 |
Correct |
4191 ms |
108968 KB |
Output is correct |
13 |
Correct |
4116 ms |
110976 KB |
Output is correct |
14 |
Correct |
3767 ms |
69632 KB |
Output is correct |
15 |
Correct |
3764 ms |
71440 KB |
Output is correct |
16 |
Correct |
20 ms |
7936 KB |
Output is correct |
17 |
Correct |
19 ms |
7936 KB |
Output is correct |
18 |
Correct |
19 ms |
7936 KB |
Output is correct |
19 |
Correct |
17 ms |
7552 KB |
Output is correct |
20 |
Correct |
4407 ms |
123912 KB |
Output is correct |
21 |
Correct |
4346 ms |
129332 KB |
Output is correct |
22 |
Correct |
4335 ms |
114560 KB |
Output is correct |
23 |
Correct |
3862 ms |
67932 KB |
Output is correct |
24 |
Correct |
103 ms |
29020 KB |
Output is correct |
25 |
Correct |
116 ms |
30920 KB |
Output is correct |
26 |
Correct |
119 ms |
31652 KB |
Output is correct |
27 |
Correct |
3954 ms |
66300 KB |
Output is correct |
28 |
Correct |
3820 ms |
67816 KB |
Output is correct |
29 |
Correct |
18 ms |
7672 KB |
Output is correct |
30 |
Correct |
19 ms |
7800 KB |
Output is correct |
31 |
Correct |
20 ms |
7936 KB |
Output is correct |
32 |
Correct |
22 ms |
8232 KB |
Output is correct |
33 |
Correct |
3932 ms |
80492 KB |
Output is correct |
34 |
Correct |
4072 ms |
98776 KB |
Output is correct |
35 |
Correct |
4489 ms |
114112 KB |
Output is correct |
36 |
Correct |
4502 ms |
146288 KB |
Output is correct |
37 |
Correct |
849 ms |
117032 KB |
Output is correct |
38 |
Correct |
1155 ms |
185924 KB |
Output is correct |
39 |
Correct |
843 ms |
116844 KB |
Output is correct |
40 |
Correct |
1167 ms |
186736 KB |
Output is correct |
41 |
Correct |
827 ms |
116996 KB |
Output is correct |
42 |
Correct |
1125 ms |
187080 KB |
Output is correct |
43 |
Correct |
3997 ms |
65208 KB |
Output is correct |
44 |
Correct |
3871 ms |
66620 KB |
Output is correct |
45 |
Correct |
500 ms |
67648 KB |
Output is correct |
46 |
Correct |
446 ms |
67416 KB |
Output is correct |
47 |
Correct |
1585 ms |
69000 KB |
Output is correct |
48 |
Correct |
4284 ms |
110020 KB |
Output is correct |
49 |
Correct |
121 ms |
33612 KB |
Output is correct |
50 |
Correct |
123 ms |
32844 KB |
Output is correct |
51 |
Correct |
150 ms |
34120 KB |
Output is correct |
52 |
Correct |
145 ms |
38216 KB |
Output is correct |
53 |
Correct |
154 ms |
38220 KB |
Output is correct |
54 |
Correct |
161 ms |
38344 KB |
Output is correct |