#include<bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector,Ofast")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<ll,ll> pll;
const ll MOD=1e9+7;
const ll MOD2=998244353;
const ll N=3e5+5;
const ld pi=acos(-1);
const ll INF=(1LL<<60);
#define SQ(i) ((i)*(i))
#define REP(i,n) for(ll i=0;i<n;i++)
#define REP1(i,n) for(ll i=1;i<=n;i++)
#define pb push_back
#define mp make_pair
#define X first
#define Y second
#define setp setprecision
#define lwb lower_bound
#define SZ(_a) (ll)_a.size()
char c;
ll n,q,x,y,state[N],ans[N],ty[N];
string s;
vector<pair<pll,pll> > ioi;
set<ll> location;
ll find_R(ll idx){
return *location.lwb(idx)-1;
}
ll find_L(ll idx){
return *prev(location.lwb(idx+1))+1;
}
void upd(ll idx){
state[idx]^=1;
if(state[idx]==0){
location.insert(idx);
}else{
location.erase(idx);
}
}
ll bit[N];
void add(ll idx,ll d){
if(!d)return ;
for(int i=idx;i<N;i+=(i&-i)){
bit[i]+=d;
}
}
ll query(ll idx){
ll tmp=0;
for(int i=idx;i>0;i-=(i&-i)){
tmp+=bit[i];
}
return tmp;
}
void CDQ(ll l,ll r){
if(l==r-1)return ;
ll mid=(l+r)>>1;
CDQ(l,mid);CDQ(mid,r);
ll iter=l;
for(int i=mid;i<r;i++){
while(iter<mid&&ioi[iter].X.Y<=ioi[i].X.Y)add(ioi[iter].Y.X,ioi[iter].X.X*ioi[iter].Y.Y),iter++;
if(ioi[i].Y.Y==0){
ans[ioi[i].X.X]+=query(ioi[i].Y.X);
}
}
while(iter>l)iter--,add(ioi[iter].Y.X,-ioi[iter].X.X*ioi[iter].Y.Y);
sort(ioi.begin()+l,ioi.begin()+r,[](pair<pll,pll> A,pair<pll,pll> B){
return (A.X.Y<B.X.Y);
});
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
cin>>n>>q;
REP(i,n+2)location.insert(i);
REP1(i,n){
cin>>c;
if(c=='1')upd(i);
}
REP1(i,q){
cin>>s;
if(s[0]=='q'){
ty[i]=2;
cin>>x>>y;
--y;
///cout<<"query "<<i<<" "<<x<<" "<<y<<" "<<find_R(1,1,n+1,x)<<"\n";
if(find_R(x)>=y){ans[i]+=i;}
ioi.pb(mp(mp(i,x),mp(y,0)));
}else{
ty[i]=1;
cin>>x;
ll L=find_L(x-1),R=find_R(x+1);
//cout<<x<<" "<<L<<" "<<R<<"\n";
if(state[x]==0){
//[L,x] [x,R]-=i;
ioi.pb(mp(mp(i,L),mp(x,-1)));
ioi.pb(mp(mp(i,L),mp(R+1,1)));
ioi.pb(mp(mp(i,x+1),mp(x,1)));
ioi.pb(mp(mp(i,x+1),mp(R+1,-1)));
}else{
//[L,x] [x,R] +=i ;
ioi.pb(mp(mp(i,L),mp(x,1)));
ioi.pb(mp(mp(i,L),mp(R+1,-1)));
ioi.pb(mp(mp(i,x+1),mp(x,-1)));
ioi.pb(mp(mp(i,x+1),mp(R+1,1)));
}
upd(x);
}
}
sort(ioi.begin(),ioi.end());
CDQ(0,SZ(ioi));
/*
REP(i,SZ(ioi)){
if(ioi[i].Y.Y==0){
REP(j,i){
if(ioi[j].Y.Y!=0&&ioi[j].X.Y<=ioi[i].X.Y&&ioi[j].Y.X<=ioi[i].Y.X){
ans[ioi[i].X.X]+=(ioi[j].X.X*ioi[j].Y.Y);
}
}
}
}
*/
REP1(i,q){
if(ty[i]==2)cout<<ans[i]<<"\n";
}
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1448 ms |
36776 KB |
Output is correct |
2 |
Correct |
1513 ms |
39352 KB |
Output is correct |
3 |
Correct |
1696 ms |
40100 KB |
Output is correct |
4 |
Correct |
2109 ms |
56884 KB |
Output is correct |
5 |
Correct |
1788 ms |
58036 KB |
Output is correct |
6 |
Correct |
2296 ms |
54528 KB |
Output is correct |
7 |
Correct |
906 ms |
38776 KB |
Output is correct |
8 |
Correct |
701 ms |
42732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
748 KB |
Output is correct |
2 |
Correct |
4 ms |
748 KB |
Output is correct |
3 |
Correct |
3 ms |
620 KB |
Output is correct |
4 |
Correct |
2 ms |
492 KB |
Output is correct |
5 |
Correct |
3118 ms |
88080 KB |
Output is correct |
6 |
Correct |
2461 ms |
58256 KB |
Output is correct |
7 |
Correct |
1778 ms |
56244 KB |
Output is correct |
8 |
Correct |
721 ms |
42988 KB |
Output is correct |
9 |
Correct |
244 ms |
15844 KB |
Output is correct |
10 |
Correct |
278 ms |
24144 KB |
Output is correct |
11 |
Correct |
284 ms |
24288 KB |
Output is correct |
12 |
Correct |
871 ms |
38480 KB |
Output is correct |
13 |
Correct |
705 ms |
42636 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
2 ms |
748 KB |
Output is correct |
2 |
Correct |
3 ms |
620 KB |
Output is correct |
3 |
Correct |
4 ms |
748 KB |
Output is correct |
4 |
Correct |
5 ms |
748 KB |
Output is correct |
5 |
Correct |
939 ms |
40184 KB |
Output is correct |
6 |
Correct |
1506 ms |
56628 KB |
Output is correct |
7 |
Correct |
2187 ms |
54076 KB |
Output is correct |
8 |
Correct |
3138 ms |
88760 KB |
Output is correct |
9 |
Correct |
2031 ms |
37268 KB |
Output is correct |
10 |
Correct |
2296 ms |
70936 KB |
Output is correct |
11 |
Correct |
2021 ms |
37184 KB |
Output is correct |
12 |
Correct |
2323 ms |
71100 KB |
Output is correct |
13 |
Correct |
2001 ms |
37196 KB |
Output is correct |
14 |
Correct |
2319 ms |
71240 KB |
Output is correct |
15 |
Correct |
861 ms |
38600 KB |
Output is correct |
16 |
Correct |
703 ms |
42732 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Correct |
1 ms |
364 KB |
Output is correct |
3 |
Correct |
1 ms |
492 KB |
Output is correct |
4 |
Correct |
1 ms |
364 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
364 KB |
Output is correct |
7 |
Correct |
1 ms |
384 KB |
Output is correct |
8 |
Correct |
1448 ms |
36776 KB |
Output is correct |
9 |
Correct |
1513 ms |
39352 KB |
Output is correct |
10 |
Correct |
1696 ms |
40100 KB |
Output is correct |
11 |
Correct |
2109 ms |
56884 KB |
Output is correct |
12 |
Correct |
1788 ms |
58036 KB |
Output is correct |
13 |
Correct |
2296 ms |
54528 KB |
Output is correct |
14 |
Correct |
906 ms |
38776 KB |
Output is correct |
15 |
Correct |
701 ms |
42732 KB |
Output is correct |
16 |
Correct |
5 ms |
748 KB |
Output is correct |
17 |
Correct |
4 ms |
748 KB |
Output is correct |
18 |
Correct |
3 ms |
620 KB |
Output is correct |
19 |
Correct |
2 ms |
492 KB |
Output is correct |
20 |
Correct |
3118 ms |
88080 KB |
Output is correct |
21 |
Correct |
2461 ms |
58256 KB |
Output is correct |
22 |
Correct |
1778 ms |
56244 KB |
Output is correct |
23 |
Correct |
721 ms |
42988 KB |
Output is correct |
24 |
Correct |
244 ms |
15844 KB |
Output is correct |
25 |
Correct |
278 ms |
24144 KB |
Output is correct |
26 |
Correct |
284 ms |
24288 KB |
Output is correct |
27 |
Correct |
871 ms |
38480 KB |
Output is correct |
28 |
Correct |
705 ms |
42636 KB |
Output is correct |
29 |
Correct |
2 ms |
748 KB |
Output is correct |
30 |
Correct |
3 ms |
620 KB |
Output is correct |
31 |
Correct |
4 ms |
748 KB |
Output is correct |
32 |
Correct |
5 ms |
748 KB |
Output is correct |
33 |
Correct |
939 ms |
40184 KB |
Output is correct |
34 |
Correct |
1506 ms |
56628 KB |
Output is correct |
35 |
Correct |
2187 ms |
54076 KB |
Output is correct |
36 |
Correct |
3138 ms |
88760 KB |
Output is correct |
37 |
Correct |
2031 ms |
37268 KB |
Output is correct |
38 |
Correct |
2296 ms |
70936 KB |
Output is correct |
39 |
Correct |
2021 ms |
37184 KB |
Output is correct |
40 |
Correct |
2323 ms |
71100 KB |
Output is correct |
41 |
Correct |
2001 ms |
37196 KB |
Output is correct |
42 |
Correct |
2319 ms |
71240 KB |
Output is correct |
43 |
Correct |
861 ms |
38600 KB |
Output is correct |
44 |
Correct |
703 ms |
42732 KB |
Output is correct |
45 |
Correct |
771 ms |
21820 KB |
Output is correct |
46 |
Correct |
860 ms |
21820 KB |
Output is correct |
47 |
Correct |
1171 ms |
29108 KB |
Output is correct |
48 |
Correct |
2055 ms |
55220 KB |
Output is correct |
49 |
Correct |
327 ms |
24272 KB |
Output is correct |
50 |
Correct |
317 ms |
24172 KB |
Output is correct |
51 |
Correct |
320 ms |
24396 KB |
Output is correct |
52 |
Correct |
315 ms |
24160 KB |
Output is correct |
53 |
Correct |
312 ms |
23628 KB |
Output is correct |
54 |
Correct |
314 ms |
24268 KB |
Output is correct |