Submission #1005594

#TimeUsernameProblemLanguageResultExecution timeMemory
1005594vjudge1Street Lamps (APIO19_street_lamps)C++17
100 / 100
2123 ms143888 KiB
#include<bits/stdc++.h> #pragma GCC optimize("O3") #pragma GCC target("avx2") #define ll long long #define pii pair<int,int> #define pll pair<ll,ll> #define plx pair<ll,int> #define f first #define s second #define pb push_back #define all(x) x.begin(),x.end() #define vi vector<int> #define vl vector<ll> using namespace std; const int mxn=3e5+5; int a[mxn],b[mxn],ans[mxn],n; bool vis[mxn]{0}; vector<pii>qr[mxn]; multiset<int>ms,ms2; vector<int>bit[mxn]; vector<int>bval[mxn]; vector<int>val[mxn]; void build(){ for(int i=1;i<=n;i++){ if(val[i].empty())continue; sort(val[i].begin(),val[i].end()); for(auto it : val[i]){ for(int j=i;j<=n;j+=j&-j){ bval[j].pb(it); } } } for(int i=1;i<=n;i++)sort(bval[i].begin(),bval[i].end()),bval[i].erase(unique(bval[i].begin(),bval[i].end()),bval[i].end()); for(int i=1;i<=n;i++)bit[i].resize((int)bval[i].size()+1,0); } int ub(int x,int y){ return upper_bound(bval[x].begin(),bval[x].end(),y)-bval[x].begin(); } void upd(int i,int j,int amt){ for(int x=i;x<=n;x+=x&-x)for(int y=ub(x,j);y<bit[x].size();y+=y&-y)bit[x][y]+=amt; } int query(int a,int b,int res=0){ for(int x=a;x>0;x-=x&-x)for(int y=ub(x,b);y>0;y-=y&-y)res+=bit[x][y]; return res; } int main(){ ios_base::sync_with_stdio(0);cin.tie(0); int q;cin>>n>>q;ms.insert(0);ms.insert(n+1);ms2.insert(0);ms2.insert(n+1); for(int i=1;i<=n;i++){ char x;cin>>x;a[i]=x-'0';b[i]=a[i]; if(!a[i])ms.insert(i),ms2.insert(i); }vector<pair<int,pii>>qq; for(int i=1;i<=q;i++){ string s;cin>>s;ans[i]=-1; if(s=="toggle"){ int x;cin>>x;qq.pb({1,{x,0}}); if(a[x]){ int l = 1+*prev(ms.lower_bound(x)); int r = -1+*ms.upper_bound(x); val[l].pb(x);val[l].pb(r+1);val[x+1].pb(x);val[x+1].pb(r+1); a[x]=0;ms.insert(x); } else { int l = 1+*prev(ms.lower_bound(x)); int r = -1+*ms.upper_bound(x); val[l].pb(x);val[l].pb(r+1);val[x+1].pb(x);val[x+1].pb(r+1); a[x]=1;ms.erase(x); } } else { int l,r;cin>>l>>r;qq.pb({2,{l,r}}); } }build(); for(int i=0;i<qq.size();i++){ auto it=qq[i]; if(it.f==1){int x=it.s.f; if(b[x]){ int l = 1+*prev(ms2.lower_bound(x)); int r = -1+*ms2.upper_bound(x); upd(l,x,i+1);upd(l,r+1,-i-1);upd(x+1,x,-i-1);upd(x+1,r+1,i+1); b[x]=0;ms2.insert(x); } else { int l = 1+*prev(ms2.lower_bound(x)); int r = -1+*ms2.upper_bound(x); upd(l,x,-i-1);upd(l,r+1,i+1);upd(x+1,x,i+1);upd(x+1,r+1,-i-1); b[x]=1;ms2.erase(x); } } else { ans[i+1] = query(it.s.f,it.s.s-1); int ij = *ms2.lower_bound(it.s.f); if(ij>it.s.s-1)ans[i+1]+=i+1; } } for(int i=1;i<=q;i++)if(ans[i]!=-1)cout<<ans[i]<<'\n'; }

Compilation message (stderr)

street_lamps.cpp: In function 'void upd(int, int, int)':
street_lamps.cpp:40:49: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   40 |     for(int x=i;x<=n;x+=x&-x)for(int y=ub(x,j);y<bit[x].size();y+=y&-y)bit[x][y]+=amt;
      |                                                ~^~~~~~~~~~~~~~
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:74:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   74 |     for(int i=0;i<qq.size();i++){
      |                 ~^~~~~~~~~~
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...