又一道Splay吐血题 [POJ 3580] SuperMemo

SuperMemo

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 9878   Accepted: 3177
Case Time Limit: 2000MS

Description

Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a memorizing game. At first, the host tells the participant a sequence of numbers, {A1A2, ... An}. Then the host performs a series of operations and queries on the sequence which consists:

  1. ADD x y D: Add D to each number in sub-sequence {Ax ... Ay}. For example, performing "ADD 2 4 1" on {1, 2, 3, 4, 5} results in {1, 3, 4, 5, 5}
  2. REVERSE x y: reverse the sub-sequence {Ax ... Ay}. For example, performing "REVERSE 2 4" on {1, 2, 3, 4, 5} results in {1, 4, 3, 2, 5}
  3. REVOLVE x y T: rotate sub-sequence {Ax ... AyT times. For example, performing "REVOLVE 2 4 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 2, 5}
  4. INSERT x P: insert P after Ax. For example, performing "INSERT 2 4" on {1, 2, 3, 4, 5} results in {1, 2, 4, 3, 4, 5}
  5. DELETE x: delete Ax. For example, performing "DELETE 2" on {1, 2, 3, 4, 5} results in {1, 3, 4, 5}
  6. MIN x y: query the participant what is the minimum number in sub-sequence {Ax ... Ay}. For example, the correct answer to "MIN 2 4" on {1, 2, 3, 4, 5} is 2

To make the show more interesting, the participant is granted a chance to turn to someone else that means when Jackson feels difficult in answering a query he may call you for help. You task is to watch the TV show and write a program giving the correct answer to each query in order to assist Jackson whenever he calls.

Input

The first line contains (≤ 100000).

The following n lines describe the sequence.

Then follows M (≤ 100000), the numbers of operations and queries.

The following M lines describe the operations and queries.

Output

For each "MIN" query, output the correct answer.

Sample Input

5
1
2
3
4
5
2
ADD 2 4 1
MIN 4 5

Sample Output

5

疯狂地Splay
#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
#define INF 0x7fffffff
#define N 1010000

struct SplayTree
{
    int ch[N][2],pre[N],val[N],sz[N],num[N],minx[N],add[N],rev[N];
    int top,root;

    inline void Rotate(int x,int c)
    {
        int y=pre[x];
        PushDown(y);
        PushDown(x);
        ch[y][!c]=ch[x][c];
        if(ch[x][c]) pre[ch[x][c]]=y;
        pre[x]=pre[y];
        if(pre[y]) ch[pre[y]][ch[pre[y]][1]==y]=x;
        ch[x][c]=y;
        pre[y]=x;
        PushUp(y);
        if(y==root) root=x;
    }
    inline void Splay(int x,int f)
    {
        PushDown(x);
        while(pre[x]!=f)
        {
            PushDown(pre[pre[x]]);
            PushDown(pre[x]);
            PushDown(x);
            if(pre[pre[x]]==f) Rotate(x,ch[pre[x]][0]==x);
            else
            {
                int y=pre[x],z=pre[y];
                int c=(ch[z][0]==y);
                if(ch[y][c]==x) Rotate(x,!c),Rotate(x,c);
                else Rotate(y,c),Rotate(x,c);
            }
        }
        PushUp(x);
        if(f==0) root=x;
    }
    inline void SplayKth(int k,int f)
    {
        int x=root;
        k+=1;
        while(1)
        {
            PushDown(x);
            if(k==sz[ch[x][0]]+1) break;
            else if(k<=sz[ch[x][0]]) x=ch[x][0];
            else k-=sz[ch[x][0]]+1,x=ch[x][1];
        }
        Splay(x,f);
    }
    inline void AddNode(int x,int c)
    {
        if(!x) return;
        val[x]+=c;
        add[x]+=c;
        minx[x]+=c;
    }
    inline void RevNode(int x)
    {
        if(!x) return;
        swap(ch[x][0],ch[x][1]);
        rev[x]^=1;
    }
    inline void PushUp(int x)
    {
        sz[x]=sz[ch[x][0]]+sz[ch[x][1]]+1;
        minx[x]=min(val[x],min(minx[ch[x][0]],minx[ch[x][1]]));
    }
    inline void PushDown(int x)
    {
        if(rev[x])
        {
            RevNode(ch[x][0]);
            RevNode(ch[x][1]);
            rev[x]=0;
        }
        if(add[x])
        {
            AddNode(ch[x][0],add[x]);
            AddNode(ch[x][1],add[x]);
        }
        add[x]=0;
    }
    inline void NewNode(int &x,int c,int f)
    {
        x=++top;
        sz[x]=1;
        ch[x][0]=ch[x][1]=rev[x]=add[x]=0;
        val[x]=minx[x]=c;
        pre[x]=f;
    }
    inline void Build(int &x,int l,int r,int f)
    {
        if(l>r) return;
        int m=(l+r)>>1;
        NewNode(x,num[m],f);
        Build(ch[x][0],l,m-1,x);
        Build(ch[x][1],m+1,r,x);
        PushUp(x);
    }
    inline void Init(int n)
    {
        top=pre[0]=ch[0][0]=ch[0][1]=sz[0]=rev[0]=add[0]=0;
        val[0]=minx[0]=INF;
        for(int i=1;i<=n;i++) scanf("%d",&num[i]);
        Build(root,0,n+1,0);
    }
    void Ins()
    {
        int pos,num;
        scanf("%d%d",&pos,&num);
        SplayKth(pos,0);
        SplayKth(pos+1,root);
        NewNode(ch[ch[root][1]][0],num,ch[root][1]);
        PushUp(ch[root][1]);
        PushUp(root);
    }
    void Del()
    {
        int pos;
        scanf("%d",&pos);
        SplayKth(pos-1,0);
        SplayKth(pos+1,root);
        ch[ch[root][1]][0]=0;
        PushUp(ch[root][1]);
        PushUp(root);
    }
    void Rev()
    {
        int l,r;
        scanf("%d%d",&l,&r);
        SplayKth(l-1,0);
        SplayKth(r+1,root);
        RevNode(ch[ch[root][1]][0]);
    }
    void Add()
    {
        int l,r,c;
        scanf("%d%d%d",&l,&r,&c);
        SplayKth(l-1,0);
        SplayKth(r+1,root);
        AddNode(ch[ch[root][1]][0],c);
        PushUp(ch[root][1]);
        PushUp(root);
    }
    void Res()
    {
        int l,r,t;
        scanf("%d%d%d",&l,&r,&t);
        int len=r-l+1;
        t=(t%len+len)%len;
        if(!t) return;
        SplayKth(r-t,0);
        SplayKth(r+1,root);
        int x=ch[ch[root][1]][0];
        ch[ch[root][1]][0]=0;
        PushUp(ch[root][1]);
        PushUp(root);

        SplayKth(l-1,0);
        SplayKth(l,root);
        ch[ch[root][1]][0]=x;
        pre[ch[ch[root][1]][0]]=ch[root][1];
        PushUp(ch[root][1]);
        PushUp(root);
    }
    void Min()
    {
        int l,r;
        scanf("%d%d",&l,&r);
        SplayKth(l-1,0);
        SplayKth(r+1,root);
        printf("%d\n",minx[ch[ch[root][1]][0]]);
    }
}t;
int main()
{
    int n,m;
    char op[10];
    while(scanf("%d",&n)!=EOF)
    {
        t.Init(n);
        scanf("%d",&m);
        while(m--)
        {
            scanf("%s",op);
            if(strcmp(op,"INSERT")==0) t.Ins();
            else if(strcmp(op,"DELETE")==0) t.Del();
            else if(strcmp(op,"ADD")==0) t.Add();
            else if(strcmp(op,"REVERSE")==0) t.Rev();
            else if(strcmp(op,"MIN")==0) t.Min();
            else if(strcmp(op,"REVOLVE")==0) t.Res();
        }
    }
    return 0;
}
时间: 2024-12-23 19:37:08

又一道Splay吐血题 [POJ 3580] SuperMemo的相关文章

POJ 3580 SuperMemo

裸Splay区间操作: 内存池+区间加减+区间翻转+插入+删除+维护最值 SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8552   Accepted: 2801 Case Time Limit: 2000MS Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participa

poj 3580 SuperMemo splay树模板题

题意: 给一个序列,对其进行各种操作.在对序列仅对操作的处理上,splay是比线段树强大的,虽然均摊复杂度均为logN,但它能支持1:在某个位置插入一些连续的数,2:在某个位置删除一些连续的数.只是splay随便一些200+行. 分析: 网上各种模板介绍漫天飞,这个还算简洁明了. 代码: //poj 3580 #include <stdio.h> #define maxN 200000 int N,T,node; int a[maxN],size[maxN],left[maxN],right[

POJ 3580 - SuperMemo - [伸展树splay]

题目链接:http://poj.org/problem?id=3580 Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a memorizing game. At first, the host tells the participant a sequence of numbers, {A1, A2, ... An}. Then the h

poj 3580 SuperMemo (Splay)

poj 3580 好恶心的题目,真是各种操作都来了个遍 ... 不过Splay树真是一种神奇的东西,通过旋转就能实现各种操作,而且方法也都相差不大 . 题意: 给出一个数字序列,有6种操作: (1) ADD x y d: 第x个数到第y个数加d . (2) REVERSE x y : 将区间[x,y]中的数翻转 . (3) REVOLVE x y t :将区间[x,y]旋转t次,如1 2 3 4 5 旋转2次后就变成4 5 1 2 3 . (4) INSERT x p :在第x个数后面插入p .

poj 3580 SuperMemo (splay模板)

题意: 给出一个数字序列,有6种操作: (1) ADD x y d: 第x个数到第y个数加d . (2) REVERSE x y : 将区间[x,y]中的数翻转 . (3) REVOLVE x y t :将区间[x,y]旋转t次. (4) INSERT x p :在第x个数后面插入p . (5)DELETE x :删除第x个数 . (6) MIN x y : 查询区间[x,y]中的最小值 . 思路:splay板子题,对于区间[l,r],将l-1旋为树根,将r+1旋为根的右孩子,那么r+1的左子树

POJ 3580 SuperMemo (Splay 区间更新、翻转、循环右移,插入,删除,查询)

SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 13917   Accepted: 4352 Case Time Limit: 2000MS Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a memorizing game

Splay树(多操作)——POJ 3580 SuperMemo

对应POJ题目:点击打开链接 SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11309   Accepted: 3545 Case Time Limit: 2000MS Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a

平衡树:Splaytree POJ 3580 SuperMemo

SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 12788   Accepted: 3986 Case Time Limit: 2000MS Description Your friend, Jackson is invited to a TV show called SuperMemo in which the participant is told to play a memorizing game

POJ 3580 SuperMemo 题解

题目大意:维护一个序列,支持6种操作: 1.ADD x y D 从第x个数到第y个数都增加D 2.REVERSE x y 翻转第x个数到第y个数 3.REVOLVE x y T 从x到y,向右循环移动T次 4.INSERT x P 插入P到第x个数后面 5.DELETE x 删除第x个数 6.MIN x y 查询第x个数到第y个数之间最小值 ADD和REVERSE打标记然后及时下传就可以..REVOLVE的T可能为负还可能很大,要注意取模.REVOLVE可以通过3个REVERSE操作完成,也可以