#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
const int maxn=3e5+9;
vector<int>node[maxn];
vector<long long>bit[maxn];
int n,q;
void fakeupdate(int x,int y){
for(;x<=n;x+=(x-(x&(x-1)))){
node[x].pb(y);
}
}
void fakeget(int x,int y){
for(;x>=1;x-=(x-(x&(x-1)))){
node[x].pb(y);
}
}
void build(){
for1(i,1,n){
sort(all(node[i]));
auto it=unique(all(node[i]));
node[i].resize(distance(node[i].begin(),it));
bit[i].resize(sz(node[i]));
}
}
void update(int x,int y,int val){
for(;x<=n;x+=(x-(x&(x-1)))){
for(int yy=lower_bound(all(node[x]),y)-node[x].begin();yy<sz(node[x]);yy+=(yy-(yy&(yy-1)))){
bit[x][yy]+=val;
}
}
}
long long get(int x,int y){
long long sum=0;
for(;x>=1;x-=(x-(x&(x-1)))){
for(int yy=lower_bound(all(node[x]),y)-node[x].begin();yy>=1;yy-=(yy-(yy&(yy-1)))){
sum+=bit[x][yy];
}
}
return sum;
}
struct line{
int l,r,t;
bool operator <(const line &p)const{
return r<p.r;
}
};
struct query{
int type,x,y;
}b[maxn];
void fakerect(int x1,int y1,int x2,int y2){
fakeget(x1-1,y1-1);
fakeget(x2,y2);
fakeget(x2,y1-1);
fakeget(x1-1,y2);
}
long long getrect(int x1,int y1,int x2,int y2){
return get(x2,y2)-get(x2,y1-1)-get(x1-1,y2)+get(x1-1,y1-1);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>n>>q;
string s;
cin>>s;
s=" "+s;
for1(i,1,n)node[i].pb(0);
multiset<line>nt;
for1(i,1,n){
if (s[i]=='0')continue;
else {
int p=i;
for1(j,i,n){
if (s[j]!=s[i])break;
p=j;
}
nt.insert({i,p,1});
i=p;
}
}
string t=s;
for1(i,1,q){
//cin>>b[i].type;
string quest;
cin>>quest;
if (quest=="query")b[i].type=1;
else b[i].type=2;
if (b[i].type==1){
cin>>b[i].x>>b[i].y;
if (b[i].x>b[i].y)swap(b[i].x,b[i].y);
if (b[i].x==b[i].y)continue;
b[i].y--;
//small than x and large than y
fakerect(1,b[i].y,b[i].x,n);
}
else {
cin>>b[i].x;
if (s[b[i].x]=='1'){
s[b[i].x]='0';
auto it=nt.lower_bound({b[i].x,b[i].x,-1});
line temp=(*it);
fakeupdate(temp.l,temp.r);
nt.erase(it);
if (temp.l<b[i].x){
nt.insert({temp.l,b[i].x-1,i});
}
if (temp.r>b[i].x){
nt.insert({b[i].x+1,temp.r,i});
}
}
else {
s[b[i].x]='1';
auto it=nt.upper_bound({b[i].x,b[i].x,-1});
line nw={b[i].x,b[i].x,i};
if (it!=nt.begin()){
it--;
line temp=(*it);
if (temp.r+1==b[i].x){
nt.erase(it);
fakeupdate(temp.l,temp.r);
nw.l=temp.l;
}
}
it=nt.upper_bound({b[i].x,b[i].x,-1});
if (it!=nt.end()){
line temp=(*it);
if (temp.l==b[i].x+1){
nt.erase(it);
fakeupdate(temp.l,temp.r);
nw.r=temp.r;
}
}
nt.insert(nw);
}
}
}
build();
s=t;
nt.clear();
for1(i,1,n){
if (s[i]=='0')continue;
else {
int p=i;
for1(j,i,n){
if (s[j]!=s[i])break;
p=j;
}
nt.insert({i,p,0});
i=p;
}
}
for1(i,1,q){
if (b[i].type==1){
//cout<<b[i].x<<" "<<b[i].y<<'\n';
if (b[i].x>b[i].y){
cout<<i<<'\n';
continue;
}
//small than x and large than y
long long ans=getrect(1,b[i].y,b[i].x,n);
auto it=nt.lower_bound({b[i].x,b[i].y,-1});
if (it!=nt.end()){
auto temp=(*it);
if (temp.r>=b[i].y&&temp.l<=b[i].x){
ans+=i-temp.t;
//cout<<temp.l<<" "<<temp.r<<" "<<b[i].x<<" "<<b[i].y<<" "<<temp.t<<" "<<ans<<'\n';
}
}
cout<<ans<<'\n';
}
else {
if (s[b[i].x]=='1'){
s[b[i].x]='0';
auto it=nt.lower_bound({b[i].x,b[i].x,-1});
line temp=(*it);
update(temp.l,temp.r,i-temp.t);
nt.erase(it);
if (temp.l<b[i].x){
nt.insert({temp.l,b[i].x-1,i});
}
if (temp.r>b[i].x){
nt.insert({b[i].x+1,temp.r,i});
}
}
else {
s[b[i].x]='1';
auto it=nt.upper_bound({b[i].x,b[i].x,-1});
line nw={b[i].x,b[i].x,i};
if (it!=nt.begin()){
it--;
line temp=(*it);
if (temp.r+1==b[i].x){
nt.erase(it);
update(temp.l,temp.r,i-temp.t);
nw.l=temp.l;
}
}
it=nt.upper_bound({b[i].x,b[i].x,-1});
if (it!=nt.end()){
line temp=(*it);
if (temp.l==b[i].x+1){
nt.erase(it);
update(temp.l,temp.r,i-temp.t);
nw.r=temp.r;
}
}
nt.insert(nw);
}
}
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15708 KB |
Output is correct |
2 |
Correct |
4 ms |
15708 KB |
Output is correct |
3 |
Correct |
4 ms |
15960 KB |
Output is correct |
4 |
Correct |
3 ms |
15708 KB |
Output is correct |
5 |
Correct |
4 ms |
15820 KB |
Output is correct |
6 |
Correct |
4 ms |
15708 KB |
Output is correct |
7 |
Correct |
4 ms |
15708 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
172 ms |
27256 KB |
Output is correct |
2 |
Correct |
217 ms |
30052 KB |
Output is correct |
3 |
Correct |
377 ms |
36416 KB |
Output is correct |
4 |
Correct |
1332 ms |
78752 KB |
Output is correct |
5 |
Correct |
1353 ms |
84732 KB |
Output is correct |
6 |
Correct |
1158 ms |
77916 KB |
Output is correct |
7 |
Correct |
922 ms |
79516 KB |
Output is correct |
8 |
Correct |
953 ms |
81160 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
15964 KB |
Output is correct |
2 |
Correct |
5 ms |
15964 KB |
Output is correct |
3 |
Correct |
4 ms |
15964 KB |
Output is correct |
4 |
Correct |
4 ms |
15868 KB |
Output is correct |
5 |
Correct |
1195 ms |
71856 KB |
Output is correct |
6 |
Correct |
1353 ms |
81184 KB |
Output is correct |
7 |
Correct |
1357 ms |
88528 KB |
Output is correct |
8 |
Correct |
992 ms |
85656 KB |
Output is correct |
9 |
Correct |
122 ms |
27456 KB |
Output is correct |
10 |
Correct |
131 ms |
28184 KB |
Output is correct |
11 |
Correct |
155 ms |
28724 KB |
Output is correct |
12 |
Correct |
989 ms |
84408 KB |
Output is correct |
13 |
Correct |
1010 ms |
85784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
15964 KB |
Output is correct |
2 |
Correct |
5 ms |
15964 KB |
Output is correct |
3 |
Correct |
5 ms |
15964 KB |
Output is correct |
4 |
Correct |
5 ms |
15964 KB |
Output is correct |
5 |
Correct |
1195 ms |
88836 KB |
Output is correct |
6 |
Correct |
1112 ms |
84464 KB |
Output is correct |
7 |
Correct |
1265 ms |
77924 KB |
Output is correct |
8 |
Correct |
1302 ms |
69164 KB |
Output is correct |
9 |
Correct |
234 ms |
28992 KB |
Output is correct |
10 |
Correct |
174 ms |
25144 KB |
Output is correct |
11 |
Correct |
237 ms |
29032 KB |
Output is correct |
12 |
Correct |
176 ms |
25220 KB |
Output is correct |
13 |
Correct |
234 ms |
28756 KB |
Output is correct |
14 |
Correct |
177 ms |
25388 KB |
Output is correct |
15 |
Correct |
957 ms |
84552 KB |
Output is correct |
16 |
Correct |
943 ms |
85964 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
15708 KB |
Output is correct |
2 |
Correct |
4 ms |
15708 KB |
Output is correct |
3 |
Correct |
4 ms |
15960 KB |
Output is correct |
4 |
Correct |
3 ms |
15708 KB |
Output is correct |
5 |
Correct |
4 ms |
15820 KB |
Output is correct |
6 |
Correct |
4 ms |
15708 KB |
Output is correct |
7 |
Correct |
4 ms |
15708 KB |
Output is correct |
8 |
Correct |
172 ms |
27256 KB |
Output is correct |
9 |
Correct |
217 ms |
30052 KB |
Output is correct |
10 |
Correct |
377 ms |
36416 KB |
Output is correct |
11 |
Correct |
1332 ms |
78752 KB |
Output is correct |
12 |
Correct |
1353 ms |
84732 KB |
Output is correct |
13 |
Correct |
1158 ms |
77916 KB |
Output is correct |
14 |
Correct |
922 ms |
79516 KB |
Output is correct |
15 |
Correct |
953 ms |
81160 KB |
Output is correct |
16 |
Correct |
5 ms |
15964 KB |
Output is correct |
17 |
Correct |
5 ms |
15964 KB |
Output is correct |
18 |
Correct |
4 ms |
15964 KB |
Output is correct |
19 |
Correct |
4 ms |
15868 KB |
Output is correct |
20 |
Correct |
1195 ms |
71856 KB |
Output is correct |
21 |
Correct |
1353 ms |
81184 KB |
Output is correct |
22 |
Correct |
1357 ms |
88528 KB |
Output is correct |
23 |
Correct |
992 ms |
85656 KB |
Output is correct |
24 |
Correct |
122 ms |
27456 KB |
Output is correct |
25 |
Correct |
131 ms |
28184 KB |
Output is correct |
26 |
Correct |
155 ms |
28724 KB |
Output is correct |
27 |
Correct |
989 ms |
84408 KB |
Output is correct |
28 |
Correct |
1010 ms |
85784 KB |
Output is correct |
29 |
Correct |
4 ms |
15964 KB |
Output is correct |
30 |
Correct |
5 ms |
15964 KB |
Output is correct |
31 |
Correct |
5 ms |
15964 KB |
Output is correct |
32 |
Correct |
5 ms |
15964 KB |
Output is correct |
33 |
Correct |
1195 ms |
88836 KB |
Output is correct |
34 |
Correct |
1112 ms |
84464 KB |
Output is correct |
35 |
Correct |
1265 ms |
77924 KB |
Output is correct |
36 |
Correct |
1302 ms |
69164 KB |
Output is correct |
37 |
Correct |
234 ms |
28992 KB |
Output is correct |
38 |
Correct |
174 ms |
25144 KB |
Output is correct |
39 |
Correct |
237 ms |
29032 KB |
Output is correct |
40 |
Correct |
176 ms |
25220 KB |
Output is correct |
41 |
Correct |
234 ms |
28756 KB |
Output is correct |
42 |
Correct |
177 ms |
25388 KB |
Output is correct |
43 |
Correct |
957 ms |
84552 KB |
Output is correct |
44 |
Correct |
943 ms |
85964 KB |
Output is correct |
45 |
Correct |
81 ms |
22344 KB |
Output is correct |
46 |
Correct |
130 ms |
24132 KB |
Output is correct |
47 |
Correct |
574 ms |
49164 KB |
Output is correct |
48 |
Correct |
1260 ms |
80768 KB |
Output is correct |
49 |
Correct |
150 ms |
29232 KB |
Output is correct |
50 |
Correct |
146 ms |
28840 KB |
Output is correct |
51 |
Correct |
160 ms |
29880 KB |
Output is correct |
52 |
Correct |
196 ms |
33076 KB |
Output is correct |
53 |
Correct |
196 ms |
33084 KB |
Output is correct |
54 |
Correct |
224 ms |
32892 KB |
Output is correct |