zoukankan      html  css  js  c++  java
  • BZOJ 1500 [NOI2005]维修数列 (splay)

    1500: [NOI2005]维修数列

    Time Limit: 10 Sec  Memory Limit: 64 MB
    Submit: 15076  Solved: 4973
    [Submit][Status][Discuss]

    Description

    Input

    输入的第1 行包含两个数N 和M(M ≤20 000),N 表示初始时数列中数的个数,M表示要进行的操作数目。
    第2行包含N个数字,描述初始时的数列。
    以下M行,每行一条命令,格式参见问题描述中的表格。
    任何时刻数列中最多含有500 000个数,数列中任何一个数字均在[-1 000, 1 000]内。
    插入的数字总数不超过4 000 000个,输入文件大小不超过20MBytes。

    Output

    对于输入数据中的GET-SUM和MAX-SUM操作,向输出文件依次打印结果,每个答案(数字)占一行。

    Sample Input

    9 8
    2 -6 3 5 1 -5 -3 6 3
    GET-SUM 5 4
    MAX-SUM
    INSERT 8 3 -5 7 2
    DELETE 12 1
    MAKE-SAME 3 3 2
    REVERSE 3 6
    GET-SUM 5 4
    MAX-SUM

    Sample Output

    -1
    10
    1
    10

    HINT

    Source

    析:利用spaly直接维护就好。

    代码如下:

    #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 = 5e5 + 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 root, tot1;
    int sum[maxn], rev[maxn], setv[maxn];
    int lx[maxn], rx[maxn], mx[maxn];
    int s[maxn], tot2, a[maxn];
    
    void NewNode(int &rt, int fa, int x){
      if(tot2)  rt = s[tot2--];
      else  rt = ++tot1;
      pre[rt] = fa;
      key[rt] = sum[rt] = x;
      ch[rt][0] = ch[rt][1] = 0;
      lx[rt] = rx[rt] = mx[rt] = x;
      rev[rt] = setv[rt] = 0;
      sz[rt] = 1;
    }
    
    void push_up(int rt){
      int l = ch[rt][0], r = ch[rt][1];
      sz[rt] = sz[l] + sz[r] + 1;
      sum[rt] = sum[l] + sum[r] + key[rt];
      lx[rt] = max(lx[l], sum[l]+key[rt]+max(0, lx[r]));
      rx[rt] = max(rx[r], sum[r]+key[rt]+max(0, rx[l]));
      mx[rt] = max(0, rx[l]) + key[rt] + max(0, lx[r]);
      mx[rt] = max(mx[rt], max(mx[l], mx[r]));
    }
    
    void update_rev(int rt){
      if(!rt)  return ;
      swap(ch[rt][0], ch[rt][1]);
      swap(lx[rt], rx[rt]);
      rev[rt] ^= 1;
    }
    
    void update_setv(int rt, int val){
      if(!rt)  return ;
      key[rt] = val;
      sum[rt] = val * sz[rt];
      lx[rt] = rx[rt] = mx[rt] = max(val, sum[rt]);
      setv[rt] = 1;
    }
    
    void push_down(int rt){
      if(setv[rt]){
        update_setv(ch[rt][0], key[rt]);
        update_setv(ch[rt][1], key[rt]);
        setv[rt] = 0;
      }
      if(rev[rt]){
        update_rev(ch[rt][0]);
        update_rev(ch[rt][1]);
        rev[rt] = 0;
      }
    }
    
    void Build(int &rt, int l, int r, int fa){
      if(l > r)  return ;
      int m = l+r >> 1;
      NewNode(rt, fa, a[m]);
      Build(ch[rt][0], l, m-1, rt);
      Build(ch[rt][1], m+1, r, rt);
      push_up(rt);
    }
    
    void Init(){
      tot1 = root = tot2 = 0;
      ch[root][0] = ch[root][1] = sz[root] = pre[root] = 0;
      setv[root] = rev[root] = sum[root] = key[root] = 0;
      lx[root] = rx[root] = mx[root] = -INF;
      NewNode(root, 0, -1);
      NewNode(ch[root][1], root, -1);
      for(int i = 0; i < n; ++i)  scanf("%d", a+i);
      Build(Key_value, 0, n-1, ch[root][1]);
      push_up(ch[root][1]);
      push_up(root);
    }
    
    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 Rotate(int x, int k){
      int y = pre[x];
      push_down(y);
      push_down(x);
      ch[y][!k] = ch[x][k];
      pre[ch[x][k]] = y;
      if(pre[y])  ch[pre[y]][ch[pre[y]][1]==y] = x;
      pre[x] = pre[y];
      ch[x][k] = y;
      pre[y] = x;
      push_up(y);
    }
    
    void Splay(int rt, int goal){
      push_down(rt);
      while(pre[rt] != goal){
        if(pre[pre[rt]] == goal){
          push_down(pre[rt]);
          push_down(rt);
          Rotate(rt, ch[pre[rt]][0] == rt);
          continue;
        }
        push_down(pre[pre[rt]]);
        push_down(pre[rt]);
        push_down(rt);
        int y = pre[rt];
        int k = ch[pre[y]][0] == y;
        if(ch[y][k] == rt){
          Rotate(rt, !k);
          Rotate(rt, k);
        }
        else{
          Rotate(y, k);
          Rotate(rt, k);
        }
      }
      push_up(rt);
      if(goal == 0)  root = rt;
    }
    
    void Insert(int pos, int tot){
      for(int i = 0; i < tot; ++i)  scanf("%d", a+i);
      Splay(Get_kth(root, pos+1), 0);
      Splay(Get_kth(root, pos+2), root);
      Build(Key_value, 0, tot-1, ch[root][1]);
      push_up(ch[root][1]);
      push_up(root);
    }
    
    void Erase(int rt){
      if(!rt)  return ;
      s[++tot2] = rt;
      Erase(ch[rt][0]);
      Erase(ch[rt][1]);
    }
    
    void Delete(int pos, int tot){
      Splay(Get_kth(root, pos), 0);
      Splay(Get_kth(root, pos+tot+1), root);
      Erase(Key_value);
      pre[Key_value] = 0;
      Key_value = 0;
      push_up(ch[root][1]);
      push_up(root);
    }
    
    void Reverse(int pos, int tot){
      Splay(Get_kth(root, pos), 0);
      Splay(Get_kth(root, pos+tot+1), root);
      update_rev(Key_value);
      push_up(ch[root][1]);
      push_up(root);
    }
    
    int query(int pos, int tot){
      Splay(Get_kth(root, pos), 0);
      Splay(Get_kth(root, pos+tot+1), root);
      return sum[Key_value];
    }
    
    void Make_setv(int pos, int tot, int c){
      Splay(Get_kth(root, pos), 0);
      Splay(Get_kth(root, pos+tot+1), root);
      update_setv(Key_value, c);
      push_up(ch[root][1]);
      push_up(root);
    }
    
    int Get_MaxSum(int pos, int tot){
      Splay(Get_kth(root, pos), 0);
      Splay(Get_kth(root, pos+tot+1), root);
      return mx[Key_value];
    }
    
    int main(){
      while(scanf("%d %d", &n, &m) == 2){
        Init();
        char op[20];
        while(m--){
          scanf("%s", op);
          int x, y;
          if(op[0] == 'I'){
            scanf("%d %d", &x, &y);
            Insert(x, y);
          }
          else if(op[0] == 'D'){
            scanf("%d %d", &x, &y);
            Delete(x, y);
          }
          else if(op[0] == 'R'){
            scanf("%d %d", &x, &y);
            Reverse(x, y);
          }
          else if(op[0] == 'G'){
            scanf("%d %d", &x, &y);
            printf("%d
    ", query(x, y));
          }
          else if(op[2] == 'K'){
            int z;
            scanf("%d %d %d", &x, &y, &z);
            Make_setv(x, y, z);
          }
          else{
            printf("%d
    ", Get_MaxSum(1, sz[root]-2));
          }
        }
      }
      return 0;
    }
    

      

  • 相关阅读:
    [LeetCode] Strobogrammatic Number III
    [LeetCode] Strobogrammatic Number II
    [Codeforces 1253E] Antenna Coverage
    [CodeForces 466C] Number of Ways
    UVa 806 四分树
    Uva 1572 自组合
    UVa Sculpture(离散化 floodfill)
    Uva 4916 Selling Cells(随机算法)
    UvaLive 4863 Balloons(贪心)
    UvaLive 4872 Underground Cables (最小生成树)
  • 原文地址:https://www.cnblogs.com/dwtfukgv/p/6782348.html
Copyright © 2011-2022 走看看