1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
| int n,q,w[N],sta[N],top,pos[N]; bool check(int p,int x){ if(p&1)return pos[p]<=x&&x<pos[p]+w[sta[p]]; return pos[p]-w[sta[p]]<=x&&x<pos[p]; } int query(int x){ int l=1,r=top,res=1; while(l<=r){ int mid=(l+r)/2; if(check(mid,x))res=mid,l=mid+1; else r=mid-1; } int p=sta[res]; if(res&1)x-=pos[res]; else x=pos[res]-(x+1); l=1,r=p,res=p; while(l<=r){ int mid=(l+r)/2; if(x<w[mid])res=mid,r=mid-1; else l=mid+1; } return res; } int main(){ n=read(); REP(i,1,n)w[i]=read();w[n+1]=inf; sta[++top]=n+1,pos[top]=0; q=read(); REP(i,1,q){ int type=read(),x=read(); if(type==1){ while(x>=sta[top])--top; if(top%2==0)sta[++top]=x,pos[top]=pos[top-1]-w[x]; }else if(type==2){ while(x>=sta[top])--top; if(top%2==1)sta[++top]=x,pos[top]=pos[top-1]+w[x]; }else if(type==3)write(n+1-query(x)),putchar('\n'); } return 0; }
|