zoukankan      html  css  js  c++  java
  • POJ 3580 SuperMemo

    POJ_3580

        更多splay练手的题目可以参考胡浩的博客http://www.notonlysuccess.com/index.php/splay-tree/,有了前面对区间翻转、切割的训练之后,这个题目就显得思路比较直观了。

        对于ADD操作,和线段树的处理是一样的,加一个add延迟标记即可,在pushdown的时候别忘记还会影响到子树上记录的min值就OK了。

        对于REVERSE操作,用一个rev延迟标记即可,注意到pushdown的时候子树中rev的标记应该是加1模2,而不能简单的赋值成1就OK了。

        对于REVOLVE操作,实际上相当于把一个区间挪到了另一个区间后面,于是先把要移动的区间“切割”下来,再插入到适当的位置即可。

        对于INSERT操作,可以先将x旋转到根T,再将x+1旋转right[T],这时left[right[T]]就是空的了,直接在这个位置插入即可。

        对于DELETE操作,和INSERT操作相似,最后删除left[right[T]]那个点即可。

        对于MIN操作,可以用min记录子树上的最小值,在需要输出的时候把x-1旋转到根T,然后把y+1旋转到right[T],输出min[left[right[T]]]即可。

        此外,为了避免爆空间,可以将数组开成2倍或者自己写一个回收内存的栈。

    #include<stdio.h>
    #include<string.h>
    #define MAXD 200010
    int N, M, T, node, a[MAXD], size[MAXD], left[MAXD], right[MAXD], pre[MAXD], key[MAXD];
    int add[MAXD], rev[MAXD], min[MAXD];
    void pushdown(int cur)
    {
    int ls = left[cur], rs = right[cur];
    if(add[cur])
    {
    add[ls] += add[cur], add[rs] += add[cur];
    key[ls] += add[cur], key[rs] += add[cur];
    min[ls] += add[cur], min[rs] += add[cur];
    add[cur] = 0;
    }
    if(rev[cur])
    {
    rev[ls] = (rev[ls] + 1) & 1, rev[rs] = (rev[rs] + 1) & 1;
    left[cur] = rs, right[cur] = ls;
    rev[cur] = 0;
    }
    }
    void update(int cur)
    {
    int ls = left[cur], rs = right[cur];
    size[cur] = size[ls] + size[rs] + 1;
    min[cur] = key[cur];
    if(ls && min[ls] < min[cur])
    min[cur] = min[ls];
    if(rs && min[rs] < min[cur])
    min[cur] = min[rs];
    }
    void leftrotate(int x)
    {
    int y = right[x], p = pre[x];
    right[x] = left[y];
    if(right[x])
    pre[right[x]] = x;
    left[y] = x;
    pre[x] = y;
    pre[y] = p;
    if(p == 0)
    T = y;
    else
    right[p] == x ? right[p] = y : left[p] = y;
    update(x);
    }
    void rightrotate(int x)
    {
    int y = left[x], p = pre[x];
    left[x] = right[y];
    if(left[x])
    pre[left[x]] = x;
    right[y] = x;
    pre[x] = y;
    pre[y] = p;
    if(p == 0)
    T = y;
    else
    right[p] == x ? right[p] = y : left[p] = y;
    update(x);
    }
    void splay(int x, int goal)
    {
    int y, z;
    for(;;)
    {
    if((y = pre[x]) == goal)
    break;
    if((z = pre[y]) == goal)
    right[y] == x ? leftrotate(y) : rightrotate(y);
    else
    {
    if(right[z] == y)
    {
    if(right[y] == x)
    leftrotate(z), leftrotate(y);
    else
    rightrotate(y), leftrotate(z);
    }
    else
    {
    if(left[y] == x)
    rightrotate(z), rightrotate(y);
    else
    leftrotate(y), rightrotate(z);
    }
    }
    }
    update(x);
    }
    void rotateto(int k, int goal)
    {
    int i = T;
    for(;;)
    {
    pushdown(i);
    if(size[left[i]] + 1 == k)
    break;
    if(k <= size[left[i]])
    i = left[i];
    else
    k -= size[left[i]] + 1, i = right[i];
    }
    splay(i, goal);
    }
    void newnode(int &cur, int v)
    {
    cur = ++ node;
    min[cur] = key[cur] = v;
    size[cur] = 1;
    left[cur] = right[cur] = rev[cur] = add[cur] = 0;
    }
    void build(int &cur, int x, int y, int p)
    {
    int mid = (x + y) / 2;
    newnode(cur, a[mid]);
    pre[cur] = p;
    if(x == y)
    return ;
    if(x < mid)
    build(left[cur], x, mid - 1, cur);
    if(mid < y)
    build(right[cur], mid + 1, y, cur);
    update(cur);
    }
    void init()
    {
    int i;
    for(i = 1; i <= N; i ++)
    scanf("%d", &a[i]);
    T = node = size[0] = left[0] = right[0] = pre[0] = 0;
    build(T, 0, N + 1, 0);
    }
    void ADD(int x, int y, int z)
    {
    int k;
    rotateto(x, 0), rotateto(y + 2, T);
    k = left[right[T]];
    add[k] += z, key[k] += z, min[k] += z;
    }
    void REVERSE(int x, int y)
    {
    int k;
    rotateto(x, 0), rotateto(y + 2, T);
    k = left[right[T]];
    rev[k] = (rev[k] + 1) & 1;
    }
    void REVOLVE(int x, int y, int z)
    {
    int k = z % (y - x + 1), t;
    if(k)
    {
    rotateto(x, 0), rotateto(y - k + 2, T);
    t = left[right[T]];
    left[right[T]] = 0;
    update(right[T]), update(T);
    rotateto(x + k, 0), rotateto(x + k + 1, T);
    left[right[T]] = t, pre[t] = right[T];
    update(right[T]), update(T);
    }
    }
    void INSERT(int x, int y)
    {
    rotateto(x + 1, 0), rotateto(x + 2, T);
    newnode(left[right[T]], y);
    pre[left[right[T]]] = right[T];
    update(right[T]), update(T);
    }
    void DELETE(int x)
    {
    rotateto(x, 0), rotateto(x + 2, T);
    left[right[T]] = 0;
    update(right[T]), update(T);
    }
    void MIN(int x, int y)
    {
    rotateto(x, 0), rotateto(y + 2, T);
    printf("%d\n", min[left[right[T]]]);
    }
    void solve()
    {
    int i, x, y, z;
    char b[10];
    scanf("%d", &M);
    for(i = 0; i < M; i ++)
    {
    scanf("%s", b);
    if(b[0] == 'A')
    {
    scanf("%d%d%d", &x, &y, &z);
    ADD(x, y, z);
    }
    else if(b[0] == 'R')
    {
    scanf("%d%d", &x, &y);
    if(b[3] == 'E')
    REVERSE(x, y);
    else
    {
    scanf("%d", &z);
    REVOLVE(x, y, z);
    }
    }
    else if(b[0] == 'I')
    {
    scanf("%d%d", &x, &y);
    INSERT(x, y);
    }
    else if(b[0] == 'D')
    {
    scanf("%d", &x);
    DELETE(x);
    }
    else
    {
    scanf("%d%d", &x, &y);
    MIN(x, y);
    }
    }
    }
    int main()
    {
    while(scanf("%d", &N) == 1)
    {
    init();
    solve();
    }
    return 0;
    }


  • 相关阅读:
    webform文件上传加水印
    2017-6-6 ASP.NET Ajax版页面无刷新三级联动
    2017-6-5 Ajax应用
    转【 正则表达式】
    2017-6-4 JQuery中的选择器和动画 弹窗遮罩
    Linq 组合分页查询
    2017-6-2 Linq高级查询和函数
    2017-6-3 JQuery中的Dom操作和事件
    WebForm母版页
    WebForm内置对象:Application和ViewState、Repeater的Command用法
  • 原文地址:https://www.cnblogs.com/staginner/p/2429505.html
Copyright © 2011-2022 走看看