zoukankan      html  css  js  c++  java
  • BZOJ1500 维修数列

    1500: [NOI2005]维修数列

    Time Limit: 10 Sec  Memory Limit: 64 MB

    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


    水题,同时也是splay维护序列操作的一个极限了吧。
    数据结构对于我这种渣来说就是巨大的灾难,然后我调一个SB问题调了一个小时。
    巨坑:0结点的ans我存了0!!!
    实现思路:
    1. 用一个栈存可以用的空结点。
    2. 标记正常维护
    3. 两个哨兵
    4. 注意空结点与哨兵的各项值,这是一个大坑点

    其实也不难,写写就好了。。。


      1 #include<bits/stdc++.h>
      2 using namespace std;
      3 template <class _T> inline void read(_T &_x) {
      4     int _t; bool flag = false;
      5     while ((_t = getchar()) != '-' && (_t < '0' || _t > '9')) ;
      6     if (_t == '-') _t = getchar(), flag = true; _x = _t - '0';
      7     while ((_t = getchar()) >= '0' && _t <= '9') _x = _x * 10 + _t - '0';
      8     if (flag) _x = -_x;
      9 }
     10 const int inf = 1010000000; //0x3f3f3f3f;
     11 const int maxn = 500010;
     12 int root, f[maxn], ch[maxn][2], sz[maxn], v[maxn], sum[maxn], lx[maxn], rx[maxn], mx[maxn];
     13 int st[maxn], top;
     14 bool rev[maxn], tag[maxn];
     15 inline int newnode() {
     16     int cur = st[--top];
     17     f[cur] = ch[cur][0] = ch[cur][1] = mx[cur] = 0;
     18     sz[cur] = 1, v[cur] = sum[cur] = lx[cur] = rx[cur] = -inf;
     19     rev[cur] = tag[cur] = false;
     20     return cur;
     21 }
     22 void print(int rt) {
     23     if (!rt) return ;
     24     print(ch[rt][0]);
     25     printf("%d ", v[rt]);
     26     print(ch[rt][1]);
     27 }
     28 void Print() {
     29     print(root);
     30     puts("");
     31 }
     32 inline void update(int c) {
     33     if (!c) return ;
     34     sz[c] = sz[ch[c][0]] + sz[ch[c][1]] + 1;
     35     sum[c] = sum[ch[c][0]] + sum[ch[c][1]] + v[c];
     36     lx[c] = max(lx[ch[c][0]], sum[ch[c][0]] + v[c] + max(0, lx[ch[c][1]]));
     37     rx[c] = max(rx[ch[c][1]], sum[ch[c][1]] + v[c] + max(0, rx[ch[c][0]]));
     38     mx[c] = max(max(mx[ch[c][0]], mx[ch[c][1]]), v[c] + max(rx[ch[c][0]], 0) + max(lx[ch[c][1]], 0));
     39 }
     40 inline void rotate(int c) {
     41     int b = f[c], a = f[b], x = ch[b][1]==c;
     42     ch[b][x] = ch[c][x ^ 1], f[ch[b][x]] = b;
     43     ch[c][x ^ 1] = b, f[b] = c;
     44     if (a) ch[a][ch[a][1]==b] = c; else root = c;
     45     f[c] = a; update(b);
     46 }
     47 inline void rever(int c) {
     48     if (!c) return ;
     49     swap(ch[c][0], ch[c][1]);
     50     swap(lx[c], rx[c]);
     51     rev[c] ^= 1;
     52 }
     53 inline void replace(int c, int val) {
     54     if (!c) return ;
     55     v[c] = val, sum[c] = sz[c] * val;
     56     lx[c] = rx[c] = mx[c] = max(val, val * sz[c]);
     57     tag[c] = 1, rev[c] = 0;
     58 }
     59 inline void pushdown(int c) {
     60     if (rev[c]) {
     61         if (ch[c][0]) rever(ch[c][0]);
     62         if (ch[c][1]) rever(ch[c][1]);
     63         rev[c] = 0;
     64     }
     65     if (tag[c]) {
     66         if (ch[c][0]) replace(ch[c][0], v[c]);
     67         if (ch[c][1]) replace(ch[c][1], v[c]);
     68         tag[c] = 0;
     69     }
     70 }
     71 inline void pushpath(int c) {
     72     if (f[c]) pushpath(f[c]);
     73     pushdown(c);
     74 }
     75 inline int splay(int c, int a = 0) {
     76     pushpath(c);
     77     for (int b; (b = f[c]) != a; rotate(c)) if (f[b] != a)
     78         rotate((ch[f[b]][0]==b)==(ch[b][0]==c)?b:c);
     79     update(c);
     80     return c;
     81 }
     82 inline int find_kth(int rt, int k) {
     83     int ori = f[rt];
     84     do {
     85         pushdown(rt);
     86         if (k == sz[ch[rt][0]] + 1) break;
     87         if (k <= sz[ch[rt][0]]) rt = ch[rt][0];
     88         else k -= sz[ch[rt][0]] + 1, rt = ch[rt][1];
     89     } while (true);
     90     return splay(rt, ori);
     91 }
     92 int a[maxn];
     93 void build(int &rt, int l, int r, int fa) {
     94     int mid = (l + r) >> 1;
     95     rt = newnode(), f[rt] = fa, v[rt] = a[mid];
     96     if (l < mid) build(ch[rt][0], l, mid - 1, rt);
     97     if (r > mid) build(ch[rt][1], mid + 1, r, rt);
     98     update(rt);
     99 }
    100 inline int Query(int l, int len) {
    101     int x = find_kth(root, l);
    102     int y = find_kth(ch[x][1], len + 1);
    103     return sum[ch[y][0]];
    104 }
    105 inline void Insert(int l, int len) {
    106     for (int i = 1; i <= len; ++i) read(a[i]);
    107     int x = find_kth(root, l + 1);
    108     int y = find_kth(ch[x][1], 1);
    109     build(ch[y][0], 1, len, y);
    110     update(y), update(x);
    111 }
    112 void erase(int rt) {
    113     if (!rt) return ;
    114     st[top++] = rt;
    115     erase(ch[rt][0]), erase(ch[rt][1]);
    116 }
    117 inline void Delete(int l, int len) {
    118     int x = find_kth(root, l);
    119     int y = find_kth(ch[x][1], len + 1);
    120     erase(ch[y][0]), ch[y][0] = 0;
    121     update(y), update(x);
    122 }
    123 inline void Reverse(int l, int len) {
    124     int x = find_kth(root, l);
    125     int y = find_kth(ch[x][1], len + 1);
    126     rever(ch[y][0]);
    127     update(y), update(x);
    128 }
    129 inline void Replace(int l, int len, int val) {
    130     int x = find_kth(root, l);
    131     int y = find_kth(ch[x][1], len + 1);
    132     replace(ch[y][0], val);
    133     update(y), update(x);
    134 }
    135 int n, m;
    136 int main() {
    137     //freopen("1500.txt", "r", stdin);
    138     //freopen(".out", "w", stdout);
    139     lx[0] = rx[0] = mx[0] = -inf;
    140     read(n), read(m);
    141     for (int i = 1; i < maxn; ++i) st[top++] = i;
    142     int X = root = newnode();
    143     int Y = ch[X][1] = newnode();
    144     f[Y] = X;
    145     for (int i = 1; i <= n; ++i) read(a[i]);
    146     build(ch[Y][0], 1, n, Y);
    147     update(Y), update(X);
    148     char op[20];
    149     while (m--) {
    150         int x, y, z;
    151         scanf("%s", op);
    152         switch (op[0]) {
    153             case 'I':
    154                 read(x), read(y);
    155                 Insert(x, y);
    156                 break;
    157             case 'D':
    158                 read(x), read(y);
    159                 Delete(x, y);
    160                 break;
    161             case 'M':
    162                 if (op[2] == 'K') {
    163                     read(x), read(y), read(z);
    164                     Replace(x, y, z);
    165                 } else {
    166                     printf("%d
    ", mx[root]);
    167                 }
    168                 break;
    169             case 'R':
    170                 read(x), read(y);
    171                 Reverse(x, y);
    172                 break;
    173             case 'G':
    174                 read(x), read(y);
    175                 if (y == 0) puts("0");
    176                 else printf("%d
    ", Query(x, y));
    177                 break;
    178         }
    179     }
    180     return 0;
    181 }
    View Code
  • 相关阅读:
    13,发布CRM
    12,nginx+uWSGI+django+virtualenv+supervisor发布web服务器
    11.2,nginx负载均衡实验
    11.1,nginx集群概念
    11,nginx入门与实战
    10,python开发之virtualenv与virtualenvwrapper
    9.5web service基础知识
    9.4python开发之virtualenv与virtualenvwrapper
    9.3centos7安装python3 以及tab补全功能
    Google 浏览器被劫持怎么办?
  • 原文地址:https://www.cnblogs.com/akhpl/p/6759781.html
Copyright © 2011-2022 走看看