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 ...
随机推荐
- 滑块控件CCControlSlider
#include "cocos-ext.h" //包含头文件 using namespace cocos2d::extension;//引用命名空间 /** * 创建CCContr ...
- 前端工程师在实现支付功能的时候能做些什么(V客学院技术分享)?
现在最流行的两种支付微信支付和支付宝支付,在日常开发的过程中肯定离不开支付功能的开发,有很多人第一次接触时会有些措手不及. 一.业务逻辑 (电商平台为例子) 支付大部分用在电商平台,各种打赏,游戏充值 ...
- lvds split两channel输出到一个屏显示
转:https://blog.csdn.net/changqing1990/article/details/81128552 其实之前写过LCD/LVDS的一些时序的基本概念<与LCD移植相关的 ...
- Python3.x:报错POST data should be bytes, an iterable of bytes
Python3.x:报错POST data should be bytes, an iterable of bytes 问题: python3.x:报错 POST data should be byt ...
- fastdfs安装与配置
FastDFS是一个开源的轻量级分布式文件系统,它对文件进行管理,功能包括:文件存储.文件同步.文件访问(文件上传.文件下载)等,解决了大容量存储和负载均衡的问题.特别适合以文件为载体的在线服务,如相 ...
- 20145307第七周JAVA学习报告
20145307<Java程序设计>第七周学习总结 教材学习内容总结 Lambda Lambda语法概述: Arrays的sort()方法可以用来排序,在使用sort()时,需要操作jav ...
- 完成了C++作业,本博客现在开始全面记录acm学习历程,真正的acm之路,现在开始
以下以目前遇到题目开始记录,按发布时间排序 ACM之递推递归 ACM之数学题 拓扑排序 ACM之最短路径做题笔记与记录 STL学习笔记不(定期更新) 八皇后问题解题报告
- Spring笔记(二)
1. SPRING aop入门 Aop 面向切面编程 在一个大型的系统中,会写很多的业务类--业务方法 同时,一个大型的系统中,还有很多公共的功能:比如事务管理.日志处理.缓存处理..... 1.1 ...
- 解读:Hadoop Archive
hdfs并不擅长存储小文件,因为每个文件最少一个block,每个block的元数据都会在NameNode中占用150byte内存.如果存储大量的小文件,它们会吃掉NameNode节点的大量内存.MR案 ...
- ETL增量处理总结
1 LOG表 1.1 思路 用log表记录业务库某表yw_tableA发生变化数据的主键.数据进入BI库目标表bi_tableA前,先根据log表记录的主键进行delete. 1.2 设计 1.2.1 ...