BZOJ1861: [Zjoi2006]Book shelf

BZOJ1861: [Zjoi2006]Book shelf

Description

Little T has a large bookcase. The structure of this bookcase is somewhat unique, that is, the books in the bookcase are stacked in a row from top to bottom.

She numbered each book with a positive integer from 1 to n. When Little T was reading, he took out one book at a time, put it back on the bookcase after reading it, and then took the next one.

Because these books are so attractive, she often forgets where they were on the bookcase after reading them.

However, Xiao T's memory is very good, so every time she puts the book, she can at least place the book near the position where it was taken out. Then this book can only have X-1, X, or X+1 books on it.

Of course, there are also special circumstances, such as when the phone rang suddenly while reading a book or a friend came to visit.

At this time, the careless little T will casually put the book on the top or bottom of all the books in the bookcase, and then turn around and leave.

Over time, the order of books in Little T's bookcase will become more and more disordered, and it will become more and more difficult to find a specific numbered book.

So she wants to ask you to help her write a library management program to handle some of her reading operations, and to answer her two questions: (1) where is the book numbered X in the bookcase; (2) from top to bottom What is the number of the i-th book.

Input

The first line has two numbers n and m, which represent the number of books and the number of commands respectively;

The second row is n positive integers: the ith number represents the number of the book placed at the ith position from top to bottom at the beginning;

Lines 3 to m+2, one command per line. Commands come in 5 forms:

1. Top S - Indicates that the study numbered S is on the top.

2. Bottom S - Indicates that the study numbered S is at the bottom.

3. Insert ST——T∈{-1,0,1}, if the book numbered S has X book on it, then this command means that after the book is put back, it has X+T book on it;

4. Ask S - Asks how many books are currently above the book numbered S.

5. Query S - Query the number of book S from above.

Output

For each Ask or Query statement you should output one 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% data: n,m <= 80000

Problem solving Here!
Splay is easier to do, so let's do Splay.
1. Top and Bottom are essentially putting a number in the sequence to the first and last.
The specific implementation can refer the node to be operated to the root node, and then merge the left subtree into the right subtree or merge the right subtree into the left subtree.

2. The Insert operation is to exchange the position of the node to be operated and the predecessor and successor, that is, exchange the eigenvalues ​​of the two points, that is, the numbers.

3. The Ask operation asks how many numbers are in front of this node in the sequence. It is enough to mention that the root node outputs the size of the left child.

4. The Query operation is simply the number of the s-th number in the query sequence, which is equivalent to querying the k-th largest, kth.

Since we need to locate the position of the node numbered s in the tree, I use the id array to record and maintain.

Attached code:

#include<iostream>
#include<algorithm>
#include<cstdio>
#define MAXN 80010
using namespace std;
int n,m,root=1,c=1,id[MAXN];
struct node{
	int son[2];
	int f,v,s;
} a [MAXN];
inline int read(){
	int date=0,w=1;char c=0;
	while(c<'0'||c>'9'){if(c=='-')w=-1;c=getchar();}
	while(c>='0'&&c<='9'){date=date*10+c-'0';c=getchar();}
	return date*w;
}
inline void clean(int rt){
	a[rt].son[0]=a[rt].son[1]=a[rt].f=a[rt].v=a[rt].s=0;
}
inline void pushup(int rt){
	if(!rt)return;
	a[rt].s=a[a[rt].son[0]].s+a[a[rt].son[1]].s+1;
	id[a[a[rt].son[0]].v]=a[rt].son[0];id[a[a[rt].son[1]].v]=a[rt].son[1];
}
inline void turn(int rt,int k){
	int x=a[rt].f,y=a[x].f;
	a[x].son[k^1]=a[rt].son[k];
	if(a[rt].son[k])a[a[rt].son[k]].f=x;
	a[rt].f=y;
	if(y)a[y].are[a[y].are[1]==x]=rt;
	a[x].f=rt;
	a[rt].son[k]=x;
	pushup(x);pushup(rt);
}
void splay(int rt,int ancestry){
	while(a[rt].f!=ancestry){
		int x=a[rt].f,y=a[x].f;
		if(y==ancestry)turn(rt,a[x].son[0]==rt);
		else{
			int k=a[y].are[0]==x?1:0;
			if(a[x].son[k]==rt){turn(rt,k^1);turn(rt,k);}
			else{turn(x,k);turn(rt,k);}
		}
	}
	if(ancestry==0)root=rt;
}
int kth(int rt,int x){
	int y=a[rt].son[0];
	if(a[y].s+1==x)return rt;
	else if(x<=a[y].s)return kth(y,x);
	else return kth(a[rt].son[1],x-a[y].s-1);
}
inline void newnode(int x){
	int rt=c++;
	a[rt].son[0]=a[rt].son[1]=0;
	a[rt].v=x;a[rt].s=1;id[x]=rt;
	if(rt==1)return;
	a[rt-1].son[1]=rt;a[rt].f=rt-1;
	splay(rt,0);
}
inline void top(int x){
	int rt=id[x];
	splay(rt,0);
	if(!a[rt].son[0])return;
	if(!a[rt].son[1]){
		a[rt].son[1]=a[rt].son[0];
		a[rt].son[0]=0;
	}
	else{
		int y=kth(root,a[a[rt].son[0]].s+2);
		a[a[root].are[0]].f=y;
		a[y].are[0]=a[root].are[0];
		a[root].son[0]=0;
		splay(y,0);
	}
}
inline void bottom(int x){
	int rt=id[x];
	splay(rt,0);
	if(!a[rt].son[1])return;
	if(!a[rt].son[0]){
		a[rt].son[0]=a[rt].son[1];
		a[rt].son[1]=0;
	}
	else{
		int y=kth(root,a[a[rt].son[0]].s);
		a[a[root].are[1]].f=y;
		a[y].are[1]=a[root].are[1];
		a[root].son[1]=0;
		splay(y,0);
	}
}
inline void insert(int x,int y){
	if(!y)return;
	splay(id[x],0);
	int p,q,k=kth(root,a[a[id[x]].son[0]].s+y+1);
	p=a[k].v;q=id[x];
	swap(id[x],id[p]);swap(a[q].v,a[k].v);
}
inline int ask(int x){
	x=id[x];
	splay(x,0);
	return a[a[x].son[0]].s;
}
int main(){
	char ch[10];
	int x,y;
	n=read();m=read();
	for(int i=1;i<=n;i++){
		x=read();
		newnode(x);
	}
	while(m--){
		scanf("%s",ch);x=read();
		switch(ch[0]){
			case 'T':top(x);break;
			case 'B':bottom(x);break;
			case 'I':y=read();insert(x,y);break;
			case 'A':printf("%d\n",ask(x));break;
			case 'Q':printf("%d\n",a[kth(root,x)].v);break;
		}
	}
	return 0;
}

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325222929&siteId=291194637