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

其中 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. 基于GPUImage的实时美颜滤镜

    1.背景 前段时间由于项目需求,做了一个基于GPUImage的实时美颜滤镜.现在各种各样的直播.视频App层出不穷,美颜滤镜的需求也越来越多.为了回馈开源,现在我把它放到了GitHub https:/ ...

  2. javascript中,你真的会用console吗?

    使用console进行性能测试和计算代码运行时间 对于前端开发人员,在开发过程中经常需要监控某些表达式或变量的值,如果使用用debugger会显得过于笨重,最常用的方法是会将值输出到控制台上方便调试. ...

  3. HTML5 WebAudioAPI(四)--绘制频谱图2

    绘制分析器数组所有数据.本文内容,承接上文 1.800宽度绘制 var url='../content/audio/海阔天空.mp3'; if (!window.AudioContext) { ale ...

  4. HTML5 文件域+FileReader 分段读取文件(四)

    一.分段读取txt文本 HTML: <div class="container"> <div class="panel panel-default&qu ...

  5. Mac下载并编译Google安卓AOSP项目代码

    Mac下载并编译Google安卓AOSP项目代码 参考 https://source.android.com/source/index.html 这两天用Mac下载安卓AOSP源码,且把遇到的问题记下 ...

  6. iOS、mac开源项目及库汇总

    原文地址:http://blog.csdn.net/qq_26359763/article/details/51076499    iOS每日一记------------之 中级完美大整理 iOS.m ...

  7. 段落排版--行间距, 行高(line-height)

    <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content ...

  8. SGU 181.X-Sequence

    时间限制:0.5s 空间限制:4M 题意: 令X0=A, Xi=(a*Xi-1^2,b*Xi-1+c)%m; 求Xk,(0<=k<=109),(0<=a,b<=100),(1& ...

  9. print 函数的进一步理解

    没有括号的时候,pritn是列表操作符,会把其后列表里所有东西都数出来. 但是假如print后面紧跟着左括号,它就是一个函数调用,只会将括号内的东西输出来. “假如它看起来像函数调用,它就是一个函数调 ...

  10. 获取IP城市

     新浪的接口 : http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=js 多地域测试方法:http://int.dpool.sina. ...