zoukankan      html  css  js  c++  java
  • [ZJOI2006]书架

    嘟嘟嘟


    splay++……


    这真是一道好题……
    刚开始我困扰了好久,因为前几道题都是以权值为关键字建树的,但是这题显然不是。
    后来才知道这题是以位置为关键字,即对于当前节点(now)(now)的左子树表示位置在(now)之前的,右子树表示位置在(now)之后的,即左子树的大小(+)(1)就是节点(now)表示的数在原数组的位置。


    好了,接下来看看这些操作怎么办吧
    1.建树
    还是要说一下(虽然很水)。一次把数组中的数加入splay中,每次加入一个数就把(++ncnt)作为这个数的节点,然后作为(ncnt - 1)的右儿子。这样一定保证了位置靠后的数是位置靠前的数的右儿子。
    然后如果(ncnt > 1)了,别忘了把当前节点旋到根(相当于更新该节点的祖先)。

    void insert(int x)
    {
      pos[x] = ++ncnt;
      t[ncnt].ch[0] = t[ncnt].ch[1] = 0;
      t[ncnt].siz = 1; t[ncnt].val = x;
      if(ncnt > 1) t[ncnt].fa = ncnt - 1, t[ncnt - 1].ch[1] = ncnt, splay(ncnt, 0);
    }
    

    2.Top S
    因为我们在写splay,所以一定要有“区间”的意识(看某一篇博客把splay叫做“区间之王”,笑)。
    (a[x])移到序列的头,相当于把(a[1])~(a[x - 1])插入到(a[x])(a[x + 1])之间。
    所以,先把(a[x])旋转到根,然后查找(a[x])的后继,再把根的左子树接到后继的左儿子上,最后删除左子树。

    void top(int x)
    {
      splay(pos[x], 0);
      if(!t[root].ch[0]) return;
      else if(!t[root].ch[1]) t[root].ch[1] = t[root].ch[0], t[root].ch[0] = 0;
      else
        {
          int now = t[root].ch[1];
          while(t[now].ch[0]) now = t[now].ch[0];
          //现在now就是后继了
          t[t[root].ch[0]].fa = now; t[now].ch[0] = t[root].ch[0];
          t[root].ch[0] = 0;
          splay(t[now].ch[0], 0);
        }
    }
    

    3.Bottom S
    和top同理,不说了。
    4.Ask S
    (x)所在节点旋转到根,则左子树大小就是答案。
    5.Query S
    用bst的写法就行。


    放完整代码啦

    #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 = 8e4 + 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, m;
    char c[10];
    
    struct Tree
    {
      int ch[2], fa;
      int siz, val;
    }t[maxn];
    int pos[maxn], root, ncnt = 0;
    
    void _PrintTr(int now)
    {
      if(!now) return;
      printf("nd:%d val:%d ls:%d rs:%d
    ", now, t[now].val, t[t[now].ch[0]].val, t[t[now].ch[1]].val);
      _PrintTr(t[now].ch[0]); _PrintTr(t[now].ch[1]);
    }
    void _PrintFa(int now)
    {
      if(!now) return;
      printf("nd:%d val:%d fa:%d
    ", now, t[now].val, t[t[now].fa].val);
      _PrintFa(t[now].fa);
    }
    
    void pushup(int now)
    {
      t[now].siz = t[t[now].ch[0]].siz + t[t[now].ch[1]].siz + 1;
    }
    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);
    }
    void splay(int x, int s)
    {
      while(t[x].fa != s)
        {
          int y = t[x].fa, z = t[y].fa;
          if(z != s)
    	{
    	  if((t[z].ch[1] == y) ^ (t[y].ch[1] == x)) rotate(x);
    	  else rotate(y);
    	}
          rotate(x);
        }
      if(!s) root = x;
    }
    void insert(int x)
    {
      pos[x] = ++ncnt;
      t[ncnt].ch[0] = t[ncnt].ch[1] = 0;
      t[ncnt].siz = 1; t[ncnt].val = x;
      if(ncnt > 1) t[ncnt].fa = ncnt - 1, t[ncnt - 1].ch[1] = ncnt, splay(ncnt, 0);
    }
    void top(int x)
    {
      splay(pos[x], 0);
      if(!t[root].ch[0]) return;
      else if(!t[root].ch[1]) t[root].ch[1] = t[root].ch[0], t[root].ch[0] = 0;
      else
        {
          int now = t[root].ch[1];
          while(t[now].ch[0]) now = t[now].ch[0];
          t[t[root].ch[0]].fa = now; t[now].ch[0] = t[root].ch[0];
          t[root].ch[0] = 0;
          splay(t[now].ch[0], 0);
        }
    }
    void bot(int x)
    {
      splay(pos[x], 0);
      if(!t[root].ch[1]) return;
      else if(!t[root].ch[0]) t[root].ch[0] = t[root].ch[1], t[root].ch[1] = 0;
      else
        {
          int now = t[root].ch[0];
          while(t[now].ch[1]) now = t[now].ch[1];
          t[t[root].ch[1]].fa = now; t[now].ch[1] = t[root].ch[1];
          t[root].ch[1] = 0;
          splay(t[now].ch[1], 0);
        }
    }
    void ins(int x, int flg)
    {
      if(!flg) return;
      splay(pos[x], 0);
      int now;
      if(flg == -1)
        {
          now = t[root].ch[0];
          while(t[now].ch[1]) now = t[now].ch[1];
        }
      else
        {
          now = t[root].ch[1];
          while(t[now].ch[0]) now = t[now].ch[0];
        }
      swap(pos[t[now].val], pos[t[root].val]);
      swap(t[now].val, t[root].val);
    }
    int query(int k)
    {
      int now = root;
      while(1)
        {
          if(t[t[now].ch[0]].siz >= k) now = t[now].ch[0];
          else if(t[t[now].ch[0]].siz + 1 == k) return t[now].val;
          else k -= (t[t[now].ch[0]].siz + 1), now = t[now].ch[1];
        }
    }
    
    int main()
    {
      n = read(); m = read();
      for(int i = 1, x; i <= n; ++i) x = read(), insert(x);
      for(int i = 1, x; i <= m; ++i)
        {
          scanf("%s", c); x = read();
          if(c[0] == 'T') top(x);
          else if(c[0] == 'B') bot(x);
          else if(c[0] == 'I') {int y = read(); ins(x, y);}
          else if(c[0] == 'A') splay(pos[x], 0), write(t[t[root].ch[0]].siz), enter;
          else write(query(x)), enter;
          //_PrintTr(root);
        }
      return 0;
    }
    

    猜猜我为什么又debug到崩溃?  ——因为我把splay中的while打成了if。
  • 相关阅读:
    linux echo 换行
    linux 脚本 逻辑关系的写法及区别
    linux vim ***
    跟我一起学Makefile
    linux awk
    linux grep命令 ***
    unbuntu 安装及服务器配置
    linux 静态库文件
    samba 配置
    linux tar
  • 原文地址:https://www.cnblogs.com/mrclr/p/10060317.html
Copyright © 2011-2022 走看看