BZOJ1861:Book 书架(splay)

1861: [Zjoi2006]Book 书架

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

思路:每一个操作都可以看成区间区间翻转操作,比如TOP X,那么先将1~X-1翻转,再将1~X翻转就行;其余同理。

# include <iostream>
# include <cstdio>
# include <cstring>
# include <algorithm>
# include <cstdlib>
using namespace std;
const int maxn = 8e4+30;
const int inf = 2e9+7;
int n;
struct node{
    int val, lazy, siz;
    node *fa, *son[2];
}*root, tree[maxn], *stk[maxn], *id[maxn];
int top;
node* newnode(int val, node* fa){//新建节点
    node* t = stk[top--];
    t->val = val;
    t->fa = fa;
    t->lazy = 0;
    t->siz = 1;
    t->son[0] = t->son[1] = NULL;
    return t;
}
void freenode(node* t){//释放节点
    stk[++top] = t;
}
int size(node* t){//返回子树大小
    return t==NULL?0:t->siz;
}
void update(node* t){//更新子树大小
    if(t != NULL){
        t->siz = 1;
        t->siz += size(t->son[0]);
        t->siz += size(t->son[1]);
    }
}
inline void pushdown(node* t){//下传标记
    if(t!=NULL && t->lazy){
        swap(t->son[0], t->son[1]);
        t->lazy = 0;
        if(t->son[0] != NULL) t->son[0]->lazy ^= 1;
        if(t->son[1] != NULL) t->son[1]->lazy ^= 1;
    }
}
bool son(node* f, node* s){//判断s是f的哪个儿子
    if(f == NULL) return 0;
    return f->son[1] == s;
}
void connect(node* f, node* s, bool k){//将s将到f的k儿子上
    if(f != NULL) f->son[k] = s;
    else root = s;
    if(s != NULL) s->fa = f;
}
void rotate(node* s){//将s转到它父亲的位置
    node* f = s->fa;
    node* g = f->fa;
    bool a = son(f, s), b = !a;
    connect(f, s->son[b], a);
    connect(g, s, son(g, f));
    connect(s, f, b);
    update(f);
    update(s);
}
void splay(node* s, node* p){//将s成p的儿子
    if(s == NULL) return;
    while(s->fa != p){
        node* f = s->fa;
        node* g = f->fa;
        pushdown(g);
        pushdown(f);
        pushdown(s);
        if(g == p) rotate(s);
        else{
            if(son(g,f) ^ son(f,s)) rotate(f), rotate(s);
            else rotate(s), rotate(s);
        }
    }
}
node* insert(node* t, int val){//插入一个新的数
    node* res = t->son[1];
    if(res == NULL) res = t->son[1] = newnode(val, t);
    else res = insert(res, val);
    return res;
}
node* insert(int val){//插入一个数
    if(root == NULL)
        id[val] = root = newnode(val, NULL);
    else {
        id[val] = insert(root, val);
        splay(id[val], NULL);
    }
}
node* qry(int th, node* t){//查询排在第th的是哪个点
    pushdown(t);
    if(size(t->son[0]) == th) return t;
    else if(size(t->son[0]) > th) return qry(th, t->son[0]);
    else return qry(th-size(t->son[0])-1, t->son[1]);
}
node* nxt(node* t){//t的下一个点
    pushdown(t);
    splay(t, NULL);
    node* p = t->son[1];
    pushdown(p);
    while(p->son[0]!=NULL) p = p->son[0],pushdown(p);
    return p;
}
node* pre(node* t){//t的前一个点
    pushdown(t);
    splay(t, NULL);
    node* p =t->son[0];
    pushdown(p);
    while(p->son[1] != NULL) p = p->son[1],pushdown(p);
    return p;
}
int main(){
    int m, x, y;
    char s[10];
    scanf("%d%d",&n,&m);
    top = n+10; root = NULL;
    for(int i=1; i<=top; ++i) stk[i] = tree+i;//将节点放进栈里面
    insert(0);
    for(int i=1; i<=n; ++i){
        scanf("%d",&x);
        insert(x);
    }
    insert(n+1);
    while(m--){
        scanf("%s",s);
        if(s[0] == 'T'){
            scanf("%d",&x);
            node* tmp = nxt(id[x]);
            splay(id[x], NULL);
            splay(id[0], root);
            if(root->son[0]->son[1] != NULL) root->son[0]->son[1]->lazy ^= 1;
            splay(tmp, NULL);
            splay(id[0], root);
            if(root->son[0]->son[1] != NULL) root->son[0]->son[1]->lazy ^= 1;
        }
        else if(s[0] == 'B'){
            scanf("%d",&x);
            node* tmp = pre(id[x]);
            splay(id[x], NULL);
            splay(id[n+1], root);
            if(root->son[1]->son[0] != NULL) root->son[1]->son[0]->lazy ^= 1;
            splay(tmp, NULL);
            splay(id[n+1], root);
            if(root->son[1]->son[0] != NULL) root->son[1]->son[0]->lazy ^= 1;
        }
        else if(s[0] == 'I'){
            scanf("%d%d",&x,&y);
            if(y == 0) continue;
            else if(y == -1){
                node* tmp = nxt(id[x]);
                node* tmp2 = pre(pre(id[x]));
                splay(tmp, NULL);
                splay(tmp2, root);
                if(root->son[0]->son[1] != NULL) root->son[0]->son[1]->lazy ^= 1;
            }
            else{
                node* tmp = nxt(nxt(id[x]));
                node* tmp2 = pre(id[x]);
                splay(tmp, NULL);
                splay(tmp2, root);
                if(root->son[0]->son[1] != NULL) root->son[0]->son[1]->lazy ^= 1;
            }
        }
        else if(s[0] == 'A'){
            scanf("%d",&x);
            splay(id[x], NULL);
            printf("%d\n",size(root->son[0])-1);
        }
        else{
            scanf("%d",&x);
            printf("%d\n",qry(x, root)->val);
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值