zoukankan      html  css  js  c++  java
  • [NOI2004]郁闷的出纳员

    嘟嘟嘟


    (splay)我又来了


    1.插入结点:正常操作,不说了。
    1.全局加减:单开一个变量表示变化量即可,不用区间修改打(lazy)标记。
    2.删除小于(x)的数:找(x)的后继(包括自己),然后把(x)旋到根,删除左子树即可。
    3.查询第(k)大的数:如果(k)小于右子树大小,到右子树去找;否则如果(k)小于右子树大小加上当前节点大小(size),直接返回当前节点权值;否则(k -= size),去左子树找。
    主要思路就是这些。


    细节:
    1.别忘了先往树里添加(-INF)(INF),防止出现找不到前驱或者后继的情况。
    2.由于第一条,所以每次删除后要再添加(-INF),查询的时候应该查第(k + 1)大的元素。


    然后因为我刚开始学,所以因为各种小毛病调了好久……

    #include<cstdio>
    #include<iostream>
    #include<cmath>
    #include<algorithm>
    #include<cstring>
    #include<cstdlib>
    #include<cctype>
    #include<vector>
    #include<stack>
    #include<queue>
    using namespace std;
    #define enter puts("") 
    #define space putchar(' ')
    #define Mem(a, x) memset(a, x, sizeof(a))
    #define rg register
    typedef long long ll;
    typedef double db;
    const int INF = 0x3f3f3f3f;
    const db eps = 1e-8;
    const int maxn = 1e6 + 5;
    inline ll read()
    {
      ll ans = 0;
      char ch = getchar(), last = ' ';
      while(!isdigit(ch)) last = ch, ch = getchar();
      while(isdigit(ch)) ans = (ans << 1) + (ans << 3) + ch - '0', ch = getchar();
      if(last == '-') ans = -ans;
      return ans;
    }
    inline void write(ll x)
    {
      if(x < 0) x = -x, putchar('-');
      if(x >= 10) write(x / 10);
      putchar(x % 10 + '0');
    }
    
    int n, Min, delta = 0, sum = 0;
    char c[2];
    
    struct Tree
    {
      int ch[2], fa;
      int siz, cnt, val;
    }t[maxn];
    int root, ncnt = 0;
    
    void _PrintTree(int now)  //调试用 
    {
      if(!now) return;
      printf("nd:%d val:%d lsval:%d rsval:%d
    ", now, t[now].val, t[t[now].ch[0]].val, t[t[now].ch[1]].val);
      _PrintTree(t[now].ch[0]); _PrintTree(t[now].ch[1]);
    }
    void _SizCk(int now)
    {
      if(!now) return;
      if(t[now].siz != t[t[now].ch[0]].siz + t[t[now].ch[1]].siz + t[now].cnt)
        printf("Size Error! %d
    ",now);
      _SizCk(t[now].ch[0]); _SizCk(t[now].ch[1]);
    }
    
    void pushup(int now)
    {
      t[now].siz = t[t[now].ch[0]].siz + t[t[now].ch[1]].siz + t[now].cnt;
    }
    void rotate(int x)
    {
      int y = t[x].fa, z = t[y].fa, k = (t[y].ch[1] == x);
      t[z].ch[t[z].ch[1] == y] = x; t[x].fa = z;
      t[y].ch[k] = t[x].ch[k ^ 1]; t[t[x].ch[k ^ 1]].fa = y;
      t[x].ch[k ^ 1] = y; t[y].fa = x;
      pushup(y); pushup(x);  //first x,after y
      //_SizCk(x);
    }
    void splay(int x, int s)
    {
      while(t[x].fa != s)
        {
          int y = t[x].fa, z = t[y].fa;
          if(z != s)
    	{
    	  if((x == t[y].ch[1]) ^ (y == t[z].ch[1])) rotate(x);
    	  else rotate(y);
    	}
          rotate(x);
        }
      if(!s) root = x;
    }
    
    void insert(int x)
      int now = root, f = 0;
      while(now && t[now].val != x) f = now, now = t[now].ch[x > t[now].val];
      if(now) t[now].cnt++;
      else
        {
          now = ++ncnt;
          if(f) t[f].ch[x > t[f].val] = now;
          t[now].fa = f;
          t[now].ch[0] = t[now].ch[1] = 0;
          t[now].siz = t[now].cnt = 1; t[now].val = x;
        }
      splay(now, 0);
    }
    void find(int x)
    {
      int now = root;
      while(t[now].val != x && t[now].ch[x > t[now].val]) now = t[now].ch[x > t[now].val];
      splay(now, 0);
      //_PrintTree(root);
    }
    int nxt(int x)
    {
      find(x);
      if(t[root].val >= x) return root;
      int now = t[root].ch[1];
      while(t[now].ch[0]) now = t[now].ch[0];
      return now;
    }
    void clear(int& now)
    {
      t[now].val = t[now].siz = t[now].cnt = 0;
      t[now].fa = t[now].ch[0] = t[now].ch[1] = 0;
      now = 0;
    }
    int del(int x)  //nxt(x) go to root, and del left son
    //the num >= x!
    {
      int now = nxt(x);
      splay(now, 0);
      int ret = t[t[root].ch[0]].siz;
      clear(t[root].ch[0]);
      pushup(root);
      return ret;
    }
    int query(int k)
    {
      int now = root;
      while(1)
        {
          if(k <= t[t[now].ch[1]].siz) now = t[now].ch[1];
          else if(k <= t[t[now].ch[1]].siz + t[now].cnt) return t[now].val;
          else k -= t[t[now].ch[1]].siz + t[now].cnt, now = t[now].ch[0];
        }
    }
    
    int main()
    {
      insert(-INF); insert(INF);
      n = read(); Min = read();
      for(int i = 1; i <= n; ++i)
        {
          scanf("%s", c); int x = read();
          if(c[0] == 'I' && x >= Min) insert(x - delta);
          else if(c[0] == 'A') delta += x;
          else if(c[0] == 'S') delta -= x, sum += del(Min - delta) - 1, insert(-INF);
          else if(c[0] == 'F')
    	{
    	  x++;  //有INF
    	  if(t[root].siz < x + 1) write(-1), enter;
    	  else write(query(x) + delta), enter;
    	}
          //_PrintTree(root);
        }
      write(sum), enter;
      return 0;
    }
    
  • 相关阅读:
    svn 常用控制台命令解析
    android studio Activity标题栏研究
    android 音频播放总结 soundlPool,MediaPlay
    android studio 将library导出为jar 亲测成功
    android 控件自定义样式
    android studio 程序错误
    android 界面布局
    jdk 多版本安装 for mac
    android 控件ui
    homebrew for mac
  • 原文地址:https://www.cnblogs.com/mrclr/p/10054784.html
Copyright © 2011-2022 走看看