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 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
| #include<vector> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; typedef long long ll; const int N=100000; struct SPLAYTREE{ struct NODE{ NODE *ch[2],*fa; ll key,lzy; int siz; }nod[N+7],*root,*ncnt,*NIL; inline void Rebuild(){ NIL=ncnt=&nod[0]; root=NIL->ch[0]=NIL->ch[1]=NIL->fa=NIL; } inline NODE *NewNode(ll key){ NODE *p=++ncnt; p->ch[0]=p->ch[1]=p->fa=NIL; p->key=key;p->lzy=0;p->siz=1; return p; } inline void PushUp(NODE *rt){ rt->siz=rt->ch[1]->siz+rt->ch[0]->siz+1; } inline void PushDown(NODE *rt){ if(rt->lzy){ rt->key-=rt->lzy; if(rt->ch[0]!=NIL) rt->ch[0]->lzy+=rt->lzy; if(rt->ch[1]!=NIL) rt->ch[1]->lzy+=rt->lzy; rt->lzy=0; } } inline void Rotate(NODE *x){ NODE *y=x->fa; int dir=(y->ch[0]==x); x->fa=y->fa;if(y->fa!=NIL) y->fa->ch[y->fa->ch[1]==y]=x; y->ch[!dir]=x->ch[dir];if(x->ch[dir]!=NIL) x->ch[dir]->fa=y; x->ch[dir]=y;y->fa=x; if(root==y) root=x; PushUp(y); } void Splay(NODE *x,NODE *rt){ NODE *y,*z; while(x->fa!=rt){ y=x->fa;z=y->fa; if(z==rt) Rotate(x); else{ if((z->ch[0]==y)^(y->ch[0]==x)) Rotate(x); else Rotate(y); Rotate(x); } } } NODE *renod; inline NODE *ReNode(ll key){ NODE *p=renod; p->ch[0]=p->ch[1]=p->fa=NIL; p->key=key;p->lzy=0;p->siz=1; return p; } inline void Insert(NODE *&rt,NODE *pre,ll key,bool rebegin){ if(rt==NIL){ if(rebegin) rt=ReNode(key); else rt=NewNode(key); rt->fa=pre; Splay(rt,NIL); return; } PushDown(rt); rt->siz++; int dir=(key>rt->key); Insert(rt->ch[dir],rt,key,rebegin); } inline NODE *FindPre(NODE *rt,ll key){ NODE *p; PushDown(rt); if(rt==NIL) return NIL; if(key>rt->key){ p=FindPre(rt->ch[1],key); if(p==NIL) p=rt,Splay(p,NIL); } else p=FindPre(rt->ch[0],key); return p; } inline NODE *FindNxt(NODE *rt,ll key){ NODE *p; PushDown(rt); if(rt==NIL) return NIL; if(key<rt->key){ p=FindNxt(rt->ch[0],key); if(p==NIL) p=rt,Splay(p,NIL); } else p=FindNxt(rt->ch[1],key); return p; } NODE *lst[N+7];int lstsiz; inline void GetList(NODE *rt){ if(rt==NIL) return; PushDown(rt); lst[lstsiz++]=rt; GetList(rt->ch[0]); GetList(rt->ch[1]); } void Modify(ll k){ NODE *x=FindPre(root,k+1),*y=FindNxt(root,2*k); Splay(x,NIL);Splay(y,x); lstsiz=0; GetList(y->ch[0]); int delsiz=y->ch[0]->siz; NODE *pos=y->ch[0];y->ch[0]=NIL; pos=pos->fa; while(pos!=NIL) pos->siz-=delsiz,pos=pos->fa; for(int i=0;i<lstsiz;i++) renod=lst[i],Insert(root,NIL,lst[i]->key-k,true); y->key-=k; y->ch[1]->lzy+=k; } NODE *Select(int rnk,NODE *rt){ NODE *p=root; while(true){ PushDown(p); if(rnk==p->ch[0]->siz+1) break; if(rnk<p->ch[0]->siz+1) p=p->ch[0]; else rnk-=p->ch[0]->siz+1,p=p->ch[1]; } Splay(p,rt); return p; } }splay; int main(){ int n,m;scanf("%d%d",&n,&m); splay.Rebuild(); splay.Insert(splay.root,splay.NIL,-(1ll<<50),false); splay.Insert(splay.root,splay.NIL,(1ll<<50),false); for(int i=0,key;i<n;i++){ scanf("%d",&key); splay.Insert(splay.root,splay.NIL,key,false); } while(m--){ int opt;ll x;scanf("%d%lld",&opt,&x); if(opt==1){ x++; SPLAYTREE::NODE *res=splay.Select(x,splay.NIL); printf("%lld\n",res->key); } else splay.Modify(x); } return 0; }
|