SuperMemo(POJ 3580)

SuperMemo

Time Limit: 5000MS   Memory Limit: 65536K
     
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, {A1, A2, ... 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 ... Ay} T 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 n (n ≤ 100000).

The following n lines describe the sequence.

Then follows M (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

Source

POJ Founder Monthly Contest – 2008.04.13, Yao Jinyu

这题应该是splay维护东西比较全的一题了。。注意标记下传的时候T[i].v+=Mark;我因为这个WA了一次。。

Codes:

#include<set>
#include<queue>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 100100;
#define fa(i) (T[i].p)
#define L(i) (T[i].s[0])
#define R(i) (T[i].s[1])
#define Key (L(R(root)))
#define Loc(i) (T[fa(i)].s[1] == i)
#define For(i,n) for(int i=1;i<=n;i++)
#define Rep(i,l,r) for(int i=l;i<=r;i++)
#define Sets(a,b,c) {if(a) T[a].s[c] = b; if(b) fa(b) = a;}
struct tnode{
    int s[2],v,size,p;
    int mark,Min,rev;
}T[N<<1];

int n,m,root,tot,A[N],d;

int read(){
    int num = 0 , q = 1; char ch = getchar();
    while(ch>‘9‘||ch<‘0‘) {
        if(ch==‘-‘) q = -1;
        ch = getchar();
    }
    while(ch>=‘0‘&&ch<=‘9‘){
        num = num * 10 + ch - ‘0‘;
        ch = getchar();
    }
    return num * q;
}

void Update(int i){
    T[i].size = T[L(i)].size + T[R(i)].size + 1;
    T[i].Min = min(T[L(i)].Min,T[R(i)].Min);
    T[i].Min = min(T[i].Min,T[i].v);
}

void Pushdown(int i){
    int D = T[i].mark;
    if(T[i].mark){
        if(L(i)) {T[L(i)].mark+=D; T[L(i)].Min+=D;T[L(i)].v+=D;}
        if(R(i)) {T[R(i)].mark+=D; T[R(i)].Min+=D;T[R(i)].v+=D;}
        T[i].mark = 0;
    }
    if(T[i].rev){
        if(L(i)) T[L(i)].rev ^= 1;
        if(R(i)) T[R(i)].rev ^= 1;
        swap(L(i),R(i));
        T[i].rev = 0;
    }
}

int Rank(int kth,int i){
    Pushdown(i);
    if(T[L(i)].size + 1 == kth)     return i;
    else if(T[L(i)].size >= kth)    return Rank(kth,L(i));
    else                            return Rank(kth - T[L(i)].size - 1 , R(i));
}

void Rot(int x){
    int y = fa(x) , z = fa(y);
    int lx = Loc(x) , ly = Loc(y);
    Sets(y,T[x].s[!lx],lx);
    Sets(z,x,ly);
    Sets(x,y,!lx);
    Update(y);
}

void Splay(int i,int goal){
    while(fa(i)!=goal){
        if(fa(fa(i))!=goal) Rot(fa(i));
        Rot(i);
    }
    Update(i);
    if(!goal) root = i;
}

void Build(int l,int r,int p,int &i){
    if(l>r) return;
    int m = (l+r)>>1;
    T[i=++tot].p = p;T[i].v = A[m]; T[i].size = 1;T[i].Min = T[i].v;
    Build(l,m-1,i,L(i));Build(m+1,r,i,R(i));
    Update(i);
}
char op[10];
int x,y,D;

int main(){
    #ifndef ONLINE_JUDGE
        freopen("super.in","r",stdin);
        freopen("super.out","w",stdout);
    #endif
    n = read();T[0].Min = T[0].v = A[0] = A[n+1] = 2147483647;
    For(i,n) A[i] = read();
    Build(0,n+1,0,root);
    T[Rank(n+2,root)].Min = 2147483647;
    m = read();
    For(i,m){
        scanf("%s",&op);
        if(op[0]==‘A‘){
            x = read(); y = read(); D = read();
            Splay(Rank(x,root),0);
            Splay(Rank(y+2,root),root);
            T[Key].v+=D;T[Key].Min+=D;T[Key].mark+=D;
            Update(R(root));Update(root);
        }else
        if(op[0]==‘M‘){
            x = read(); y = read();
            Splay(Rank(x,root),0);
            Splay(Rank(y+2,root),root);
            printf("%d\n",T[Key].Min);
        }else
        if(op[0]==‘I‘){
            x = read(); D = read();
            Splay(Rank(x+1,root),0);
            Splay(Rank(x+2,root),root);
            Key = ++tot;
            T[Key].p = R(root);T[Key].size = 1;
            T[Key].v = T[Key].Min = D;
            Update(R(root));Update(root);
        }else
        if(op[0]==‘D‘){
            x = read();
            Splay(Rank(x,root),0);
            Splay(Rank(x+2,root),root);
            T[Key].p = 0; Key = 0;
            Update(R(root));Update(root);
        }else
        if(op[3]==‘E‘){
            x = read(); y = read();
            Splay(Rank(x,root),0);
            Splay(Rank(y+2,root),root);
            T[Key].rev ^= 1;
        }else
        if(op[3]==‘O‘){
            x = read(); y = read(); D = read();
            while(D<0) D+=(y-x+1);
            D%=(y-x+1);
            Splay(Rank(y-D+1,root),0);
            Splay(Rank(y+2,root),root);
            int tkey = Key;Key = 0;
            Update(R(root));Update(root);
            Splay(Rank(x,root),0);
            Splay(Rank(x+1,root),root);
            Sets(R(root),tkey,0);
            Update(R(root));Update(root);
        }
    }
    return 0;
}

SuperMemo(POJ 3580)

时间: 2024-09-30 11:08:16

SuperMemo(POJ 3580)的相关文章

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)

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树模板题

题意: 给一个序列,对其进行各种操作.在对序列仅对操作的处理上,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 Splay

G - SuperMemo Time Limit:5000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 3580 Appoint description:  System Crawler  (2014-11-27) Description Your friend, Jackson is invited to a TV show called SuperMemo in

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

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

又一道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.

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

平衡树: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