题解:

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. iOS 个人所得税 app 基础解析实践

    前言:2019年 新个税实施在即,全国几乎所有在职员工都会下载“个人所得税”app来使用,并且 注册使用过程需要填写身份证号等相当私密重要的个人信息. 至今,各大app平台应用下载榜首仍然“无人能出其 ...

  2. oracle 将当前系统时间戳插入timestamp字段

    oracle 将当前系统时间戳插入timestamp字段 --insert records 精确到秒:insert into userlogin_his(usrname,logintime) valu ...

  3. ansible一些基本操作

    一.介绍 特性 (1).no agents:不需要在被管控主机上安装任何客户端: (2).no server:无服务器端,使用时直接运行命令即可: (3).modules in any languag ...

  4. 基于JWT的token身份认证方案

    一.使用JSON Web Token的好处? 1.性能问题. JWT方式将用户状态分散到了客户端中,相比于session,可以明显减轻服务端的内存压力. Session方式存储用户id的最大弊病在于S ...

  5. LINUX SHELL 笔记 02: 变量初识

    https://www.shellscript.sh/variables1.html 变量是一个可操作(读.写)的内存块的名字. 尝试-1 创建一个变量: root@iZwz:~/labs# sh m ...

  6. 【Python】高阶函数

    filter def is_palindrome(n): L = str(n) i = 0 j = len(L) - 1 while i != j: if L[i] != L[j]: return F ...

  7. docker使用Mesos

    https://github.com/PyreneGitHub/mesos_use/tree/master

  8. 20145109 实验四 Andoid开发基础

    安装Android 打开 默认程序中有helloworld 按下下图红框中的键: 遇到问题: 方法:修改build.gradle

  9. Oracle查询一个表的数据插入到另一个表

    1. 新增一个表,通过另一个表的结构和数据 create table XTHAME.tab1 as select * from DSKNOW.COMBDVERSION 2. 如果表存在: insert ...

  10. C#开发自己的Web服务器

    介绍 我们将学习如何写一个简单的web服务器,用于响应知名的HTTP请求(GET和POST),用C#发送响应.然后,我们从网络访问这台服务器,这次我们会说“Hello world!” 背景 HTTP协 ...