zoukankan      html  css  js  c++  java
  • 【HYSBZ】1503 郁闷的出纳员

      1 #include<cstdio>
      2 #define INF 0x7FFFFFFF
      3 #define MAXN 100010
      4 struct SplayTree {
      5     int num[MAXN], next[MAXN][2], pre[MAXN], key[MAXN];
      6     int root, size;
      7     inline void PushUp(int x) {
      8         num[x] = num[next[x][0]] + num[next[x][1]] + 1;
      9     }
     10     inline void Rotate(int x, int kind) {
     11         int y, z;
     12         y = pre[x];
     13         z = pre[y];
     14         next[y][!kind] = next[x][kind];
     15         pre[next[x][kind]] = y;
     16         next[z][next[z][1] == y] = x;
     17         pre[x] = z;
     18         next[x][kind] = y;
     19         pre[y] = x;
     20         PushUp(y);
     21         PushUp(x);
     22     }
     23     void Splay(int x, int goal) {
     24         if (x != goal) {
     25             while (pre[x] != goal) {
     26                 if (next[pre[x]][0] == x)
     27                     Rotate(x, 1);
     28                 else
     29                     Rotate(x, 0);
     30             }
     31             if (!goal)
     32                 root = x;
     33         }
     34     }
     35     inline void NewNode(int &x, int y, int val) {
     36         x = ++size;
     37         num[x] = 1;
     38         pre[x] = y;
     39         next[x][0] = next[x][1] = 0;
     40         key[x] = val;
     41         next[y][val > key[y]] = x;
     42     }
     43     void Insert(int val) {
     44         int x, y;
     45         for (x = root, y = 0; x; x = next[x][val > key[x]]) {
     46             y = x;
     47             num[x]++;
     48         }
     49         NewNode(x, y, val);
     50         Splay(x, 0);
     51     }
     52     int Search(int val) {
     53         int res, x;
     54         for (res = 0, x = root; x; x = next[x][val > key[x]]) {
     55             if (key[x] >= val && key[res] >= key[x])
     56                 res = x;
     57         }
     58         return res;
     59     }
     60     int Select(int k) {
     61         int x;
     62         k = num[root] - k;
     63         for (x = root; num[next[x][0]] + 1 != k;) {
     64             if (num[next[x][0]] + 1 < k) {
     65                 k -= num[next[x][0]] + 1;
     66                 x = next[x][1];
     67             } else
     68                 x = next[x][0];
     69         }
     70         Splay(x, 0);
     71         return key[x];
     72     }
     73     inline void Init() {
     74         root = size = 0;
     75         num[0] = next[0][0] = next[0][1] = pre[0];
     76         key[0] = INF;
     77         Insert(INF);
     78     }
     79 } tree;
     80 int main() {
     81     char ch;
     82     int q, base, diff, ans, x;
     83     while (~scanf("%d%d", &q, &base)) {
     84         tree.Init();
     85         diff = ans = 0;
     86         while (q--) {
     87             scanf(" %c%d", &ch, &x);
     88             if (ch == 'I') {
     89                 if (x >= base)
     90                     tree.Insert(x - base + diff);
     91             } else if (ch == 'A')
     92                 diff -= x;
     93             else if (ch == 'S') {
     94                 diff += x;
     95                 tree.Splay(tree.Search(diff), 0);
     96                 ans += tree.num[tree.next[tree.root][0]];
     97                 tree.num[tree.root] -= tree.num[tree.next[tree.root][0]];
     98                 tree.next[tree.root][0] = 0;
     99             } else {
    100                 if (x >= tree.num[tree.root])
    101                     puts("-1");
    102                 else
    103                     printf("%d\n", tree.Select(x) + base - diff);
    104             }
    105         }
    106         printf("%d\n", ans);
    107     }
    108     return 0;
    109 }
  • 相关阅读:
    docker数据卷
    docker容器的启动、停止、运行、导入、导出、删除
    docker镜像的获取、创建、修改、删除、导入操作
    docker使用-spark安装
    python爬虫-3 解析库
    python爬虫-2 requests使用
    NLP-HMM
    NLP-中文分词-预处理
    python爬虫-1环境安装
    学习笔记3
  • 原文地址:https://www.cnblogs.com/DrunBee/p/2635364.html
Copyright © 2011-2022 走看看