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<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include<cmath>
#include<set>
#include<algorithm>
#include<bitset>
#include<list>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<pii,pii>
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define p push
#define pb push_back
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
using namespace std;
#define int long long
const int mxn=3e5,mod=69,lg=42,root=80,inf=1e18;
int n;
bitset<mxn+10>on;
struct seg{
int vmx[2*mxn+10],vmn[2*mxn+10];
void init(){for(int i=0;i<=2*mxn;i++)vmx[i]=vmn[i]=0;}
void updatemx(int pos,int val){
pos+=n;
vmx[pos]=val;
for(;pos>0;pos>>=1)vmx[pos>>1]=max(vmx[pos],vmx[pos^1]);
}
int qrymx(int l,int r){
int ans=0;
for(l+=n,r+=n;l<=r;l>>=1,r>>=1){
if(l&1)ans=max(ans,vmx[l++]);
if(!(r&1))ans=max(ans,vmx[r--]);
}
return ans;
}
void updatemn(int pos,int val){
pos+=n;
vmn[pos]=val;
for(;pos>0;pos>>=1)vmn[pos>>1]=min(vmn[pos],vmn[pos^1]);
}
int qrymn(int l,int r){
int ans=inf;
for(l+=n,r+=n;l<=r;l>>=1,r>>=1){
if(l&1)ans=min(ans,vmn[l++]);
if(!(r&1))ans=min(ans,vmn[r--]);
}
return ans;
}
}t;//pos qry mx
struct fen{
vector<int>all_y[mxn+10],fwk[mxn+10];
int rank(int x,int y){return upper_bound(all(all_y[x]),y)-all_y[x].begin();}
void init(vector<pii>v){
sort(all(v));
vector<int>cnt_y(mxn+10,0);
for(auto i:v){
for(int j=i.f;j<=n;j+=(j&-j))++cnt_y[j];
}
for(int i=0;i<=n;i++){
all_y[i].reserve(cnt_y[i]);
fwk[i].resize(cnt_y[i]);
}
for(auto i:v)for(int j=i.f;j<=n;j+=(j&-j))all_y[j].pb(i.s);
}
void update(int x,int y2,int t){
for(;x<=n;x+=x&-x)for (int y=rank(x,y2);y<=all_y[x].size();y+=y&-y)fwk[x][y-1]+=t;
}
int qry(int x,int y2){
int ans=0;
for (;x;x-=x&-x)for(int y=rank(x,y2);y;y-=y&-y)ans+=fwk[x][y-1];
return ans;
}
}f;
pii bs(int pos){
int l=pos,r=n;
int a=pos,b=inf;
while(l<=r){
int mid=l+(r-l)/2;
if(t.qrymn(pos,mid)){
l=mid+1,a=max(a,mid);
}
else r=mid-1;
}
l=1,r=pos;
while(l<=r){
int mid=l+(r-l)/2;
if(t.qrymn(mid,pos))r=mid-1,b=min(b,mid);
else l=mid+1;
}
return {b,a};
}
vector<int>u;
vector<pii>pass;
map<pii,bool>yes;
void put(pii a){
if(!yes[{a.f,a.f}])pass.pb({a.f,a.f});
if(!yes[{a.f,a.s+1}])pass.pb({a.f,a.s+1});
yes[{a.f,a.s}]=true;
yes[{a.f,a.s+1}]=true;
}
int32_t main(){
fastio
int m;cin>>n>>m;
string a,b;cin>>b;
t.init();
for(int i=0;i<n;i++){
if(b[i]=='0')t.updatemx(i+1,inf);
else on[i+1]=true;
t.updatemn(i+1,b[i]-'0');
}
vector<ppii>q;
q.pb({{0,0},{0,0}});
for(int i=1;i<=m;i++){
cin>>a;
if(a[0]=='t'){
int x;cin>>x;
if(on[x]){
pii range=bs(x);
t.updatemn(x,0);
on[x]=false;
put(range);
q.pb({{1,x},{range.f,range.s}});
}
else{
on[x]=true;
t.updatemn(x,1);
q.pb({{1,x},{x,-1}});
}
}
else{
int l,r;cin>>l>>r;
if(!yes[{l,r-1}])pass.pb({l,r-1}),yes[{l,r-1}]=true;
q.pb({{2,1},{l,r-1}});
}
}
f.init(pass);
on.reset();
for(int i=0;i<n;i++)if(b[i]=='1')on[i+1]=true;
for(int i=1;i<=m;i++){//time
if(q[i].f.f==1){
int x=q[i].f.s;
if(on[x]){
on[x]=false;
pii range=q[i].s;
int g=t.vmx[x+n],comp=0;
if(g!=inf){
comp=i-g;
f.update(q[i].s.f,q[i].s.s,comp);
f.update(q[i].s.f,q[i].s.s+1,-comp);
}
t.updatemx(x,inf);
}
else{
on[x]=true;
t.updatemx(x,i);
}
}
else{
int l=q[i].s.f,r=q[i].s.s;
int g=t.qrymx(l,r);
if(g==inf)g=0;
else g=i-g;
cout<<g+f.qry(l,r)<<'\n';
}
}
}
Compilation message (stderr)
street_lamps.cpp: In member function 'void fen::update(long long int, long long int, long long int)':
street_lamps.cpp:81:50: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
81 | for(;x<=n;x+=x&-x)for (int y=rank(x,y2);y<=all_y[x].size();y+=y&-y)fwk[x][y-1]+=t;
| ~^~~~~~~~~~~~~~~~~
street_lamps.cpp: In function 'int32_t main()':
street_lamps.cpp:159:21: warning: variable 'range' set but not used [-Wunused-but-set-variable]
159 | pii range=q[i].s;
| ^~~~~
# | 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... |