BZOJ1861: [Zjoi2006]book Bookshelf Splay

Source: Internet
Author: User
Tags printf time limit
BZOJ1861: [Zjoi2006]book BookshelfTime Limit:4 Sec Memory limit:64 MB submit:1302 solved:747 [Submit][status][discuss] Puzzle : SplayThere are not many things to say about the two types of inquiries. For the remaining several modifications, essentially the same, first of all to determine if the insert and t==0 then continue and then the X-point from the tree, the specific removal method is to the interval [x,x]splay out and then ls[x+1] disconnect and then insert, To find the location of the interval [x,x+1]splay out, at this time ls[x+1] is empty, and then directly to the node to X+1 can be (in fact, for bottom and top can simplify some code see)
#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include <
Algorithm> using namespace std;
const int n=100005;
int ls[n],rs[n],siz[n],fa[n],a[n],n,m,root,s,t;
Char c[11];
	void pushup (int x) {siz[x]=siz[ls[x]]+siz[rs[x]]+1;} void Print () {cout<< "root=" <<root<<endl;
for (int i=0;i<=n+2;i++) printf ("i=%d ls=%d rs=%d fa=%d siz=%d\n", I,ls[i],rs[i],fa[i],siz[i]);
    } void Rotate (int x,int &k) {int y=fa[x],z=fa[y];
    if (k==y) k=x;
        else {if (y==ls[z]) ls[z]=x;
    else rs[z]=x;
    } fa[x]=z;
    Fa[y]=x;
        if (ls[y]==x) {ls[y]=rs[x];
        Fa[rs[x]]=y;
    Rs[x]=y;
        } else {rs[y]=ls[x];
        Fa[ls[x]]=y;
    Ls[x]=y;
    } pushup (y);
Pushup (x);
		} void splay (int x,int &k) {while (x!=k) {int y=fa[x],z=fa[y];
			if (y!=k) {if (ls[z]==y^ls[y]==x) Rotate (x,k);
		else Rotate (y,k);
	} Rotate (X,k); }} void Build (int l,inT R,int f) {if (l>r) return;
		if (l==r) {siz[a[l]]=1;
		FA[A[L]]=A[F];
		if (l<f) ls[a[f]]=a[l];
		else Rs[a[f]]=a[l];
	Return
	} int mid= (L+R) >>1;
	Build (L,mid-1,mid);
	Build (Mid+1,r,mid);
	Pushup (A[mid]);
	FA[A[MID]]=A[F];
	if (mid<f) Ls[a[f]]=a[mid];
else Rs[a[f]]=a[mid]; 
} int tot;
	int getnum (int x,int rank) {if (rank==siz[ls[x]]+1) return x;
	else if (Rank<=siz[ls[x]]) return Getnum (Ls[x],rank);
else return Getnum (rs[x],rank-siz[ls[x]]-1);
	} int GetRank (int x) {splay (x,root);
return siz[ls[x]]+1;
	} void Del (int x) {int tmp=getrank (x);
	int Y=getnum (root,tmp-1), Z=getnum (root,tmp+1);
	Splay (Y,root);
	Splay (Z,rs[y]);
	ls[z]=fa[x]=0;
Pushup (z);
	} void Ins (int nu,int x) {int u=getnum (ROOT,NU);
	int V=getnum (root,nu+1);
	Splay (U,root);
	Splay (V,rs[u]);
	Ls[v]=x;
	Fa[x]=v;
Pushup (v);
	} void Top (int x) {int u=getnum (root,2);
	Splay (U,root);
	Fa[s]=x,ls[x]=s;
	Fa[x]=u,ls[u]=x;
Pushup (S), Pushup (x), Pushup (U);
	} void Bot (int x) {int u=getnum (root,n); SPlay (U,root);
	fa[t]=x,rs[x]=t;
	Fa[x]=u,rs[u]=x;
Pushup (T), Pushup (x), Pushup (U);
	} int main () {scanf ("%d%d", &n,&m);
	for (int i=2;i<=n+1;i++) scanf ("%d", &a[i]);
	s=n+1,t=n+2;
	a[1]=s,a[n+2]=t;
	root=a[(n+3) >>1];
	Build (1,n+2,0);
	int x,y,u,v;
		while (m--) {scanf ("%s%d", c,&x);
		if (c[0]== ' Q ') printf ("%d\n", Getnum (root,x+1));
		else if (c[0]== ' A ') printf ("%d\n", GetRank (x)-2);
			else if (c[0]== ' I ') {scanf ("%d", &y);
			if (y==0) continue;
			int Tmp=getrank (x);
			Del (x);
			if (y==-1) Ins (tmp-2,x);
		if (y==1) Ins (tmp,x);
			} else {Del (x);
			if (c[0]== ' T ') Top (x);
		if (c[0]== ' B ') Bot (x);
 }
	}
}


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 Input 10 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 2 Sample Output 2
9
9
7
5
3 HINT

Data range
100% of data, n,m < = 80000

Source

Day2 [Submit][status][discuss]

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.