Submission #142664

#TimeUsernameProblemLanguageResultExecution timeMemory
142664Bodo171Street Lamps (APIO19_street_lamps)C++14
100 / 100
2832 ms287840 KiB
#include <iostream> #include <fstream> #include <set> using namespace std; const int nmax=300005; struct pct { int x,y,val; }nl; int n,i,q,j,poz,a,b; string ini; bool operator <(pct unu,pct doi) { return unu.x<doi.x; } bool operator !=(pct unu,pct doi) { return (unu.x!=doi.x||unu.y!=doi.y||unu.val!=doi.val); } struct node { node *ls,*rs; int sum; node() { ls=rs=0; sum=0; } }; set<pct> s; set<pct>::iterator it; string tip; struct aint { node *arb[nmax]; int nr,stX,drX,stY,drY; void uY(node *nod,int l,int r,int poz,int val) { nod->sum+=val; if(l==r) return; int m=(l+r)/2; if(poz<=m) { if(!nod->ls) nod->ls=new node; uY(nod->ls,l,m,poz,val); } else { if(!nod->rs) nod->rs=new node; uY(nod->rs,m+1,r,poz,val); } } void qY(node *nod,int l,int r) { if(stY<=l&&r<=drY) { ret+=nod->sum; return; } int m=(l+r)/2; if(stY<=m&&nod->ls) qY(nod->ls,l,m); if(m<drY&&nod->rs) qY(nod->rs,m+1,r); } int ret; inline int lbit(int x) { return ((x^(x-1))&x); } void query(int x,int y) { for(int idx=x;idx>0;idx-=lbit(idx)) qY(arb[idx],1,n); } void update(int x,int y,int val) { for(int idx=x;idx<=n;idx+=lbit(idx)) uY(arb[idx],1,n,y,val); } int Q(int x,int y) { ret=0; stY=y;drY=n; query(x,y); return ret; } void ini() { for(i=1;i<=n;i++) arb[i]=new node; } }A; void ins(int poz) { pct st=nl,dr=nl,act; it=s.lower_bound({poz,0,0}); if((*it).x==poz+1) dr=(*it); if(it!=s.begin()) { it--; if((*it).y==poz-1) st=(*it); } act.val=i;act.x=act.y=poz; if(st!=nl) { A.update(st.x,st.y,i-st.val); act.x=st.x; s.erase(st); } if(dr!=nl) { A.update(dr.x,dr.y,i-dr.val); act.y=dr.y; s.erase(dr); } s.insert(act); return; } void del(int poz) { pct st=nl,dr=nl,act; it=s.lower_bound({poz+1,0,0}); it--;//mereu se poate act=(*it); A.update(act.x,act.y,i-act.val); s.erase(act); if(poz>act.x) { st={act.x,poz-1,i}; s.insert(st); } if(poz<act.y) { dr={poz+1,act.y,i}; s.insert(dr); } } int main() { //freopen("data.in","r",stdin); ios_base::sync_with_stdio(false); cin>>n>>q; cin>>ini; ini=" "+ini; for(i=1;i<ini.size();i++) { if(ini[i]=='1') { j=i; while(j<ini.size()&&ini[j]=='1') j++; j--; s.insert({i,j,0}); i=j; } } A.ini(); for(i=1;i<=q;i++) { cin>>tip; if(tip=="toggle") { cin>>poz; if(ini[poz]=='1') { ini[poz]='0'; del(poz); } else { ini[poz]='1'; ins(poz); } } else { int ans; cin>>a>>b; it=s.lower_bound({b,0,0}); ans=0; if(it!=s.begin()) { it--; if((*it).x<=a&&(*it).y>=b-1) ans=i-(*it).val; } ans+=A.Q(a,b-1); cout<<ans<<'\n'; } } return 0; }

Compilation message (stderr)

street_lamps.cpp: In function 'int main()':
street_lamps.cpp:149:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
     for(i=1;i<ini.size();i++)
             ~^~~~~~~~~~~
street_lamps.cpp:154:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
             while(j<ini.size()&&ini[j]=='1')
                   ~^~~~~~~~~~~
#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...