您的位置:首页 > 其它

BZOJ 1861: [Zjoi2006]Book 书架

2016-12-09 22:05 387 查看

1861: [Zjoi2006]Book 书架

Time Limit: 4 Sec Memory Limit: 64 MB
Submit: 1290 Solved: 740
[Submit][Status][Discuss]

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

HINT

数据范围

100%的数据,n,m < = 80000

Source

Day2

[Submit][Status][Discuss]

小生的又一道Splay模板题,用伸展树模拟序列,并在外面用一个指针数组记录各个编号所在的结点即可。

#include <bits/stdc++.h>

struct node {
int size, value;
node *father, *son[2];
node (int v = 0, node *f = 0) {
size = 1; value = v; father = f; son[0] = son[1] = 0;
}
}*root = NULL, *to[80005];

inline int size(node *t) {
return t ? t->size : 0;
}

inline bool son(node *f, node *s) {
return f && f->son[1] == s;
}

inline void connect(node *f, node *s, bool k) {
if (f)f->son[k] = s; else root = s;
if (s)s->father = f;
}

inline void update(node *t) {
if (t)t->size = 1 + size(t->son[0]) + size(t->son[1]);
}

inline void rotate(node *t) {
node *f = t->father;
node *g = f->father;
bool k = son(f, t);
connect(f, t->son[!k], k);
connect(g, t, son(g, f));
connect(t, f, !k);
update(f);
update(t);
}

inline void splay(node *t, node *p) {
while (t && t->father != p) {
node *f = t->father;
node *g = f->father;
if (g == p)rotate(t);
else rotate(son(g, f)^son(f, t) ? t : f), rotate(t);
}
}

node *top(node *t) {
return t && t->son[0] ? top(t->son[0]) : t;
}

node *bot(node *t) {
return t && t->son[1] ? bot(t->son[1]) : t;
}

inline void insert_l(int val) {
splay(top(root), 0); connect(root, new node(val), 0); update(root);
}

inline void insert_r(int val) {
splay(bot(root), 0); connect(root, new node(val), 1); update(root);
}

inline void remove(node *t) {
splay(t, 0); splay(bot(t->son[0]), t);
connect(0, t->son[0], 0);connect(root, t->son[1], 1); update(root);
}

inline int rnk(int k) {
for (node *t = root; t; ) {
if (size(t->son[0]) < k) {
if ((k -= size(t->son[0])) == 1)return t->value;
else --k, t = t->son[1];
} else t = t->son[0];
}
}

inline void swap(int a, int k) { if (!k)return;
splay(to[a], 0); int b = (k>0 ? top(root->son[1]) : bot(root->son[0]))->value;
splay(to[b], root); std::swap(to[a], to[b]); std::swap(to[a]->value, to[b]->value);
}

int n, m, a, b; char s[50];

signed main(void) {
scanf("%d%d", &n, &m);
for (int i = 1; i <= n; ++i)
scanf("%d", &a), insert_r(a), to[a] = bot(root);
for (int i = 1; i <= m; ++i) {
scanf("%s%d", s, &a); switch (s[0]) {
case 'Q': printf("%d\n", rnk(a)); break;
case 'I': scanf("%d", &b); swap(a, b); break;
case 'T': remove(to[a]); insert_l(a); to[a] = top(root); break;
case 'B': remove(to[a]); insert_r(a); to[a] = bot(root); break;
case 'A': splay(to[a], 0); printf("%d\n", size(root->son[0])); break;
}
}
}


@Author: YouSiki
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: