题解:

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的更多相关文章

  1. hdu4453 Looploop 2012年杭州现场赛 Splay

    题意:维护一个圈,实现六个功能,给某位置起的一些数增加某值,反转某一段数,添加删除某些数,移动当前所指的位置, 简单的splay,把圈拆成链,对于每种操作,处理一下. #define inf 0x3f ...

随机推荐

  1. Uvalive 7037 The Problem Needs 3D Arrays(最大密度子图)

    题意:给一段子序列,定义密度:子序列中的逆序对数/子序列的长度 求这个序列的对大密度. 分析:将序列中的每个位置视作点,逆序对\(<i,j>\)之间表示点i与点j之间有一条无向边.所以就转 ...

  2. Gym 100548F Color 2014-2015 ACM-ICPC, Asia Xian Regional Contest (容斥原理+大数取模)

    题意:有N朵花,在M种颜色中选择恰好k种不同的颜色,将这N朵花染色,要求相邻的两朵花颜色不相同. 分析:若限制改为选择不超过k种颜色将N朵花朵染色,则方案数\(f(N,k) = k*(k-1)^{N- ...

  3. scala drools and map

    需求,安全检查,例如Linux系统,用户安全检查,配置项检查等,这些检查的规则在Drools里面去实现,数据传送过来即可, 问题:如何定义数据结构,不同的检查项会有不同的数据结构,如何规范呢? 思路: ...

  4. C++命名规则(转)

    如果想要有效的管理一个稍微复杂一点的体系,针对其中事物的一套统一.带层次结构.清晰明了的命名准则就是必不可少而且非常好用的工具. 活跃在生物学.化学.军队.监狱.黑社会.恐怖组织等各个领域内的大量有识 ...

  5. NodeJS NPM HTTPS

    npm config set registry http://registry.npmjs.org/

  6. $python collections库

    collections库是python内置的集合库,本文主要讲解以下5种数据结构的用法: namedtuple 命名元组,是tuple的子类 deque 双向列表 defaultdict 有默认值的字 ...

  7. Git笔记之初识vi编辑器

    1.vi编辑器 如同Windows下的记事本,vi编辑器是Linux下的标配,通过它我们可以创建.编辑文件.它是一个随系统一起安装的文本编辑软件. vi编辑器提供了3种模式,分别是命令模式.插入模式. ...

  8. Finder Quick Menu FAQ

    How to use Finder Quick Menur: 1. Start Finder Quick Menu.2. Open "System Preferences -> Ext ...

  9. 批处理命令 For循环命令详解!

    批处理for命令详解FOR这条命令基本上都被用来处理文本,但还有其他一些好用的功能!看看他的基本格式(这里我引用的是批处理中的格式,直接在命令行只需要一个%号)FOR 参数 %%变量名 IN (相关文 ...

  10. 浅析ProcessBuilder

    概述 ProcessBuilder类是J2SE 1.5在java.lang中新添加的一个新类,此类用于创建操作系统进程,它提供一种启动和管理进程(也就是应用程序)的方法.在J2SE 1.5之前,都是由 ...