BZOJ1861: [Zjoi2006]book Bookshelf

Source: Internet
Author: User

Basic balance tree operation, Del+insert

The problem is to record the number in the balance tree and the corresponding number of the book, note that these two are not monotonous

When you write a balance tree, you are always mentally ill. Always feel the label in the balance tree is monotonous.

This is also the template is not modified before the written, constant large ...

1#include <bits/stdc++.h>2 using namespacestd;3 intRead () {4   intx=0, f=1;CharCh=GetChar ();5    while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();}6    while(ch>='0'&&ch<='9') {x=x*Ten+ch-'0'; ch=GetChar ();}7   returnx*F;8 }9 #defineN 80005Ten intn,m,a[n],pos[n],root,sz[n],ch[n][2],fa[n]; One voidPushup (intx) { Asz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1; - } - voidBuildintLintRintf) { the   intMid=l+r>>1; -sz[mid]=1; fa[mid]=F; -   if(mid<f) ch[f][0]=mid;Elsech[f][1]=mid; -   if(L==R)return; +   if(L<mid) Build (l,mid-1, mid); -   if(R>mid) Build (mid+1, r,mid); + Pushup (mid); A } at voidRotateintXint&Beto) { -   inty=fa[x],t=ch[y][1]==x; -   if(Y!=beto) ch[fa[y]][ch[fa[y]][1]==y]=x;Elsebeto=x; -fa[x]=fa[y];fa[y]=x; -fa[ch[x][!t]]=y; -ch[y][t]=ch[x][!T]; inch[x][!t]=y; - pushup (y);p ushup (x); to } + voidSplay (intXint&Beto) { -    while(x!=Beto) { the     inty=Fa[x]; *     if(y!=Beto) { $       if((ch[fa[y]][1]==y) = = (ch[y][1]==x)) Rotate (y,beto);Panax Notoginseng       Elserotate (x,beto); - }rotate (x,beto); the   } + } A intFind_k (intXintk) { the   if(sz[ch[x][0]]==k-1)returnx; +   if(sz[ch[x][0]]<k-1)returnFind_k (ch[x][1],k-sz[ch[x][0]]-1); -   returnFind_k (ch[x][0],k); $ } $ voidDelintx) { -   intL=find_k (root,x-1), R=find_k (root,x+1); -Splay (L,root); Splay (r,ch[l][1]); thex=ch[r][0];ch[r][0]=0; - pushup (R);p Ushup (l);Wuyi } the intMain () { -N=read (); M=read (); n+=2; Wu    for(intI=2; i<n;i++) A[i]=read (), pos[a[i]]=i; -Build1N0); Root= (1+n) >>1; About   Chars[Ten];intx; $    for(intI=1; i<=m;i++){ -scanf"%s", s); x=read (); -     if(s[0]=='T'){ -       intp=Pos[x],rk; ASplay (p,root); rk=sz[ch[p][0]]+1; + del (RK); the       intL=find_k (Root,1), R=find_k (Root,2); -Splay (L,root); Splay (r,ch[l][1]); $ch[r][0]=p;fa[p]=R; the pushup (R);p Ushup (l); the     } the     Else if(s[0]=='B'){ the       intp=Pos[x],rk; -Splay (p,root); rk=sz[ch[p][0]]+1; in del (RK); the       intL=find_k (root,n-2), R=find_k (root,n-1); theSplay (L,root); Splay (r,ch[l][1]); Aboutch[r][0]=p;fa[p]=R; the pushup (R);p Ushup (l); the     } the     Else if(s[0]=='I'){ +       intt=read (); -       intp=Pos[x],rk; theSplay (p,root); rk=sz[ch[p][0]]+1;Bayi del (RK); the       intL=find_k (root,rk+t-1), R=find_k (root,rk+t); theSplay (L,root); Splay (r,ch[l][1]); -ch[r][0]=p;fa[p]=R; - pushup (R);p Ushup (l); the     } the     Else if(s[0]=='A'){ the       intp=Pos[x],rk; the     //cout<< "P" <<p<<endl; -Splay (p,root); rk=sz[ch[p][0]]+1; the     //cout<< "RK" <<rk<<endl; theprintf"%d\n", rk-2); the     }94     Else if(s[0]=='Q'){ the       intP=find_k (root,x+1); theprintf"%d\n", A[p]); the     }98   } About   return 0; -}
View Code 1861: [Zjoi2006]book Bookshelf time limit:4 Sec Memory limit:64 MB
submit:1183 solved:689
[Submit] [Status] [Discuss] Description

Little T has a big bookcase. The structure of this bookcase is somewhat unique, that is, the book in the bookcase is stacked from top to bottom into a column. She numbered every book with a positive integer of 1 to N. Small T in reading, every time take out a book, after reading and put back to the bookcase and then take a copy. Because these books are so attractive, she often forgets what position they were placed on the bookcase after reading it. But the memory of little T is very good, so at least every time when the book can be placed in the place where the book is taken out, for example, when she took the book has an x book, then put back when the book can only be X-1, X or x+1 book. Of course, there are special circumstances, such as when the phone rang when the book suddenly or have friends to visit. This time the careless little T will conveniently put the book in the bookcase in the top or bottom of all the books, and then turned away. Over time, the order of the books in Little T's bookcase will become more and more chaotic, and finding a specific numbered book becomes more and more difficult. So she wants you to help her write a book management program, handle her reading some of the operation, as well as answer her two questions: (1) The book is numbered x where the bookcase, (2) from the top to the next I book number is how much.

Input

The first line has two number n,m, respectively, the number of books and the number of orders, and the second behavior n positive integers: The number of number I represents the initial time from the top to the bottom of the book placed in the first position, the third line to the m+2 line, one command per line. There are 5 forms of the command: 1. Top s--says the study with number S is on the top. 2. Bottom s--says the study with the number S is at the bottom. 3. Insert s t--t∈{-1,0,1}, if there is an X book on the book numbered S, this command means that the book is put back and that it has a x+t book on it; 4. Ask s--asked how many books are currently on the top of the book numbered S. 5. Query s--asks for the number of book S from above.

Output

For each ask or query statement you should output a line, a number, representing the answer to the query.

Sample Input10 10
1 3 2 7 5 8 10 4 9 6
Query 3
Top 5
Ask 6
Bottom 3
Ask 3
Top 6
Insert 4-1
Query 5
Query 2
Ask 2Sample Output2
9
9
7
5
3HINT

Data range


100% of data, n,m < = 80000

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.