This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include<bits/stdc++.h>
#define st first
#define nd second
#define all(x) (x).begin(), (x).end()
#define pb push_back
#define eb emplace_back
#define pp pop_back
#define mp make_pair
using namespace std;
using pii = pair<int, int>;
using ll = long long;
using vi = vector<int>;
using vii = vector<pii>;
void debug(){cerr<<"\n";}
template<typename H, typename... T>
void debug(H h, T... t){
cerr<<h;
if(sizeof...(t)){
cerr<<", ";
}
debug(t...);
}
#define deb(x...) cerr<<#x<<" = ";debug(x);
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const int N=1<<19;
vi todo[N];
int pref[N+N], suf[N+N], L[N+N], R[N+N], siz[N+N], M[N], tim[N+N];
int male[N];
int ans[N], typ[N];
vector<pair<pii, pii> > U[N];
vector<pair<int, pii> > Q[N];
void upd(int v){
int l=v+v, r=v+v+1;
if(pref[l]==siz[l])pref[v]=pref[r]+siz[l];
else pref[v]=pref[l];
if(suf[r]==siz[r])suf[v]=suf[l]+siz[r];
else suf[v]=suf[r];
}
int main(){
int n, q;
cin>>n>>q;
string s;
cin>>s;
for(int i=0; i<n; i++){
siz[N+i]=1;
L[N+i]=i;
R[N+i]=i+1;
suf[N+i]=pref[N+i]=(s[i]-'0');
}
for(int i=N-1; i; i--){
siz[i]=siz[i+i]+siz[i+i+1];
L[i]=L[i+i];
M[i]=R[i+i];
if(R[i+i+1]==0)R[i]=R[i+i];
else R[i]=R[i+i+1];
upd(i);
}
for(int qq=1; qq<=q; qq++){
cin>>s;
if(s[0]=='q'){
//deb(qq);
typ[qq]=1;
int a, b;
cin>>a>>b;
a--;
b--;
if(a==b-1){
//deb(a, suf[N+a], tim[N+a]);
ans[qq]=male[a]+suf[N+a]*(qq-tim[N+a]);
continue;
}
//[a, b)
int v=1;
while(v<N){
int m=M[v];
if(m>=b)v=v+v;
else if(m<=a)v=v+v+1;
else{
int x=m-a, y=b-m;
//deb(v, L[v], R[v], suf[v+v], pref[v+v+1], qq-tim[v]);deb(x, y);
U[v].eb(mp(qq, qq-tim[v]), mp(suf[v+v], pref[v+v+1]));
tim[v]=qq;
Q[v].eb(qq, mp(x, y));
break;
}
}
}
else{
int v;
cin>>v;
v--;
if(suf[N+v]){
male[v]+=qq-tim[N+v];
}
v+=N;
tim[v]=qq;
for(int vv=v/2; vv; vv/=2){
U[vv].eb(mp(qq, qq-tim[vv]), mp(suf[vv+vv], pref[vv+vv+1]));
}
suf[v]^=1;
pref[v]^=1;
for(v/=2; v; v/=2){
//deb(v, L[v], R[v], suf[v+v], pref[v+v+1], qq-tim[v]);
tim[v]=qq;
upd(v);
}
}
}
for(int v=1; v<N; v++){
if(Q[v].size()){
for(auto i:Q[v]){
for(auto j:U[v]){
if(j.st.st<=i.st && j.nd.st>=i.nd.st && j.nd.nd>=i.nd.nd){
ans[i.st]+=j.st.nd;
}
}
}
}
}
for(int i=1; i<=q; i++){
if(typ[i])cout<<ans[i]<<"\n";
}
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |