bzoj1861: [Zjoi2006]Book 书架
Description
小T有一个很大的书柜。这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列。她用1到n的正整数给每本书都编了号。 小T在看书的时候,每次取出一本书,看完后放回书柜然后再拿下一本。由于这些书太有吸引力了,所以她看完后常常会忘记原来是放在书柜的什么位置。不过小T的记忆力是非常好的,所以每次放书的时候至少能够将那本书放在拿出来时的位置附近,比如说她拿的时候这本书上面有X本书,那么放回去时这本书上面就只可能有X-1、X或X+1本书。 当然也有特殊情况,比如在看书的时候突然电话响了或者有朋友来访。这时候粗心的小T会随手把书放在书柜里所有书的最上面或者最下面,然后转身离开。 久而久之,小T的书柜里的书的顺序就会越来越乱,找到特定的编号的书就变得越来越困难。于是她想请你帮她编写一个图书管理程序,处理她看书时的一些操作,以及回答她的两个提问:(1)编号为X的书在书柜的什么位置;(2)从上到下第i本书的编号是多少。
Input
第一行有两个数n,m,分别表示书的个数以及命令的条数;第二行为n个正整数:第i个数表示初始时从上至下第i个位置放置的书的编号;第三行到m+2行,每行一条命令。命令有5种形式: 1. Top S——表示把编号为S的书房在最上面。 2. Bottom S——表示把编号为S的书房在最下面。 3. Insert S T——T∈{-1,0,1},若编号为S的书上面有X本书,则这条命令表示把这本书放回去后它的上面有X+T本书; 4. Ask S——询问编号为S的书的上面目前有多少本书。 5. Query S——询问从上面数起的第S本书的编号。
Output
对于每一条Ask或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
题解
突然发现这道裸题
对于位置操作什么的写过bzoj1500的我已经无所畏惧
注意一下X+-1的移动顺序就好了
代码
#include<iostream>
#include<cstdlib>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#include<cmath>
using namespace std;
const int N = 220000, inf = 80001;
int read() {
char ch = getchar(); int x = 0, f = 1;
while(ch < '0' || ch > '9') {if(ch == '-') f = -1; ch = getchar();}
while(ch >= '0' && ch <= '9') {x = x * 10 + ch - '0'; ch = getchar();}
return x * f;
}
int ch[N][2], siz[N], fa[N], pos[N], a[N], val[N], root, sz, n, m;
int wh(int p) {return ch[fa[p]][1] == p;}
void update(int p) {siz[p] = siz[ch[p][0]] + siz[ch[p][1]] + 1;}
void Rotate(int p) {
int f = fa[p], g = fa[f], c = wh(p);
if(g) ch[g][wh(f)] = p; fa[p] = g;
ch[f][c] = ch[p][c ^ 1]; if(ch[f][c]) fa[ch[f][c]] = f;
ch[p][c ^ 1] = f; fa[f] = p;
update(f);
}
void Splay(int p, int tar) {
for(;fa[p] != tar; Rotate(p))
if(fa[fa[p]] != tar) Rotate(wh(p) == wh(fa[p]) ? fa[p] : p);
update(p);
if(!tar) root = p;
}
void Build(int &p, int L, int R, int f) {
if(!p) p = ++sz;
fa[p] = f; int mid = L + R >> 1;
val[p] = a[mid]; siz[p] = 1; pos[a[mid]] = p;
if(L < mid) Build(ch[p][0], L, mid - 1, p);
if(R > mid) Build(ch[p][1], mid + 1, R, p);
update(p);
}
int Kth(int k) {
int p = root, lsize = 0;
while(p) {
int cur = lsize + siz[ch[p][0]];
if(k == cur + 1) return p;
if(k <= cur) p = ch[p][0];
else {
lsize = cur + 1;
p = ch[p][1];
}
}
}
int Ins(int k, int S) {
int f = Kth(k + 1), p = Kth(k + 2);
Splay(f, 0); Splay(p, f);
ch[p][0] = ++sz; siz[ch[p][0]] = 1; val[ch[p][0]] = S; fa[ch[p][0]] = p;
update(p); update(f);
return sz;
}
void Del(int k) {
int f = Kth(k - 1), p = Kth(k + 1);
Splay(f, 0); Splay(p, f);
ch[p][0] = 0; update(p); update(f);
}
void print(int p) {
if(ch[p][0]) print(ch[p][0]);
cout<<val[p]<<" "<<siz[p]<<" "<<fa[p]<<endl;
if(ch[p][1]) print(ch[p][1]);
}
int main() {
n = read(); m = read();
for(int i = 2;i <= n + 1; ++i) a[i] = read(); a[1] = inf; a[n + 2] = inf + 1;
Build(root, 1, n + 2, 0);
while(m--) {
char opt = getchar();
while(opt < 'A' || opt > 'Z') opt = getchar(); int S = read();
if(opt == 'A') {Splay(pos[S], 0); printf("%d\n", siz[ch[root][0]] - 1);}
else if(opt == 'Q') printf("%d\n", val[Kth(S + 1)]);
else {
Splay(pos[S], 0); int rank = siz[ch[root][0]];
Del(rank + 1);
if(opt == 'T') pos[S] = Ins(0, S);
if(opt == 'B') pos[S] = Ins(n - 1, S);
if(opt == 'I') pos[S] = Ins(rank + read() - 1, S);
}
}
return 0;
}