题目链接:http://poj.org/problem?id=3580
——————————————————————————————————————————
SuperMemo
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 15846 Accepted: 4992
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:

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,5,5}
REVERSE x y: reverse the sub-sequence {Ax … Ay}. For example,performing “REVERSE 2 4” on {1,5}
REVOLVE x y T: rotate sub-sequence {Ax … Ay} T times. For example,performing “REVOLVE 2 4 2” on {1,5}
INSERT x P: insert P after Ax. For example,performing “INSERT 2 4” on {1,5}
DELETE x: delete Ax. For example,performing “DELETE 2” on {1,5}
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,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

——————————————————————————————————————————

题意:
  给出一个数字序列,有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 。

    (5)DELETE x :删除第x个数 。

    (6) MIN x y : 查询区间[x,y]中的最小值 。

本来不想写来着 但想到 好多天没有更新博客了,加上这题还是挺好玩儿的,还是应该更新一波吧。

就是区间加,翻转,剪切,询问最值。点插入,删除。这几个操作

有翻转了 所以用SPLAY来维护一下

区间加 区间最小值就不说了 和普通的二叉搜索树一模一样.

点插入 删除

假如要插入的点在x
那么让x-1做为树根,x+1伸展到根节点下面,那么x+1的左儿子就是空出来的 加个值就好了
删除发过来一样的

区间操作

对于区间[l,r]
那么让l-1做为树根,r+1伸展到根节点下面,那么r+1的左儿子就是这个区间

但为了更好的处理[1,n]这个区间 加上个0和n+1这两个节点

翻转

同样在一个二叉树中 翻转也就是让每个节点的两个儿子交换一下顺序就好了,打个标记 就行了,

旋转

其实旋转说白了就是将这个区间分成两段然后交换一下子,

所以我们可以将后一个区间处理到一个子树上,然后放到 l1,l 这两个节点之间,就好了,先减掉,然后在加上去就好了

注: 个人的SPLAY模板正在建设,这个的代码比较杂乱,见谅.

附本题代码
——————————————————————————————————————————

//#include <bits/stdc++.h>
#include <stdio.h>

typedef long long int LL;

const int N = 200000+7;

inline int read(){
    int x=0,f=1;char ch=getchar();
    for(;ch<'0'||'9'<ch;ch=getchar())   if(ch=='-')f=-1;
    for(;'0'<=ch&&ch<='9';ch=getchar()) x=(x<<3)+(x<<1)+ch-'0';
    return x*f;
}

/*******************************************************/

/*************SPLAY-tree************/

int n,m;

int ch[N][2];  //ch[][0] lson ch[][1] rson
int f[N];      //father
int sz[N];     //size
int val[N];    //value of node_i
int lazy[N];   //lazy-tag
int mi[N];     //min of son-tree : root of i
int rev[N];    //tag of revear
int root;      //root of splay-tree
int tot;       //tot,total,is the number of node of tree

void myswap(int &x,int &y){
    x^=y,y^=x,x^=y;
}
int min(int x,int y){
    return (x<y)?x:y;
}
void update_rev(int x){
    if(!x) return ;
    myswap(ch[x][0],ch[x][1]);
    rev[x]^=1;
}

void update_add(int x,int v){
    if(x) lazy[x]+=v,val[x]+=v,mi[x]+=v;
}

void pushdown(int x){
    if(!x) return ;
    if(lazy[x]){
        update_add(ch[x][0],lazy[x]);
        update_add(ch[x][1],lazy[x]);
        lazy[x]=0;
    }
    if(rev[x]){
        update_rev(ch[x][0]);
        update_rev(ch[x][1]);
        rev[x]=0;
    }
}

void pushup(int x){
    if(!x)return ;
    sz[x]=1,mi[x]=val[x];
    if(ch[x][0])sz[x]+=sz[ch[x][0]],mi[x]=min(mi[x],mi[ch[x][0]]);
    if(ch[x][1])sz[x]+=sz[ch[x][1]],mi[ch[x][1]]);
}

void rotate(int x,int k){   // k = 0 左旋, k = 1 右旋
    int y=f[x];int z=f[y];
    pushdown(y),pushdown(x);
    ch[y][!k]=ch[x][k];if(ch[x][k])f[ch[x][k]]=y;
    f[x]=z;if(z)ch[z][ch[z][1]==y]=x;
    f[y]=x;ch[x][k]=y;
    pushup(y),pushup(x);
}
/*** 这样的SPLAY 不好么? 相比分6种旋转的 zig-zag */
void splay(int x,int goal){
    for(int y=f[x];f[x]!=goal;y=f[x])
        rotate(x,(ch[y][0]==x));
    if(goal==0) root=x;
}

void newnode(int rt,int v,int fa){
// printf("%d <---\n",rt);
    f[rt]=fa;
    mi[rt]=val[rt]=v;sz[rt]=1;
    ch[rt][0]=ch[rt][1]=rev[rt]=lazy[rt]=0;
}
void delnode(int rt){
    f[rt]=mi[rt]=val[rt]=sz[rt]=0;
    ch[rt][0]=ch[rt][1]=rev[rt]=lazy[rt]=0;
}
void build(int &rt,int l,int r,int fa){
    if(l>r) return ;
    int m = r+l >> 1;
    rt=m; newnode(rt,val[rt],fa);
    build(ch[rt][0],l,m-1,rt);
    build(ch[rt][1],m+1,r,rt);
    pushup(rt);
}

void init(int n){
    root=0;
    f[0]=sz[0]=ch[0][0]=ch[0][1]=rev[0]=0;
    build(root,1,n,0);
    pushup(root);
}

/***************************以下是DEBUG***************************/

void Traversal(int rt){
    if(!rt) return;
    pushdown(ch[rt][0]);Traversal(ch[rt][0]);
    printf("%d f[]=%d sz[]=%d lson=%d rson=%d val[]=%d mi[]=%d \n",rt,f[rt],sz[rt],ch[rt][0],ch[rt][1],mi[rt]);
    pushdown(ch[rt][1]);Traversal(ch[rt][1]);
    pushup(rt);
}
void debug(){
    printf("ROOT = %d <---\n",root);
    pushdown(root);
    Traversal(root);
}

/**************************以下是前置操作**************************/

//以x为根的子树 的最左节点
int x_left(int x){
    for(pushdown(x);ch[x][0];pushdown(x)) x=ch[x][0];
    return x;
}
//以x为根的子树 的最右节点
int x_right(int x){
    for(pushdown(x);ch[x][1];pushdown(x)) x=ch[x][1];
    return x;
}
//以x为根的子树 第k个数的位置
int kth(int x,int k){
    pushdown(x);
    if(sz[ch[x][0]]+1 == k) return x;
    else if(sz[ch[x][0]]>=k) return kth(ch[x][0],k);
    else return kth(ch[x][1],k-sz[ch[x][0]]-1);
}

/***************************以下是正经操作**************************/
/*** 如果有区间为[1,n]情况不好处理, 所以我们可以 多添加一个head,一个tail 这样的话区间[1,n]就是tail的左儿子了,*/
//区间交换
void exchange(int l1,int r1,int l2,int r2){
    int x=kth(root,l2-1),y=kth(root,r2+1);
    splay(x,0),splay(y,x);
    int tmp_right = ch[y][0]; ch[y][0]=0;
    x=kth(root,l1-1),l1);
    splay(x,x);
    ch[y][0] = tmp_right;
    f[tmp_right]=y;
}

//区间翻转
void reversal(int l,int r){
    int x=kth(root,l-1),r+1);
    splay(x,0);splay(y,x);
    update_rev(ch[y][0]);
}

//区间加
void add(int l,int v){
    int x=kth(root,x);
    update_add(ch[y][0],v);
}

//按照二叉排序树性质插入x
void _insert(int x){
    /** 其实我们也可以 将插入后临街的两个节点 a x b 将a伸展到根 b伸展到 根下 那么b的左儿子一定没有,插进去就行了, */
}

//在第k个数后插入值为x的节点
void _insert(int k,int x){
    int r=kth(root,k),rr=kth(root,k+1);
    splay(r,splay(rr,r);
// puts("begin insert <-------------------");
// printf("%d %d %d\n",rr,tot);
// debug();
// puts("end insert <-------------------");
    newnode(++tot,x,rr);ch[rr][0]=tot;
    for(r=rr;r;r=f[r])pushdown(r),pushup(r);
    splay(rr,0);
}

//删除第k个数
void _delete(int k){
    splay(kth(root,k-1),0);
    splay(kth(root,k+1),root);
    delnode(ch[ch[root][1]][0]);
    ch[ch[root][1]][0]=0;
    pushup(ch[root][1]);
    pushup(root);
}

//int get_max(int l,int r){
// int x=kth(root,l-1),r+1);
// splay(x,0);splay(y,x);
// return mx[ch[y][0]];
//}

int get_min(int l,x);
    return mi[ch[y][0]];
}

/*****************************************************/

char s[12];

int main(){
    scanf("%d",&n);
    val[1]=val[n+2]=1000000000;
    for(int i=1+1;i<=n+1;i++) val[i]=read();
    tot=n+2;init(n+2);
    scanf("%d",&m);
    for(int i=1,d,v;i<=m;i++){
        scanf("%s",s);
        if(s[0]=='A'){ //ADD
            scanf("%d%d%d",&l,&r,&d);
            add(l+1,r+1,d);
        }
        else if(s[0]=='I'){ //INSERT
            scanf("%d%d",&d);
            _insert(l+1,d);
        }
        else if(s[0]=='M'){ //MIN
            scanf("%d%d",&r);
            printf("%d\n",get_min(l+1,r+1));
        }
        else if(s[0]=='D'){ //DELETE
            scanf("%d",&l);
            _delete(l+1);
        }
        else if(s[3]=='E'){ //REVERSE
            scanf("%d%d",&r);
            reversal(l+1,r+1);
        }
        else { //REVOLVE
            scanf("%d%d%d",&d);
            d=(d%(r-l+1)+r-l+1)%(r-l+1);
            if(d) exchange(l +1,r-d +1,r-d+1 +1,r +1);
        }
// debug();
    }
// debug();

    return 0;
}

/**** 5 1 2 3 4 5 6 ADD 1 3 1 INSERT 3 3 DELETE 4 MIN 1 5 MIN 2 5 REVOLVE 1 5 2 10 1 2 3 4 5 6 7 8 9 10 15 ADD 4 8 3 MIN 5 7 MIN 7 10 REVERSE 2 5 MIN 2 6 MIN 2 3 INSERT 3 4 MIN 3 4 MIN 5 10 DELETE 6 MIN 3 5 MIN 4 4 REVOLVE 3 6 7 MIN 5 8 MIN 7 10 // */

BZOJ 1895 & POJ 3580 supermemo [SPLAY]【数据结构】的更多相关文章

  1. 伸展树(SPLAY)个人总结+模板 [平衡树]【数据结构】【模板】

    前言最近3个月内,无论是现场赛还线上赛中SPLAY出现的概率大的惊人啊啊啊!!!然而不会的我就GG了,同时发现大家都会SPLAY,,,,然后就学习了一波。——————————————————————————-附上整体代码-md贴上来太卡了,去题解里看吧维护序列的维护一堆数的

  2. BZOJ 1895 &amp; POJ 3580 supermemo [SPLAY]【数据结构】

    Ay}Ttimes.Forexample,performing“REVOLVE242”on{1,5}INSERTxP:insertPafterAx.Forexample,performing“INSERT24”on{1,5}DELETEx:deleteAx.Forexample,performing“DELETE2”on{1,5}MINxy:querytheparticipantwhatistheminimumnumberinsub-sequence{Ax…Ay}.Forexample,thecorrec

  3. BZOJ 3224: Tyvj 1728 普通平衡树 [Splay]【数据结构】

    于是学了学Splay其实Splay很好懂只要知道平衡树的左旋和右旋操作,知道作用是吧一棵树做矮,就是不使它的某一条链变成。本质还是一颗二叉树,只不过多了伸展的操作,也就是能通过左旋,右旋降低树高,保证某些操作总是在O的不会变成O。附模板————————————————————————————————————

随机推荐

  1. 【数据结构】单调栈

    显然,每个发射站发来的能量有可能被0或1或2个其他发射站所接受,特别是为了安全,每个发射站接收到的能量总和是我们很关心的问题。由于数据很多,现只需要你帮忙计算出接收最多能量的发射站接收的能量是多少。输入输出格式输入格式:第1行:一个整数N;第2到N+1行:第i+1行有两个整数Hi和Vi,表示第i个人发射站的高度和发射的能量值。输入输出样例输入样例:34235610输出样例:7题解中有讲解代码实现

  2. BZOJ 1798 [Ahoi2009] Seq 维护序列seq [线段树+多重标记下传]【数据结构】

    有长为N的数列,不妨设为a1,a2,…Input第一行两个整数N和P。第二行含有N个非负整数,从左到右依次为a1,aN,。表示把所有满足t≤i≤g的ai改为ai×c。操作2:“2tgc”。同一行相邻两数之间用一个空格隔开,每行开头和末尾没有多余空格。Output对每个操作3,按照它在输入中出现的顺序,依次输出一行一个整数表示询问结果。SampleInput7431234567512553242379313347SampleOutput2358HINT初始时数列为。对第5次操作,和为29+34+15+16=

  3. 陈越《数据结构》第一讲 基本概念

    陈越《数据结构》第一讲基本概念1什么是数据结构1.1引子例子:如何在书架上摆放图书?数据结构是:1.数据对象在计算机中的组织方式;2.数据对象必定与一系列加在其上的操作相关联;3.完成这些操作所用的方法就是算法。抽象数据类型数据类型-数据对象集;-数据集合相关联的操作集。抽象-与存放数据的机器无关;-与数据存储的物理结构无关;-与实现操作的算法和编程语言均无关。

  4. 陈越《数据结构》第二章 线性结构

    表中元素个数称为线性表的长度;线性表没有元素时,称为空表;表起始位置称表头,表结束位置称表尾。插入和删除操作只能在链栈的栈顶进行。

  5. 【数据结构】

    非线性结构:线性结构的元素之间具有线性关系,非线性结构中的元素之间不再是序列的关系,他们呈现的是更复杂的层次关系,即一个数据元素有且仅有一个直接前驱,但可有另个或者多个直接后继,显然比序列关系复杂常见非线性结构:树,图散列表PHP中的hashtable就是哈希表就是由数组和链表组成,一个长度为16的数组中,每个元素存储的是一个链表的头结点。

  6. 【数据结构】【C++STL】FIFO队列&amp;优先队列

    首先都需要打头文件queueFIFO队列是先进先出的就好像排队一样STL定义FIFO队列优先队列的话是有优先级存在的STL定义优先队列定义方式都是大根堆FIFO队列和优先队列都有一些操作COYG

  7. 【数据结构】 堆

    自底向上://增加/减少已有节点值Heap_Increase_Key//向堆插入新的节点HeapInsert自顶向下://替换堆顶后,维持堆函数KeepHeap//弹出堆顶函数Pop

  8. 【数据结构】链表

    线性表的顺序存储结构有存储密度高及能够随机存取等优点,但存在以下不足:线性表的链式存储(单链表)的实现单向循环链表的实现

  9. 伸展树(SPLAY)个人总结+模板 [平衡树]【数据结构】【模板】

    前言最近3个月内,无论是现场赛还线上赛中SPLAY出现的概率大的惊人啊啊啊!!!然而不会的我就GG了,同时发现大家都会SPLAY,,,,然后就学习了一波。——————————————————————————-附上整体代码-md贴上来太卡了,去题解里看吧维护序列的维护一堆数的

  10. BZOJ 1895 &amp; POJ 3580 supermemo [SPLAY]【数据结构】

    Ay}Ttimes.Forexample,performing“REVOLVE242”on{1,5}INSERTxP:insertPafterAx.Forexample,performing“INSERT24”on{1,5}DELETEx:deleteAx.Forexample,performing“DELETE2”on{1,5}MINxy:querytheparticipantwhatistheminimumnumberinsub-sequence{Ax…Ay}.Forexample,thecorrec

返回
顶部