#include<bits/stdc++.h>
using namespace std;
using ll = long long;
using pll = pair<ll, ll>;
using pii = pair<int,int>;
#define X first
#define Y second
#define SZ(x) int(x.size())
#define pb push_back
#define Mp make_pair
#define mid ((l+r)>>1)
mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
const int MXN = 3e5+5;
const int LOG = 20;
ll fen[MXN];
vector<pii> fen_his;
inline void updx(int p, int x, bool add=1) {
if(add) fen_his.pb(Mp(p, x));
for(p+=2; p<MXN; p+=p&-p) fen[p] += x;
}
inline ll getx(int p) {
ll res=0;
for(p+=2; p; p-=p&-p) res += fen[p];
return res;
}
inline ll getr(int s, int e) { return getx(e)-getx(s-1); }
inline void roll() {
while(SZ(fen_his)) {
updx(fen_his.back().X, -fen_his.back().Y, 0);
fen_his.pop_back();
}
}
bool isq[MXN];
int n, q, ans[MXN];
string s;
vector<array<int, 4>> segs;
void dcq(int l, int r) {
if(l>=r) return;
dcq(l, mid);
dcq(mid+1, r);
int p0=l, p1=mid+1;
vector<array<int, 4>> sorted;
while(p0<=mid && p1<=r)
if(segs[p0][2]<=segs[p1][2]) {
sorted.pb(segs[p0++]);
if(sorted.back()[0] == 0) updx(sorted.back()[3], sorted.back()[1]);
}
else {
sorted.pb(segs[p1++]);
if(sorted.back()[0] == 1)
ans[sorted.back()[1]] += getr(sorted.back()[3], MXN-3);
}
while(p0<=mid) {
sorted.pb(segs[p0++]);
if(sorted.back()[0] == 0) updx(sorted.back()[3], sorted.back()[1]);
}
while(p1<=r) {
sorted.pb(segs[p1++]);
if(sorted.back()[0] == 1)
ans[sorted.back()[1]] += getr(sorted.back()[3], MXN-3);
}
assert(SZ(sorted) == r-l+1);
for(int i=l; i<=r; i++) segs[i] = sorted[i-l];
roll();
}
int32_t main() {
cin.tie(0); cout.tie(0); ios_base::sync_with_stdio(0);
cin >> n >> q >> s;
s = '0' + s + '0';
n += 2;
set<int> st;
for(int l=0,r=0; l<n; l=r)
if(s[l]=='0') {
st.insert(l);
r++;
}
else {
while(r<n && s[r] == '1') r++;
segs.pb({0, -1, l, r-1});
}
for(int i=2; i<=q+1; i++) {
string t;
cin >> t;
if(t == "query") {
isq[i] = 1;
int l, r;
cin >> l >> r;
segs.pb({1, i, l, r-1});
}
else {
int p;
cin >> p;
if(s[p] == '0') {
s[p] = '1';
st.erase(p);
int nxt = *st.lower_bound(p), pre = *--st.lower_bound(p);
segs.pb({0, i, pre+1, p-1});
segs.pb({0, i, p+1, nxt-1});
segs.pb({0, -i, pre+1, nxt-1});
}
else {
int nxt = *st.lower_bound(p), pre = *--st.lower_bound(p);
segs.pb({0, i, pre+1, nxt-1});
segs.pb({0, -i, pre+1, p-1});
segs.pb({0, -i, p+1, nxt-1});
s[p] = '0';
st.insert(p);
}
}
}
dcq(0, SZ(segs)-1);
for(int i=2; i<=q+1; i++)
if(isq[i])
cout << ans[i] + (ans[i]<0)*i << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
616 ms |
40832 KB |
Output is correct |
2 |
Correct |
594 ms |
43688 KB |
Output is correct |
3 |
Correct |
539 ms |
44968 KB |
Output is correct |
4 |
Correct |
599 ms |
57460 KB |
Output is correct |
5 |
Correct |
520 ms |
49520 KB |
Output is correct |
6 |
Correct |
593 ms |
57712 KB |
Output is correct |
7 |
Correct |
273 ms |
39632 KB |
Output is correct |
8 |
Correct |
226 ms |
26668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
2648 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
742 ms |
68264 KB |
Output is correct |
6 |
Correct |
626 ms |
55892 KB |
Output is correct |
7 |
Correct |
509 ms |
49524 KB |
Output is correct |
8 |
Correct |
232 ms |
25268 KB |
Output is correct |
9 |
Correct |
104 ms |
16080 KB |
Output is correct |
10 |
Correct |
128 ms |
24088 KB |
Output is correct |
11 |
Correct |
127 ms |
22304 KB |
Output is correct |
12 |
Correct |
288 ms |
39912 KB |
Output is correct |
13 |
Correct |
240 ms |
26464 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
2648 KB |
Output is correct |
2 |
Correct |
1 ms |
2652 KB |
Output is correct |
3 |
Correct |
2 ms |
2652 KB |
Output is correct |
4 |
Correct |
2 ms |
2652 KB |
Output is correct |
5 |
Correct |
294 ms |
39116 KB |
Output is correct |
6 |
Correct |
458 ms |
52848 KB |
Output is correct |
7 |
Correct |
628 ms |
56688 KB |
Output is correct |
8 |
Correct |
788 ms |
63712 KB |
Output is correct |
9 |
Correct |
734 ms |
48612 KB |
Output is correct |
10 |
Correct |
909 ms |
54120 KB |
Output is correct |
11 |
Correct |
736 ms |
48380 KB |
Output is correct |
12 |
Correct |
908 ms |
53420 KB |
Output is correct |
13 |
Correct |
743 ms |
46276 KB |
Output is correct |
14 |
Correct |
903 ms |
51884 KB |
Output is correct |
15 |
Correct |
295 ms |
38864 KB |
Output is correct |
16 |
Correct |
247 ms |
25712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
0 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
616 ms |
40832 KB |
Output is correct |
9 |
Correct |
594 ms |
43688 KB |
Output is correct |
10 |
Correct |
539 ms |
44968 KB |
Output is correct |
11 |
Correct |
599 ms |
57460 KB |
Output is correct |
12 |
Correct |
520 ms |
49520 KB |
Output is correct |
13 |
Correct |
593 ms |
57712 KB |
Output is correct |
14 |
Correct |
273 ms |
39632 KB |
Output is correct |
15 |
Correct |
226 ms |
26668 KB |
Output is correct |
16 |
Correct |
3 ms |
2648 KB |
Output is correct |
17 |
Correct |
2 ms |
2652 KB |
Output is correct |
18 |
Correct |
2 ms |
2652 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
742 ms |
68264 KB |
Output is correct |
21 |
Correct |
626 ms |
55892 KB |
Output is correct |
22 |
Correct |
509 ms |
49524 KB |
Output is correct |
23 |
Correct |
232 ms |
25268 KB |
Output is correct |
24 |
Correct |
104 ms |
16080 KB |
Output is correct |
25 |
Correct |
128 ms |
24088 KB |
Output is correct |
26 |
Correct |
127 ms |
22304 KB |
Output is correct |
27 |
Correct |
288 ms |
39912 KB |
Output is correct |
28 |
Correct |
240 ms |
26464 KB |
Output is correct |
29 |
Correct |
2 ms |
2648 KB |
Output is correct |
30 |
Correct |
1 ms |
2652 KB |
Output is correct |
31 |
Correct |
2 ms |
2652 KB |
Output is correct |
32 |
Correct |
2 ms |
2652 KB |
Output is correct |
33 |
Correct |
294 ms |
39116 KB |
Output is correct |
34 |
Correct |
458 ms |
52848 KB |
Output is correct |
35 |
Correct |
628 ms |
56688 KB |
Output is correct |
36 |
Correct |
788 ms |
63712 KB |
Output is correct |
37 |
Correct |
734 ms |
48612 KB |
Output is correct |
38 |
Correct |
909 ms |
54120 KB |
Output is correct |
39 |
Correct |
736 ms |
48380 KB |
Output is correct |
40 |
Correct |
908 ms |
53420 KB |
Output is correct |
41 |
Correct |
743 ms |
46276 KB |
Output is correct |
42 |
Correct |
903 ms |
51884 KB |
Output is correct |
43 |
Correct |
295 ms |
38864 KB |
Output is correct |
44 |
Correct |
247 ms |
25712 KB |
Output is correct |
45 |
Correct |
379 ms |
26520 KB |
Output is correct |
46 |
Correct |
413 ms |
28600 KB |
Output is correct |
47 |
Correct |
356 ms |
30384 KB |
Output is correct |
48 |
Correct |
616 ms |
54128 KB |
Output is correct |
49 |
Correct |
193 ms |
24084 KB |
Output is correct |
50 |
Correct |
151 ms |
23480 KB |
Output is correct |
51 |
Correct |
150 ms |
24412 KB |
Output is correct |
52 |
Correct |
149 ms |
23636 KB |
Output is correct |
53 |
Correct |
159 ms |
22980 KB |
Output is correct |
54 |
Correct |
149 ms |
24636 KB |
Output is correct |