Bzoj1861[zjoi2006]book Bookshelf

Source: Internet
Author: User

Bzoj1861[zjoi2006]book Bookshelf

Test instructions

Maintains a sequence that supports moving elements, the query element is the first, and the number of k elements is queried.

Exercises

can use Treap and splay, I write is splay. Moving an element is to delete a node before inserting the node into its corresponding position, noting the order of various sub-operations (such as Splay, find). Reflection: Ben Weak and WA and T, and finally made a limit to the data found in the dead loop, the big data for a half-day to find that the sub-operation of improper order caused errors.

Code:

1#include <cstdio>2#include <cstring>3#include <algorithm>4 #defineInc (I,J,K) for (int i=j;i<=k;i++)5 #defineMAXN 801006 using namespacestd;7 8 intch[maxn][2],fa[maxn],v[maxn],sz[maxn],pos[maxn],root,book[maxn],tot,n,m;9InlineintRead () {Ten     CharCh=getchar ();intf=1, x=0; One      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; Ch=GetChar ();}; A      while(ch>='0'&&ch<='9') x=x*Ten+ch-'0', ch=GetChar (); -     returnf*x; - } theInlinevoidUpdateintx) {sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;} - voidRotateintx) { -     if(!x| |! FA[X])return;intA=FA[X],B=FA[FA[X]];BOOLc=x==ch[fa[x]][1],d=a==ch[fa[a]][1]; -     if(b) ch[b][d]=x; Fa[x]=b; CH[A][C]=CH[X][!C];if(Ch[x][!c]) fa[ch[x][!c]]=a; Ch[x][!c]=a; fa[a]=x; +Update (a); Update (x);if(b) update (b); - } + voidSplay (intXinty) { A     if(x==y)return;intz=Fa[y]; at      while(fa[x]!=z) { -         if(fa[x]!=y) (x==ch[fa[x]][1]) ^ (fa[x]==ch[fa[fa[x]]][1])?Rotate (x): Rotate (fa[x]); - rotate (x); -     } -     if(root==y) root=x; - } in voidBuildintXintLintR) { -     intMid=l+r>>1; V[x]=book[mid]; pos[book[mid]]=x; to     if(l<=mid-1) ch[x][0]=++tot,build (ch[x][0],l,mid-1), fa[ch[x][0]]=x; +     if(mid+1&LT;=R) ch[x][1]=++tot,build (ch[x][1],mid+1, R), fa[ch[x][1]]=x; - update (x); the } * intQuerynum (intXintk) { $     if(k<=sz[ch[x][0]])returnQuerynum (ch[x][0],k);Panax Notoginseng     if(k==sz[ch[x][0]]+1)returnx; -     returnQuerynum (ch[x][1],k-sz[ch[x][0]]-1); the } + intQueryrank (intx) { ASplay (X,root);returnsz[ch[x][0]]; the } + intPreinty) { -Splay (Y,root);returnQuerynum (ch[y][0],sz[ch[y][0]]); $ } $ intNexinty) { -Splay (Y,root);returnQuerynum (ch[y][1],1); - } the voidAddintXintYintz) { -Splay (Y,root); Splay (x,ch[root][0]); ch[x][1]=z; fa[z]=x; update (x); update (root) ;Wuyi } the voidEraseintz) { -     intX=pre (z), Y=nex (z); Splay (Y,root); Splay (x,ch[root][0]); ch[x][1]=0; fa[z]=0; update (x); update (root) ; Wu } - voidTopints) { About     intX=pos[s]; Erase (x);intY=querynum (Root,1), z=Nex (y); add (y,z,x); $ } - voidBottomints) { -     intX=pos[s]; Erase (x);intY=querynum (root,sz[root]-1), z=Nex (y); add (y,z,x); - } A voidInsertintSintt) { +     intA1=pos[s],a2=queryrank (A1) +t; Erase (A1);intA3=querynum (ROOT,A2), a4=NEX (A3); add (A3,A4,A1); the } - intAskints) {returnQueryrank (Pos[s])-1;} $ intQueryints) {returnV[querynum (root,s+1)];} the intMain () { theN=read (); M=read (); Inc (I,2, n+1) Book[i]=read (); tot=root=1; Build (Root,1, n+2); theInc (I,1, M) { the         Charopt[8]; scanf"%s", opt); -         if(opt[0]=='T'){intA=read (); top (a);} in         if(opt[0]=='B'){intA=read (); bottom (a);} the         if(opt[0]=='I'){intA=read (), b=read (); Insert (A, b);} the         if(opt[0]=='A'){intA=read (); printf"%d\n", Ask (a));} About         if(opt[0]=='Q'){intA=read (); printf"%d\n", Query (a));} the     } the     return 0; the}

20160811

Bzoj1861[zjoi2006]book Bookshelf

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.