hdu4453
题解:
splay模板
删除,翻转等等
代码:
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
const int M=;
int root,N,ch[M][],pre[M],a[M],size[M],key[M],add[M],rev[M],s[M],tot2,tot1;
#define Key_value ch[ch[root][1]][0]
void NewNode(int &r,int f,int val)
{
if (tot2)r=s[tot2--];
else r+=++tot1;
ch[r][]=ch[r][]=;
pre[r]=f;
size[r]=;
key[r]=val;
rev[r]=;
add[r]=;
}
void pushup(int r){size[r]=size[ch[r][]]+size[ch[r][]]+;}
void build(int &x,int l,int r,int f)
{
if (l>r)return;
int mid=(l+r)>>;
NewNode(x,f,a[mid]);
build(ch[x][],l,mid-,x);
build(ch[x][],mid+,r,x);
pushup(x);
}
void update_add(int r,int x)
{
if (!r)return;
add[r]+=x;
key[r]+=x;
}
void update_rev(int r)
{
if (!r)return;
swap(ch[r][],ch[r][]);
rev[r]^=;
}
void pushdown(int r)
{
if (add[r])
{
update_add(ch[r][],add[r]);
update_add(ch[r][],add[r]);
add[r]=;
}
if (rev[r])
{
update_rev(ch[r][]);
update_rev(ch[r][]);
rev[r]=;
}
}
void Rotate(int x,int kind)
{
int y=pre[x];
pushdown(y);
pushdown(x);
ch[y][!kind]=ch[x][kind];
pre[ch[x][kind]]=y;
if (pre[y])ch[pre[y]][ch[pre[y]][]==y]=x;
pre[x]=pre[y];
ch[x][kind]=y;
pre[y]=x;
pushup(y);
}
void Splay(int r,int goal)
{
pushdown(r);
while(pre[r]!=goal)
{
if (pre[pre[r]]==goal)
{
pushdown(pre[r]);
pushdown(r);
Rotate(r,ch[pre[r]][]==r);
}
else
{
pushdown(pre[pre[r]]);
pushdown(pre[r]);
pushdown(r);
int y=pre[r],kind=(ch[pre[y]][]==y);
if (ch[y][kind]==r)
{
Rotate(r,!kind);
Rotate(r,kind);
}
else
{
Rotate(y,kind);
Rotate(r,kind);
}
}
}
pushup(r);
if (goal==)root=r;
}
int get_kth(int r,int k)
{
pushdown(r);
int t=size[ch[r][]]+;
if (t==k)return r;
if (t>k)return get_kth(ch[r][],k);
else return get_kth(ch[r][],k-t);
}
void change(int &poss,int k)
{
int pos=get_kth(root,poss);
Splay(pos,);
int tmp=size[ch[root][]]+;
Splay(get_kth(root,),);
Splay(get_kth(root,tmp),root);
tmp=Key_value;
Key_value=;
pushup(ch[root][]);
pushup(root);
Splay(get_kth(root,size[root]-),);
Key_value=tmp;
pre[Key_value]=ch[root][];
pushup(ch[root][]);
pushup(root);
Splay(get_kth(root,),);
Splay(get_kth(root,k+),root);
}
void Add(int &poss,int k2,int x)
{
change(poss,k2);
update_add(Key_value,x);
pushup(ch[root][]);
pushup(root);
poss=;
}
void Reverse(int &poss,int k1)
{
change(poss,k1);
update_rev(Key_value);
pushup(ch[root][]);
pushup(root);
poss=;
}
void Insert(int &poss,int x)
{
int pos=get_kth(root,poss);
Splay(pos,);
Splay(get_kth(root,size[ch[root][]]+),root);
NewNode(Key_value,ch[root][],x);
pushup(ch[root][]);
pushup(root);
}
void erase(int r)
{
if (r)
{
s[++tot2]=r;
erase(ch[r][]);
erase(ch[r][]);
}
}
void Delete(int &poss)
{
int pos=get_kth(root,poss);
Splay(pos,);
int tmp=size[ch[root][]]+;
Splay(get_kth(root,tmp-),);
Splay(get_kth(root,tmp+),root);
erase(Key_value);
pre[Key_value]=;
Key_value=;
pushup(ch[root][]);
pushup(root);
if (tmp==size[root])poss=;
else poss=tmp;
}
void Move(int &poss,int x)
{
int pos=get_kth(root,poss);
if (x==)
{
Splay(pos,);
int tmp=size[ch[root][]];
if (size[ch[root][]]==)tmp=size[root]-;
poss=tmp;
}
else
{
Splay(pos,);
int tmp=size[ch[root][]]+;
if (tmp==size[root])tmp=;
poss=tmp;
}
}
int Query(int &poss)
{
int pos=get_kth(root,poss);
Splay(pos,);
return key[root];
}
int main()
{
int n,m,k1,k2,t=;
while(scanf("%d%d%d%d",&n,&m,&k1,&k2)!=EOF)
{
N=n;
if (n==&&m==&&k1==&&k2==)break;
printf("Case #%d:\n",t++);
for (int i=;i<=n;i++)scanf("%d",&a[i]);
int poss=,sum=n;
root=tot1=tot2=;
ch[root][]=ch[root][]=size[root]=key[root]=add[root]=pre[root]=;
rev[root]=;
NewNode(root,,-);
NewNode(ch[root][],root,-);
build(Key_value,,N,ch[root][]);
pushup(ch[root][]);
pushup(root);
for (int i=;i<=m;i++)
{
char a[];
scanf("%s",a);
if (a[]=='q')
{
int res=Query(poss);
printf("%d\n",res);
}
else if (a[]=='r')Reverse(poss,k1);
else if (a[]=='i')
{
int x;
sum+=;
scanf("%d",&x);
Insert(poss,x);
}
else if (a[]=='d')
{
sum-=;
Delete(poss);
}
else if (a[]=='m')
{
int x;
scanf("%d",&x);
if (x==)
{
if (poss==)poss=sum+;
else poss-=;
}
if (x==)
{
if (poss==sum+)poss=;
else poss+=;
}
}
else if (a[]=='a')
{
int x;
scanf("%d",&x);
Add(poss,k2,x);
}
}
}
return ;
}
hdu4453的更多相关文章
- hdu4453 Looploop 2012年杭州现场赛 Splay
题意:维护一个圈,实现六个功能,给某位置起的一些数增加某值,反转某一段数,添加删除某些数,移动当前所指的位置, 简单的splay,把圈拆成链,对于每种操作,处理一下. #define inf 0x3f ...
随机推荐
- 理解android分辨率限定符 layout-sw360dp
首先,我们来看看单位dp是怎么一回事,dp是一种单位,使用它,你可以在高.低分辨率上获得一样的空间表现,也就是说,它是像素无关的. dp计算公式:160*pix/ppi(pix是你要计算的高度或者宽度 ...
- springcloud---2
每一个都是独立的springboot工程.通过自己的ip和端口访问. Eureka是服务发现组件,Eureka里面有一个服务注册表,存的是服务消费者和服务生产者的ip和端口.Eureka集群里面每个E ...
- android - Session 'app': Error Installing APK
File->Settings->Build,Execuion,Deployment->Instant Run 然后把Enable Instant Run to...那一项的勾去掉就好 ...
- 20145335《java程序设计》第10周学习总结
20145335郝昊 <Java程序设计>第10周学习总结 教材学习内容总结 网络编程 网络编程就是在两个或两个以上的设备(例如计算机)之间传输数据.程序员所作的事情就是把数据发送到指定的 ...
- SpringBoot 打包为Docker进行
可以有两种方式: 1.dockerfile 2.maven docker 第一种方式:通过dockerfile打包Docker镜像 1.将dockerfile和 springboot打包的jar文件放 ...
- 从零开始玩转JMX(二)——Condition
Notification 一个MBean提供的管理接口允许代理对其管理资源进行控制和配置.然而,对管理复杂的分布式系统来说,这些接口知识提供了一部分功能.通常,管理应用程序需要对状态变化或者当特别情况 ...
- maven中pom.xml解释
知识点:解释maven中,各个标签的含义 转载:http://blog.sina.com.cn/s/blog_534f69a001010lpv.html (1)Introduce maven项目的核心 ...
- java程序结构
if....else.... 1. if都需要接判断表达式 2. else不需要表达式 3. 有if没else可以,但else必须要有一个if,if数>=else数 if (A条件) ...
- poj 2528 Mayor's posters 线段树+离散化 || hihocode #1079 离散化
Mayor's posters Description The citizens of Bytetown, AB, could not stand that the candidates in the ...
- 读懂 ECMAScript 规格
概述 规格文件是计算机语言的官方标准,详细描述语法规则和实现方法. 一般来说,没有必要阅读规格,除非你要写编译器.因为规格写得非常抽象和精炼,又缺乏实例,不容易理解,而且对于解决实际的应用问题,帮助不 ...