POJ-3580-SuperMemo(splay的各种操作)
题意:对数组进行各种操作
其中 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的各种操作)的更多相关文章
- Splay树(多操作)——POJ 3580 SuperMemo
相应POJ题目:点击打开链接 SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 11309 Accept ...
- poj 3580 SuperMemo
题目连接 http://poj.org/problem?id=3580 SuperMemo Description Your friend, Jackson is invited to a TV sh ...
- POJ 3580 - SuperMemo - [伸展树splay]
题目链接:http://poj.org/problem?id=3580 Your friend, Jackson is invited to a TV show called SuperMemo in ...
- POJ 3580 SuperMemo (splay tree)
SuperMemo Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 6841 Accepted: 2268 Case Ti ...
- 平衡树(Splay):Splaytree POJ 3580 SuperMemo
SuperMemo Description Your friend, Jackson is invited to a TV show called SuperMemo in which ...
- POJ 3580 SuperMemo (FHQ_Treap)
题意:让你维护一个序列,支持以下6种操作: ADD x y d: 第x个数到第y个数加d . REVERSE x y : 将区间[x,y]中的数翻转 . REVOLVE x y t :将区间[x,y] ...
- POJ 3580 SuperMemo 伸展树
题意: 维护一个序列,支持如下几种操作: ADD x y D:将区间\([x,y]\)的数加上\(D\) REVERSE x y:翻转区间\([x,y]\) REVOLVE x y T:将区间\([x ...
- Splay 的区间操作
学完Splay的查找作用,发现和普通的二叉查找树没什么区别,只是用了splay操作节省了时间开支. 而Splay序列之王的称号可不是白给的. Splay真正强大的地方是他的区间操作. 怎么实现呢? 我 ...
- hdu 1890 Robotic SortI(splay区间旋转操作)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 题解:splay又一高级的功能,区间旋转这个是用线段树这些实现不了的,这题可以学习splay的旋 ...
随机推荐
- Java基础知识强化之集合框架笔记20:数据结构之 栈 和 队列
1. 栈 先进后出 解析图: 2. 队列 先进先出 解析图:
- Android(java)学习笔记200:Android中View动画之 XML实现 和 代码实现
1.Animation 动画类型 Android的animation由四种类型组成: XML中: alph 渐变透明度动画效果 scale 渐变尺寸伸缩动画效果 translate 画面转换位置移动动 ...
- oracle左右连接 完全连接 有效连接 心得总结
左链接 A表 Left join B表 on 条件 示例 A表 B表 SELECT * FROM A left JOIN B ON A.AID = B.BID; 结果: 左链接查询出来的数 ...
- ASP.NET MVC 第三回 Controller与View
这节我们让ASP.NET MVC真正的跑起来 一.新建Controller 首先我们自己新建一个新的Controller在Controllers上点右键,添加,Controller选项 之后出现一 ...
- 转 sqlserver字段描述相关操作sql
可以自己查询系统表: SELECT o.name AS tableName, c.name AS columnName, p.[value] AS Description FROM sysproper ...
- CSS Clip剪切元素动画实例
1.CSS .fixed { position: fixed; width: 90px; height: 90px; background: red; border: 0px solid blue; ...
- 认识k_BackingField【转】
事情从Json的序列化和反序列化说起. 在C#2.0的项目中,以前经常使用Json.Net实现序列化和反序列化.后来从c#3.0中开始使用新增的DataContractJsonSerializer进行 ...
- 函数返回一个SqlDataReader对象
解决方法: 1.尝试使用dataset 2.执行数据库操作命令 SqlDataReader reader=mySqlCommand.ExecuteReader(CommandBehavior.Clos ...
- CentOS 7设置iptables防火墙开放proftpd端口
由于ftp的被动模式是这样的,客户端跟服务器端的21号端口交互信令,服务器端开启21号端口能够使客户端登录以及查看目录.但是ftp被动模式用于传输数据的端口却不是21,而是大于1024的随机或配置文件 ...
- Java设计模式(学习整理)---策略模式
1. 模式定义 把会变化的内容取出并封装起来,以便以后可以轻易地改动或扩充部分,而不影响不需要变化的其他部分: 2.模式本质: 少用继承,多用组合,简单地说就是:固定不变的信息 ...