题意:对数组进行各种操作

其中 REVOLVE右移操作。将区间[a,b]右移c位

首先c可能比较多,可以先对区间长度取模。

在右移之后,可以发现[a,b]被分为两个区间[a,b-c]  [b-c+1,b],将后者插入到前者之前即可。

// File Name: ACM/POJ/3580.cpp
// Author: Zlbing
// Created Time: 2013年08月10日 星期六 10时51分07秒 #include<iostream>
#include<string>
#include<algorithm>
#include<cstdlib>
#include<cstdio>
#include<set>
#include<map>
#include<vector>
#include<cstring>
#include<stack>
#include<cmath>
#include<queue>
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3fffffff
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)
#define L ch[x][0]
#define R ch[x][1]
#define KT (ch[ ch[rt][1] ][0])
const int MAXN = 2e5+;
int num[MAXN];
struct SplayTree {
int sz[MAXN];
int ch[MAXN][];
int pre[MAXN];
int rt,top;
inline void down(int x){
if(flip[x]) {
flip[ L ] ^= ;
flip[ R ] ^= ;
swap(L,R);
flip[x]=;
}
if(add[x])
{
if(L)
{
add[L]+=add[x];
mi[L]+=add[x];
val[L]+=add[x];
}
if(R)
{
add[R]+=add[x];
mi[R]+=add[x];
val[R]+=add[x];
}
add[x]=;
}
}
inline void up(int x){
sz[x]=+sz[ L ] + sz[ R ];
mi[x]=min(val[x],min(mi[L],mi[R]));
}
inline void Rotate(int x,int f){
int y=pre[x];
down(y);
down(x);
ch[y][!f] = ch[x][f];
pre[ ch[x][f] ] = y;
pre[x] = pre[y];
if(pre[x]) ch[ pre[y] ][ ch[pre[y]][] == y ] =x;
ch[x][f] = y;
pre[y] = x;
up(y);
}
inline void Splay(int x,int goal){//将x旋转到goal的下面
down(x);////防止pre[x]就是目标点,下面的循环就进不去了,x的信息就传不下去了
while(pre[x] != goal){
down(pre[pre[x]]); down(pre[x]);down(x);//在旋转之前需要先下传标记,因为节点的位置可能会发生改变
if(pre[pre[x]] == goal) Rotate(x , ch[pre[x]][] == x);
else {
int y=pre[x],z=pre[y];
int f = (ch[z][]==y);
if(ch[y][f] == x) Rotate(x,!f),Rotate(x,f);
else Rotate(y,f),Rotate(x,f);
}
}
up(x);
if(goal==) rt=x;
}
inline void RTO(int k,int goal){//将第k位数旋转到goal的下面
int x=rt;
down(x);
while(sz[ L ]+ != k) {
if(k < sz[ L ] + ) x=L;
else {
k-=(sz[ L ]+);
x = R;
}
down(x);
}
Splay(x,goal);
}
void vist(int x){
if(x){
printf("结点%2d : 左儿子 %2d 右儿子 %2d %2d flip:%d\n",x,L,R,val[x],flip[x]);
printf("结点%2d mi=%2d\n",x,mi[x]);
vist(L);
vist(R);
}
}
void Newnode(int &x,int c,int f){
x=++top;flip[x]=;
L = R = ; pre[x] = f;
sz[x]=; val[x]=c;
mi[x]=c; add[x]=;
}
inline void build(int &x,int l,int r,int f){
if(l>r) return ;
int m=(l+r)>>;
Newnode(x,num[m],f);
build(L , l , m- , x);
build(R , m+ , r , x);
pre[x]=f;
up(x);
} //终于明白初始化结点0的用处了。就是所有的叶子结点,是终止结点
inline void init(int n){
ch[][]=ch[][]=pre[]=sz[]=;
rt=top=; flip[]=; val[]=;
add[]=;mi[]=INF;
Newnode(rt,INF,);
Newnode(ch[rt][],INF,rt);
sz[rt]=;
build(KT,,n,ch[rt][]);
up(ch[rt][]);up(rt);
}
void Del(){
int t=rt;
if(ch[rt][]) {
rt=ch[rt][];
RTO(,);
ch[rt][]=ch[t][];
if(ch[rt][]) pre[ch[rt][]]=rt;
}
else rt=ch[rt][];
pre[rt]=;
up(rt);
}
void ADD(int a,int b,int d)
{
if(a>b)swap(a,b);
RTO(a,);
RTO(b+,rt);
add[KT]+=d;
val[KT]+=d;
mi[KT]+=d;
}
void REVERSE(int a,int b)
{
if(a>b)swap(a,b);
RTO(a,);
RTO(b+,rt);
flip[KT]^=;
}
//t有可能为负
void REVOLVE(int x,int y,int t)
{
if(x>y)swap(x,y);
t=t%(y-x+);
t=(t+y-x+)%(y-x+);
if(t==)return;
int l=y-t+;
int r=y+;
RTO(l,);
RTO(r,rt);
int tmp=KT;
KT=;
up(ch[rt][]);up(rt); RTO(x,);
RTO(x+,rt);
KT=tmp;pre[tmp]=ch[rt][];
up(ch[rt][]);up(rt);
}
void INSERT(int x,int p)
{
RTO(x+,);
RTO(x+,rt);
Newnode(KT,p,ch[rt][]);
up(ch[rt][]);up(rt);
}
void DELETE(int x)
{
RTO(x,);
RTO(x+,rt);
KT=;
up(ch[rt][]);up(rt);
}
int MIN(int x,int y)
{
if(x>y)swap(x,y);
RTO(x,);
RTO(y+,rt);
return mi[KT];
}
int flip[MAXN];
int val[MAXN];
int mi[MAXN];
int add[MAXN];
}spt; int main()
{
int n,m;
while(~scanf("%d",&n))
{
REP(i,,n)
scanf("%d",&num[i]);
scanf("%d",&m);
spt.init(n);
char op[];
int a,b,c;
REP(i,,m)
{
scanf("%s",op);
if(op[]=='A')
{
scanf("%d%d%d",&a,&b,&c);
spt.ADD(a,b,c);
}
else if(op[]=='I')
{
scanf("%d%d",&a,&b);
spt.INSERT(a,b);
}
else if(op[]=='D')
{
scanf("%d",&a);
spt.DELETE(a);
}
else if(op[]=='M')
{
scanf("%d%d",&a,&b);
int ans=spt.MIN(a,b);
printf("%d\n",ans);
}
else if(strcmp(op,"REVERSE")==)
{
scanf("%d%d",&a,&b);
spt.REVERSE(a,b);
}
else if(strcmp(op,"REVOLVE")==)
{
scanf("%d%d%d",&a,&b,&c);
spt.REVOLVE(a,b,c);
}
}
}
return ;
}

POJ-3580-SuperMemo(splay的各种操作)的更多相关文章

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

    相应POJ题目:点击打开链接 SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 11309   Accept ...

  2. poj 3580 SuperMemo

    题目连接 http://poj.org/problem?id=3580 SuperMemo Description Your friend, Jackson is invited to a TV sh ...

  3. POJ 3580 - SuperMemo - [伸展树splay]

    题目链接:http://poj.org/problem?id=3580 Your friend, Jackson is invited to a TV show called SuperMemo in ...

  4. POJ 3580 SuperMemo (splay tree)

    SuperMemo Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 6841   Accepted: 2268 Case Ti ...

  5. 平衡树(Splay):Splaytree POJ 3580 SuperMemo

    SuperMemo         Description Your friend, Jackson is invited to a TV show called SuperMemo in which ...

  6. POJ 3580 SuperMemo (FHQ_Treap)

    题意:让你维护一个序列,支持以下6种操作: ADD x y d: 第x个数到第y个数加d . REVERSE x y : 将区间[x,y]中的数翻转 . REVOLVE x y t :将区间[x,y] ...

  7. POJ 3580 SuperMemo 伸展树

    题意: 维护一个序列,支持如下几种操作: ADD x y D:将区间\([x,y]\)的数加上\(D\) REVERSE x y:翻转区间\([x,y]\) REVOLVE x y T:将区间\([x ...

  8. Splay 的区间操作

    学完Splay的查找作用,发现和普通的二叉查找树没什么区别,只是用了splay操作节省了时间开支. 而Splay序列之王的称号可不是白给的. Splay真正强大的地方是他的区间操作. 怎么实现呢? 我 ...

  9. hdu 1890 Robotic SortI(splay区间旋转操作)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题解:splay又一高级的功能,区间旋转这个是用线段树这些实现不了的,这题可以学习splay的旋 ...

随机推荐

  1. 趣谈iOS运行时的方法调用原理

    一个成熟的计算机语言必然有丰富的体系,复杂的容错机制,处理逻辑以及判断逻辑.但这些复杂的逻辑都是围绕一个主线丰富和展开的,所以在学习计算机语言的时候,先掌握核心,然后了解其原理,明白程序语言设计的实质 ...

  2. Python CMDB开发

    Python CMDB开发   运维自动化路线: cmdb的开发需要包含三部分功能: 采集硬件数据 API 页面管理 执行流程:服务器的客户端采集硬件数据,然后将硬件信息发送到API,API负责将获取 ...

  3. Maven Integration for Eclipse 正确地址

    m2eclipse has moved from sonatype to eclipse. The correct update site is http://download.eclipse.org ...

  4. GitHub Desktop安装异常解决

    为了更好的共同学习,共同进步,哥们推荐我使用GitHub记录自己每天的学习记录,当下很火的提供一个分布式的版本控制系统(Git)服务的网站,GitHub提供GitHub Desktop桌面程序方便协同 ...

  5. Android开发需要注意的地方

          1.理解运用商场概略 开发者对商场状况的理解与APP的胜利紧密相连,往常,AppStore和GooglePlay能够说是挪动运用最为丰厚的运用生态,像苹果的下载计算表单会记载抢手运用的下载 ...

  6. GUID是什么意思及Guid在sqlserver中的使用

    GUID(全球唯一标识)是微软使用的一个术语,由一个特定的算法,给某一个实体,如Word文档,创建一个唯一的标识,GUID值就是这个唯一的标识码.GUID广泛应用于微软的产品中,用于识别接口.复制品. ...

  7. HDOJ 2036

    错误代码: #include<stdio.h>#include<math.h>int main(){ int x[102],y[102]; int i,n; float s,a ...

  8. 从XML文件中获取格式化的文本信息

    在FMW的运维过程中,时常需要将中间传输的XML信息转换为excel格式化的问题提交给关联系统人员,现总结三种格式化问题提供方式 一.使用Excel转换 因为从系统中取到的xml文档为中间信息文档,需 ...

  9. c++异常机制实现原理

    今天突然看到一篇文章,讲异常机制的实现,所以分享一下:http://baiy.cn/doc/cpp/inside_exception.htm 内容讲的很深,但是编译器的实现是不是真是这样就不知道了,我 ...

  10. iOS 视频播放横屏,隐藏状态栏

    MPMoviePlayerViewController *moviePlayerViewController = [[MPMoviePlayerViewController alloc] init]; ...