zoukankan      html  css  js  c++  java
  • HDU 3487 Play with Chain (Splay)

    题意:给定一个序列,1 2 3 ... n,然后有两种操作。

    第一种是 CUT x y z ,把第 x 到 第 y 个数剪切下来,然后放到第 z 个后面。

    第二种是 FLIP x y 把 第 x 到第 y 个反转。

    最后输出序列。

    析:几乎就是裸的Splay,就是两种操作,剪切和反转,用splay很好维护,剪切的时候不要一个结点一个结点的剪,要直接剪切一棵子树。粘贴也是。

    代码如下:

    #pragma comment(linker, "/STACK:1024000000,1024000000")
    #include <cstdio>
    #include <string>
    #include <cstdlib>
    #include <cmath>
    #include <iostream>
    #include <cstring>
    #include <set>
    #include <queue>
    #include <algorithm>
    #include <vector>
    #include <map>
    #include <cctype>
    #include <cmath>
    #include <stack>
    #include <sstream>
    #define debug() puts("++++");
    #define gcd(a, b) __gcd(a, b)
    #define lson l,m,rt<<1
    #define rson m+1,r,rt<<1|1
    #define freopenr freopen("in.txt", "r", stdin)
    #define freopenw freopen("out.txt", "w", stdout)
    using namespace std;
    
    typedef long long LL;
    typedef unsigned long long ULL;
    typedef pair<int, int> P;
    const int INF = 0x3f3f3f3f;
    const LL LNF = 1e16;
    const double inf = 0x3f3f3f3f3f3f;
    const double PI = acos(-1.0);
    const double eps = 1e-8;
    const int maxn = 3e5 + 10;
    const int mod = 1e9 + 7;
    const int dr[] = {-1, 0, 1, 0};
    const int dc[] = {0, 1, 0, -1};
    const char *de[] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
    int n, m;
    const int mon[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    const int monn[] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
    inline bool is_in(int r, int c){
      return r >= 0 && r < n && c >= 0 && c < m;
    }
    
    #define Key_value ch[ch[root][1]][0]
    int pre[maxn], ch[maxn][2], key[maxn], sz[maxn];
    int rev[maxn];
    int root, tot1;
    int a[maxn], b[maxn];
    int cnt;
    
    void NewNode(int &rt, int fa, int val){
      rt = ++tot1;
      pre[rt] = fa;
      ch[rt][0] = ch[rt][1] = 0;
      key[rt] = val;
      rev[rt] = 0;
      sz[rt] = 1;
    }
    
    void update(int rt){
      if(!rt)  return ;
      swap(ch[rt][0], ch[rt][1]);
      rev[rt] ^= 1;
    }
    
    void push_up(int rt){
      sz[rt] = sz[ch[rt][0]] + sz[ch[rt][1]] + 1;
    }
    
    void push_down(int rt){
      if(rev[rt]){
        update(ch[rt][0]);
        update(ch[rt][1]);
        rev[rt] = 0;
      }
    }
    
    void Build(int &rt, int l, int r, int fa, int *a){
      if(l > r)  return ;
      int m = l + r >> 1;
      NewNode(rt, fa, a[m]);
      Build(ch[rt][0], l, m-1, rt, a);
      Build(ch[rt][1], m+1, r, rt, a);
      push_up(rt);
    }
    
    void Init(){
      root = tot1 = 0;
      ch[root][0] = ch[root][1] = sz[root] = pre[root] = 0;
      rev[root] = key[root] = 0;
      NewNode(root, 0, -1);
      NewNode(ch[root][1], root, -1);
      Build(Key_value, 1, n, ch[root][1], a);
      push_up(ch[root][1]);
      push_up(root);
    }
    
    void Rotate(int x, int kind){
      int y = pre[x];
      push_down(y);
      push_down(x);
      ch[y][!kind] = ch[x][kind];
      pre[ch[x][kind]] = y;
      if(pre[y])  ch[pre[y]][ch[pre[y]][1]==y] = x;
      pre[x] = pre[y];
      ch[x][kind] = y;
      pre[y] = x;
      push_up(y);
    }
    
    void Splay(int r, int goal){
      push_down(r);
      while(pre[r] != goal){
        if(pre[pre[r]] == goal){
          push_down(pre[r]);
          push_down(r);
          Rotate(r, ch[pre[r]][0] == r);
        }
        else{
          push_down(pre[pre[r]]);
          push_down(pre[r]);
          push_down(r);
          int y = pre[r];
          int kind = ch[pre[y]][0] == y;
          if(ch[y][kind] == r){
            Rotate(r, !kind);
            Rotate(r, kind);
          }
          else{
            Rotate(y, kind);
            Rotate(r, kind);
          }
        }
      }
      push_up(r);
      if(goal == 0)  root = r;
    }
    
    int Get_kth(int rt, int k){
      push_down(rt);
      int t = sz[ch[rt][0]] + 1;
      if(t == k)  return rt;
      if(t > k)  return Get_kth(ch[rt][0], k);
      return Get_kth(ch[rt][1], k-t);
    }
    
    void Reverse(int pos, int tot){
      Splay(Get_kth(root, pos), 0);
      Splay(Get_kth(root, pos+tot+1), root);
      update(Key_value);
      push_up(ch[root][1]);
      push_up(root);
    }
    
    
    void Cut(int x, int y, int z){
      int tot = y - x + 1;
      Splay(Get_kth(root, x), 0);
      Splay(Get_kth(root, x+tot+1), root);
      int t = Key_value;
      Key_value = 0;
      push_up(ch[root][1]);
      push_up(root);
      Splay(Get_kth(root, z+1), 0);
      Splay(Get_kth(root, z+2), root);
      Key_value = t;
      pre[t] = ch[root][1];
      push_up(ch[root][1]);
      push_up(root);
    }
    
    void print(int rt){
      if(!rt)  return ;
      push_down(rt);
      print(ch[rt][0]);
      if(cnt > 0 && cnt <= n){
        if(cnt > 1)  putchar(' ');
        printf("%d", key[rt]);
      }
      ++cnt;
      print(ch[rt][1]);
    }
    
    
    int main(){
      for(int i = 1; i < maxn; ++i)  a[i] = i;
      while(scanf("%d %d", &n, &m) == 2 && n + m > 0){
        Init();
        char s[10];
        while(m--){
          int x, y, z;
          scanf("%s %d %d", s, &x, &y);
          if(s[0] == 'C'){
            scanf("%d", &z);
            Cut(x, y, z);
          }
          else  Reverse(x, y-x+1);
        }
        cnt = 0;
        print(root);
        printf("
    ");
      }
      return 0;
    }
    

      

  • 相关阅读:
    File类与字节流
    字符流
    二进制文件的读写与小结
    下拉列表框与线性、帧布局管理器
    android第二章控件2
    第一章
    安卓时间
    2017《Java技术》 预留作业2 胡开辉
    2017 《JAVA技术》 胡开辉
    ESB产品对比
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/7276831.html
Copyright © 2011-2022 走看看