#include <iostream>
#include <fstream>
#include <set>
using namespace std;
const int nmax=1005;
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=0;rs=0;
sum=0;
}
};
set<pct> s;
set<pct>::iterator it;
string tip;
struct aint
{
node *arb[4*nmax];
int nr,stX,drX,stY,drY;
void uY(node *nod,int l,int r,int poz,int val)
{
nod->sum+=poz;
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);
}
void update(int nod,int l,int r,int x,int y,int val)
{
uY(arb[nod],1,n,y,val);
if(l==r) return;
int m=(l+r)/2;
if(x<=m) update(2*nod,l,m,x,y,val);
else update(2*nod+1,m+1,r,x,y,val);
}
int ret;
void query(int nod,int l,int r)
{
if(stX<=l&&r<=drX)
{
qY(arb[nod],1,n);
return;
}
int m=(l+r)/2;
if(stX<=m) query(2*nod,l,m);
if(m<drX) query(2*nod+1,m+1,r);
}
int Q(int x,int y)
{
ret=0;
stX=1;drX=x;stY=y;drY=n;
query(1,1,n);
return ret;
}
void build(int nod,int l,int r)
{
arb[nod]=new node;
if(l==r) return;
int m=(l+r)/2;
build(2*nod,l,m);
build(2*nod+1,m+1,r);
}
}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(1,1,n,st.x,st.y,i-st.val);
act.x=st.x;
s.erase(st);
}
if(dr!=nl)
{
A.update(1,1,n,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(1,1,n,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.build(1,1,n);
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
street_lamps.cpp: In function 'int main()':
street_lamps.cpp:157:14: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(i=1;i<ini.size();i++)
~^~~~~~~~~~~
street_lamps.cpp:162:20: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(j<ini.size()&&ini[j]=='1')
~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
657 ms |
1548 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
1528 KB |
Output is correct |
2 |
Incorrect |
6 ms |
1400 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
504 KB |
Output is correct |
2 |
Incorrect |
5 ms |
888 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
2 ms |
376 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |