1861: [Zjoi2006]Book 书架

Time Limit: 4 Sec  Memory Limit: 64 MB
Submit: 1396  Solved: 803
[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

[Submit][Status][Discuss]


  将用Splay按照下标来建树,另外用一个数组来记录编号为i的书在Splay中的位置(指针)。

  Top、Bottom和Insert操作就把该节点删掉,然后重新取出来,然后插到对应的位置。

  Ask操作把对应节点伸展到根,然后求左子树的大小。Query操作就求K小值就行了。

Code

 /**
* bzoj
* Problem#1861
* Accepted
* Time:1280ms
* Memory:3464k
*/
#include<iostream>
#include<fstream>
#include<sstream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cctype>
#include<cmath>
#include<algorithm>
#include<stack>
#include<queue>
#include<set>
#include<map>
#include<vector>
using namespace std;
typedef bool boolean;
#define smin(a, b) (a) = min((a), (b))
#define smax(a, b) (a) = max((a), (b))
template<typename T>
inline void readInteger(T& u){
char x;
int aFlag = ;
while(!isdigit((x = getchar())) && x != '-' && x != -);
if(x == -) return;
if(x == '-'){
x = getchar();
aFlag = -;
}
for(u = x - ''; isdigit((x = getchar())); u = (u << ) + (u << ) + x - '');
ungetc(x, stdin);
u *= aFlag;
} template<typename T>
class SplayNode{
public:
T data;
int s;
SplayNode* next[];
SplayNode* father;
SplayNode(T data, SplayNode* father):data(data), father(father), s(){
memset(next, , sizeof(next));
}
inline void maintain(){
this->s = ;
for(int i = ; i < ; i++)
if(next[i] != NULL)
s += next[i]->s;
}
int which(SplayNode* p){
return (next[] == p) ? () : ();
}
}; template<typename T>
class Splay{
protected:
inline static void rotate(SplayNode<T>*& node, int d){
SplayNode<T> *father = node->father;
SplayNode<T> *newRoot = node->next[d ^ ];
node->next[d ^ ] = newRoot->next[d];
node->father = newRoot;
newRoot->next[d] = node;
newRoot->father = father;
if(node->next[d ^ ] != NULL) node->next[d ^ ]->father = node;
if(father != NULL) father->next[father->which(node)] = newRoot;
node->maintain();
node->father->maintain();
} static SplayNode<T>* findKth(SplayNode<T>*& node, int k){
int ls = (node->next[] != NULL) ? (node->next[]->s) : ();
if(k == ls + ) return node;
if(k <= ls) return findKth(node->next[], k);
return findKth(node->next[], k - ls - );
} public:
SplayNode<T>* root; Splay():root(NULL){ } inline void splay(SplayNode<T>*& node, SplayNode<T>* father){
while(node->father != father){
SplayNode<T>* f = node->father;
int fd = f->which(node);
SplayNode<T>* ff = f->father;
if(ff == father){
rotate(f, fd ^ );
break;
}
int ffd = ff->which(f);
if(fd == ffd){
rotate(ff, ffd ^ );
rotate(f, fd ^ );
}else{
rotate(f, fd ^ );
rotate(ff, ffd ^ );
}
}
if(father == NULL)
root = node;
} inline SplayNode<T>* findKth(int k, SplayNode<T>* father){
if(root == NULL || k < || k > root->s) return NULL;
SplayNode<T>* res = findKth(root, k);
splay(res, father);
return res;
} inline void insert(T data, int index){
if(root == NULL){
root = new SplayNode<T>(data, NULL);
return;
}
SplayNode<T>* added;
if(index <= ){
findKth(, NULL);
added = root->next[] = new SplayNode<T>(data, root);
}else if(index >= root->s + ){
findKth(root->s, NULL);
added = root->next[] = new SplayNode<T>(data, root);
}else if(index == root->s){
findKth(index, NULL);
findKth(index - , root);
added = new SplayNode<T>(data, root);
added->next[] = root->next[];
root->next[]->father = added;
root->next[] = added;
}else{
findKth(index - , NULL);
findKth(index + , root);
SplayNode<T>* node = root->next[]->next[];
added = node->next[] = new SplayNode<T>(data, node);
}
splay(added, NULL);
} inline void remove(SplayNode<T>* node){
splay(node, NULL);
SplayNode<T>* maxi = node->next[];
if(maxi == NULL){
root = node->next[];
if(node->next[] != NULL) node->next[]->father = NULL;
delete node;
return;
}
while(maxi->next[] != NULL) maxi = maxi->next[];
splay(maxi, node);
maxi->next[] = node->next[];
if(node->next[] != NULL) node->next[]->father = maxi;
maxi->father = NULL;
delete node;
root = maxi;
maxi->maintain();
} }; int n, m;
Splay<int> s;
SplayNode<int>** books; inline void init(){
readInteger(n);
readInteger(m);
books = new SplayNode<int>*[(const int)(n + )];
for(int i = , a; i <= n; i++){
readInteger(a);
s.insert(a, i);
books[a] = s.root;
}
} inline void solve(){
char cmd[];
int a, b;
while(m--){
scanf("%s", cmd);
readInteger(a);
if(cmd[] == 'T'){
s.remove(books[a]);
s.insert(a, );
books[a] = s.root;
}else if(cmd[] == 'B'){
s.remove(books[a]);
s.insert(a, n);
books[a] = s.root;
}else if(cmd[] == 'I'){
readInteger(b);
if(b == ) continue;
s.splay(books[a], NULL);
int r = (s.root->next[] == NULL) ? () : (s.root->next[]->s);
s.remove(books[a]);
s.insert(a, r + + b);
books[a] = s.root;
}else if(cmd[] == 'A'){
s.splay(books[a], NULL);
int r = (s.root->next[] == NULL) ? () : (s.root->next[]->s);
printf("%d\n", r);
}else if(cmd[] == 'Q'){
SplayNode<int> *node = s.findKth(a, NULL);
printf("%d\n", node->data);
}
}
} int main(){
init();
solve();
return ;
}

[题解]bzoj 1861 Book 书架 - Splay的更多相关文章

  1. [bzoj 1861][zjoi2006] 书架

    传送门 Description 1. Top S--表示把编号为S的书放在最上面. 2. Bottom S--表示把编号为S的书放在最下面. 3. Insert S T--T∈{-1,0,1},若编号 ...

  2. BZOJ 1861书架

    小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后再拿下一本.由于这些书太有吸引 ...

  3. [BZOJ3523][Poi2014]KLO-Bricks——全网唯一 一篇O(n)题解+bzoj最优解

    Description 有n种颜色的砖块,第i种颜色的砖块有a[i]个,你需要把他们放成一排,使得相邻两个砖块的颜色不相同,限定第一个砖块的颜色是start,最后一个砖块的颜色是end,请构造出一种合 ...

  4. 洛谷 题解 P2676 【超级书架】

    题解 P2676 [超级书架] 这题就只是一个从大到小的排序而已,用"sort"函数 再用"while"判断奶牛塔的高度是否比书架高度要高 送上代码: #inc ...

  5. BZOJ 1861: [Zjoi2006]Book 书架 splay

    1861: [Zjoi2006]Book 书架 Description 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书 ...

  6. BZOJ 1861: [Zjoi2006]Book 书架 (splay)

    1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1453  Solved: 822[Submit][Stat ...

  7. BZOJ 1861: [Zjoi2006]Book 书架 | SPlay 板题

    #include<cstdio> #include<algorithm> #include<cstring> #define N 80010 #define whi ...

  8. BZOJ 1861 [Zjoi2006]Book 书架 ——Splay

    [题目分析] 模板题目. 首尾两个虚拟结点,十分方便操作. [代码] #include <cstdio> #include <cstring> #include <cma ...

  9. BZOJ 1861: [Zjoi2006]Book 书架

    1861: [Zjoi2006]Book 书架 Time Limit: 4 Sec  Memory Limit: 64 MBSubmit: 1290  Solved: 740[Submit][Stat ...

随机推荐

  1. 转:Selenium2.0 click()不生效的解决办法

    除了http://573301735.com/?p=5126讲的,昨天又发现一个让我1个小时生不如死的问题,就是使用两个不同的配置文件来初始化driver,findelement方法获取到的坐标居然不 ...

  2. aspx基础开始

    <%@ Page Language="C#" Debug="true" trace="false" validateRequest=& ...

  3. java开发第四天——莫名其妙的一天

    搞了一天的ACM,欲哭无泪,消化的不好打了一天的嗝,然后在机房睡了一个下午,感觉还真的有点对不起队友的说.别的借口我也不找了,确实是自己不努力,时至今日,一切都是我咎由自取.等这次项目一结束我就全身心 ...

  4. ural1424 Minibus

    Minibus Time limit: 1.0 secondMemory limit: 64 MB Background Minibus driver Sergey A. Greedson has b ...

  5. 超级素数(sprime)

    超级素数(sprime) 题目描述 超级素数是指一个素数,每去掉后面一个数字,总能保证剩下的数为质数,例如:373->37->3这是一个长为3的超级素数. 输入 输入一个整数n (10≤n ...

  6. NSDate,NSCalendar

    NSLog(@"%@",[NSDate date]); 打印结果: 2015-06-30 08:42:14 +0000 把它转换成2015年6月30日 8时42分14秒 怎么转? ...

  7. HDU 1754 I Hate It(线段树基础应用)

    基础线段树 #include<iostream> #include<cstdio> #include<cstring> using namespace std; # ...

  8. jQuery判断当前元素是第几个元素&获取第N个元素

    假设有下面这样一段HTML代码: <ul> <li>jQuery判断当前元素是第几个元素示例</li> <li>jQuery获取第N个元素示例</ ...

  9. bootstrap-table 表头和内容对不齐解决办法

    偶然机会学习bootstrap,表格利用bootstrap-table实现,使用bootstrap-table过程中,发现了一个非常棘手的问题,在ie浏览器中,表格的表头和内容对不齐,特别是列比较多且 ...

  10. [iOS开发]WKWebView加载JS

    最近项目要用webView加载js文件,挺同事说WKWebView比UIWebView更加好用,于是我今天就试试,百度一发,自己写了个demo. 先看我写的代码,然后再来看WKWebView跟UIWe ...