#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#define ll long long
using namespace std;
struct event{
int t; int x; int y; ll value;
};
bool cmpt(event a, event b){return a.t<b.t;}
bool cmpx(event a, event b){if(a.x!=b.x) return a.x<b.x; else return a.t<b.t;}
int last[300500],first[300500],toggle[300500];
ll ans[300500];
ll fw1[300500], fw2[300500];
set<int> S;
event EA[300500][4],EB[1005000], EA0[1005000];
int Asize[300500],EA0size;
void cdq(int l, int r, int n){
int mid = (l+r)/2;
if(l==r) return;
cdq(l, mid, n);
cdq(mid+1, r, n);
int EBS = 0;
for(int i=l; i<=mid; i++){
if(i==0) {
for(int j=0; j<EA0size; j++){
if(EA0[j].value!=0) EB[EBS++] = EA0[j];
}
}
else{
for(int j=0; j<Asize[i]; j++){
if(EA[i][j].value!=0) EB[EBS++] = EA[i][j];
}
}
}
for(int i=mid+1; i<=r; i++){
for(int j=0; j<Asize[i]; j++){
if(EA[i][j].value==0) EB[EBS++] = EA[i][j];
}
}
sort(EB, EB+EBS, cmpx);
for(int i=0; i<EBS; i++){
if(EB[i].t<=mid){
int x = n+2-EB[i].y;
while(x < n+2) {
fw1[x] += EB[i].value*EB[i].t;
fw2[x] += EB[i].value;
x += (x & -x);
}
}
if(EB[i].t>mid) {
int x = n+2-EB[i].y;
while (x > 0) {
ans[EB[i].t] += fw2[x]*EB[i].t;
ans[EB[i].t] -= fw1[x];
x -= (x & -x);
}
}
}
for(int i=0; i<EBS; i++){
if(EB[i].t<=mid){
int x = n+2-EB[i].y;
while(x < n+2) {
fw1[x] -= EB[i].value*EB[i].t;
fw2[x] -= EB[i].value;
x += (x & -x);
}
}
}
}
int main() {
cin.sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
int n, m;
cin>>n>>m;
string s;
cin>>s;
last[n+1] = n+1;
first[1] = 1;
for(int i=n+1; i>=2; i--) last[i-1] = (s[i-2]=='1')?last[i]:(i-1);
for(int i=2; i<=n+1; i++) first[i] = (s[i-2]=='1')?first[i-1]:i;
S.insert(n+1);
for(int i=1; i<=n; i++) if(s[i-1]=='0') S.insert(i);
for(int i=1; i<=n+1; i++){
if(last[i]==i){
event e = {0, first[i], i, 1};
if(first[i]!=i) EA0[EA0size++] = e;
}
}
for(int i=1; i<=m; i++){
string v;
cin>>v;
if(v=="query"){
int x, y;
cin>>x>>y;
event e = {i, x, y, 0};
EA[i][Asize[i]++] = e;
toggle[i] = 1;
}
else{
int a;
event e1, e2, e3;
cin>>a;
if(s[a-1]=='0'){
s[a-1]='1';
e1 = {i, a+1, last[a+1], -1};
e2 = {i, first[a], last[a+1], 1};
e3 = {i, first[a], a, -1};
last[first[a]] = last[a+1];
first[last[a+1]] = first[a];
auto k = S.find(a);
S.erase(k);
}
else{
s[a-1] = '0';
int x1, x2;
x2 = *S.lower_bound(a+1);
x1 = first[x2];
e1 = {i, x1, x2, -1};
e2 = {i, x1, a, 1};
e3 = {i, a+1, x2, 1};
last[x1] = a;
last[a+1] = x2;
first[a] = x1;
first[x2] = a+1;
S.insert(a);
}
if(e1.x!=e1.y) EA[i][Asize[i]++] = e1;
if(e2.x!=e2.y) EA[i][Asize[i]++] = e2;
if(e3.x!=e3.y) EA[i][Asize[i]++] = e3;
}
}
cdq(0, m, n);
for(int i=1; i<=m; i++){
if(toggle[i]==1) cout<<ans[i]<<"\n";
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
723 ms |
39524 KB |
Output is correct |
2 |
Correct |
742 ms |
39640 KB |
Output is correct |
3 |
Correct |
785 ms |
39916 KB |
Output is correct |
4 |
Correct |
1392 ms |
57344 KB |
Output is correct |
5 |
Correct |
1335 ms |
56208 KB |
Output is correct |
6 |
Correct |
1478 ms |
59648 KB |
Output is correct |
7 |
Correct |
551 ms |
54144 KB |
Output is correct |
8 |
Correct |
487 ms |
41600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
620 KB |
Output is correct |
2 |
Correct |
3 ms |
620 KB |
Output is correct |
3 |
Correct |
3 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
1392 ms |
60112 KB |
Output is correct |
6 |
Correct |
1556 ms |
59520 KB |
Output is correct |
7 |
Correct |
1372 ms |
55552 KB |
Output is correct |
8 |
Correct |
480 ms |
41728 KB |
Output is correct |
9 |
Correct |
260 ms |
30728 KB |
Output is correct |
10 |
Correct |
297 ms |
33772 KB |
Output is correct |
11 |
Correct |
300 ms |
33516 KB |
Output is correct |
12 |
Correct |
551 ms |
54272 KB |
Output is correct |
13 |
Correct |
479 ms |
41728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
620 KB |
Output is correct |
2 |
Correct |
3 ms |
620 KB |
Output is correct |
3 |
Correct |
3 ms |
620 KB |
Output is correct |
4 |
Correct |
3 ms |
620 KB |
Output is correct |
5 |
Correct |
923 ms |
55552 KB |
Output is correct |
6 |
Correct |
1222 ms |
58496 KB |
Output is correct |
7 |
Correct |
1489 ms |
59264 KB |
Output is correct |
8 |
Correct |
1737 ms |
54880 KB |
Output is correct |
9 |
Correct |
854 ms |
39916 KB |
Output is correct |
10 |
Correct |
909 ms |
37228 KB |
Output is correct |
11 |
Correct |
850 ms |
39828 KB |
Output is correct |
12 |
Correct |
890 ms |
36984 KB |
Output is correct |
13 |
Correct |
847 ms |
39916 KB |
Output is correct |
14 |
Correct |
892 ms |
36972 KB |
Output is correct |
15 |
Correct |
554 ms |
54144 KB |
Output is correct |
16 |
Correct |
489 ms |
41728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
2 ms |
364 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
364 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
364 KB |
Output is correct |
8 |
Correct |
723 ms |
39524 KB |
Output is correct |
9 |
Correct |
742 ms |
39640 KB |
Output is correct |
10 |
Correct |
785 ms |
39916 KB |
Output is correct |
11 |
Correct |
1392 ms |
57344 KB |
Output is correct |
12 |
Correct |
1335 ms |
56208 KB |
Output is correct |
13 |
Correct |
1478 ms |
59648 KB |
Output is correct |
14 |
Correct |
551 ms |
54144 KB |
Output is correct |
15 |
Correct |
487 ms |
41600 KB |
Output is correct |
16 |
Correct |
3 ms |
620 KB |
Output is correct |
17 |
Correct |
3 ms |
620 KB |
Output is correct |
18 |
Correct |
3 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
1392 ms |
60112 KB |
Output is correct |
21 |
Correct |
1556 ms |
59520 KB |
Output is correct |
22 |
Correct |
1372 ms |
55552 KB |
Output is correct |
23 |
Correct |
480 ms |
41728 KB |
Output is correct |
24 |
Correct |
260 ms |
30728 KB |
Output is correct |
25 |
Correct |
297 ms |
33772 KB |
Output is correct |
26 |
Correct |
300 ms |
33516 KB |
Output is correct |
27 |
Correct |
551 ms |
54272 KB |
Output is correct |
28 |
Correct |
479 ms |
41728 KB |
Output is correct |
29 |
Correct |
2 ms |
620 KB |
Output is correct |
30 |
Correct |
3 ms |
620 KB |
Output is correct |
31 |
Correct |
3 ms |
620 KB |
Output is correct |
32 |
Correct |
3 ms |
620 KB |
Output is correct |
33 |
Correct |
923 ms |
55552 KB |
Output is correct |
34 |
Correct |
1222 ms |
58496 KB |
Output is correct |
35 |
Correct |
1489 ms |
59264 KB |
Output is correct |
36 |
Correct |
1737 ms |
54880 KB |
Output is correct |
37 |
Correct |
854 ms |
39916 KB |
Output is correct |
38 |
Correct |
909 ms |
37228 KB |
Output is correct |
39 |
Correct |
850 ms |
39828 KB |
Output is correct |
40 |
Correct |
890 ms |
36984 KB |
Output is correct |
41 |
Correct |
847 ms |
39916 KB |
Output is correct |
42 |
Correct |
892 ms |
36972 KB |
Output is correct |
43 |
Correct |
554 ms |
54144 KB |
Output is correct |
44 |
Correct |
489 ms |
41728 KB |
Output is correct |
45 |
Correct |
427 ms |
26188 KB |
Output is correct |
46 |
Correct |
462 ms |
26160 KB |
Output is correct |
47 |
Correct |
690 ms |
32236 KB |
Output is correct |
48 |
Correct |
1577 ms |
56848 KB |
Output is correct |
49 |
Correct |
371 ms |
37484 KB |
Output is correct |
50 |
Correct |
345 ms |
37484 KB |
Output is correct |
51 |
Correct |
457 ms |
37484 KB |
Output is correct |
52 |
Correct |
354 ms |
37484 KB |
Output is correct |
53 |
Correct |
350 ms |
37484 KB |
Output is correct |
54 |
Correct |
347 ms |
37476 KB |
Output is correct |