zoukankan      html  css  js  c++  java
  • BZOJ 1861: [Zjoi2006]Book 书架

    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

    [Submit][Status][Discuss]

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

     1 #include <bits/stdc++.h>
     2 
     3 struct node {
     4     int size, value;
     5     node *father, *son[2];
     6     node (int v = 0, node *f = 0) {
     7         size = 1; value = v; father = f; son[0] = son[1] = 0;
     8     }
     9 }*root = NULL, *to[80005];
    10 
    11 inline int size(node *t) { 
    12     return t ? t->size : 0;
    13 }
    14 
    15 inline bool son(node *f, node *s) {
    16     return f && f->son[1] == s;
    17 }
    18 
    19 inline void connect(node *f, node *s, bool k) {
    20     if (f)f->son[k] = s; else root = s;
    21     if (s)s->father = f;
    22 }
    23 
    24 inline void update(node *t) {
    25     if (t)t->size = 1 + size(t->son[0]) + size(t->son[1]);
    26 }
    27 
    28 inline void rotate(node *t) {
    29     node *f = t->father;
    30     node *g = f->father;
    31     bool k = son(f, t);
    32     connect(f, t->son[!k], k);
    33     connect(g, t, son(g, f));
    34     connect(t, f, !k);
    35     update(f);
    36     update(t);
    37 }
    38 
    39 inline void splay(node *t, node *p) {
    40     while (t && t->father != p) {
    41         node *f = t->father;
    42         node *g = f->father;
    43         if (g == p)rotate(t);
    44         else rotate(son(g, f)^son(f, t) ? t : f), rotate(t);
    45     }
    46 }
    47 
    48 node *top(node *t) {
    49     return t && t->son[0] ? top(t->son[0]) : t;
    50 }
    51 
    52 node *bot(node *t) {
    53     return t && t->son[1] ? bot(t->son[1]) : t;
    54 }
    55 
    56 inline void insert_l(int val) {
    57     splay(top(root), 0); connect(root, new node(val), 0); update(root);
    58 }
    59 
    60 inline void insert_r(int val) { 
    61     splay(bot(root), 0); connect(root, new node(val), 1); update(root);
    62 }
    63 
    64 inline void remove(node *t) {
    65     splay(t, 0); splay(bot(t->son[0]), t);
    66     connect(0, t->son[0], 0);connect(root, t->son[1], 1); update(root);
    67 }
    68 
    69 inline int rnk(int k) {
    70     for (node *t = root; t; ) {
    71         if (size(t->son[0]) < k) {
    72             if ((k -= size(t->son[0])) == 1)return t->value;
    73             else --k, t = t->son[1];
    74         } else t = t->son[0];
    75     }
    76 }
    77 
    78 inline void swap(int a, int k) { if (!k)return;
    79     splay(to[a], 0); int b = (k>0 ? top(root->son[1]) : bot(root->son[0]))->value;
    80     splay(to[b], root); std::swap(to[a], to[b]); std::swap(to[a]->value, to[b]->value);
    81 }
    82 
    83 int n, m, a, b; char s[50];
    84 
    85 signed main(void) {
    86     scanf("%d%d", &n, &m);
    87     for (int i = 1; i <= n; ++i)
    88         scanf("%d", &a), insert_r(a), to[a] = bot(root);
    89     for (int i = 1; i <= m; ++i) {
    90         scanf("%s%d", s, &a); switch (s[0]) {
    91             case 'Q': printf("%d
    ", rnk(a)); break;
    92             case 'I': scanf("%d", &b); swap(a, b); break;
    93             case 'T': remove(to[a]); insert_l(a); to[a] = top(root); break; 
    94             case 'B': remove(to[a]); insert_r(a); to[a] = bot(root); break;
    95             case 'A': splay(to[a], 0); printf("%d
    ", size(root->son[0])); break;
    96         }
    97     }
    98 }

    @Author: YouSiki

  • 相关阅读:
    人脸识别总结(附开源项目代码与各大数据集下载路径)
    simpledet 的配置
    论文笔记--PCN:Real-Time Rotation-Invariant Face Detection with Progressive Calibration Networks
    smallcorgi/Faster-RCNN_TF训练自己的数据
    保存文件名至txt文件中,不含后缀
    训练 smallcorgi/Faster-RCNN_TF 模型(附ImageNet model百度云下载地址)
    调试 smallcorgi/Faster-RCNN_TF 的demo过程遇到的问题
    python字符串前缀和格式化
    摩斯电码与字母相互转换
    django配置mysql
  • 原文地址:https://www.cnblogs.com/yousiki/p/6151705.html
Copyright © 2011-2022 走看看