hdu3487 splay树
Play with Chain
Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5426 Accepted Submission(s): 2185
At first, the diamonds on the chain is a sequence: 1, 2, 3, …, n.
He will perform two types of operations:
CUT a b c: He will first cut down the chain from the ath diamond to the bth diamond. And then insert it after the cth diamond on the remaining chain.
For example, if n=8, the chain is: 1 2 3 4 5 6 7 8; We perform “CUT 3 5 4”, Then we first cut down 3 4 5, and the remaining chain would be: 1 2 6 7 8. Then we insert “3 4 5” into the chain before 5th diamond, the chain turns out to be: 1 2 6 7 3 4 5 8.
FLIP a b: We first cut down the chain from the ath diamond to the bth diamond. Then reverse the chain and put them back to the original position.
For example, if we perform “FLIP 2 6” on the chain: 1 2 6 7 3 4 5 8. The chain will turn out to be: 1 4 3 7 6 2 5 8
He wants to know what the chain looks like after perform m operations. Could you help him?
For each test case, the first line contains two numbers: n and m (1≤n, m≤3*100000), indicating the total number of diamonds on the chain and the number of operations respectively.
Then m lines follow, each line contains one operation. The command is like this:
CUT a b c // Means a CUT operation, 1 ≤ a ≤ b ≤ n, 0≤ c ≤ n-(b-a+1).
FLIP a b // Means a FLIP operation, 1 ≤ a < b ≤ n.
The input ends up with two negative numbers, which should not be processed as a case.
CUT 3 5 4
FLIP 2 6
-1 -1
/*
hdu3487 splay树
cut是将一串数字复制到另外一个位置,flip是将一串数字逆序
cut直接进行一次删除插入即可,flip则是给个转换标记然后更新
hhh-2016-02-20 06:50:15
*/ #include <functional>
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
#include <map>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
#define key_value ch[ch[root][1]][0]
const int maxn = 300010; int ch[maxn][2];
int rev[maxn],a[maxn];
int pre[maxn],key[maxn],siz[maxn];
int root,TOT,cnt,n; void push_up(int r)
{
int lson = ch[r][0],rson = ch[r][1];
siz[r] = siz[lson] + siz[rson] + 1;
} void update_rev(int r)
{
if(!r) return ;
swap(ch[r][0],ch[r][1]);
rev[r] ^= 1;
} void push_down(int r)
{
if(rev[r])
{
update_rev(ch[r][0]);
update_rev(ch[r][1]);
rev[r] = 0;
}
} void inOrder(int r)
{
if(!r)return;
push_down(r);
inOrder(ch[r][0]);
if(cnt >=1 && cnt <= n)
{
printf("%d",key[r]);
if(cnt < n)printf(" ");
else printf("\n");
}
cnt++;
inOrder(ch[r][1]);
} void debug()
{
cnt = 0;
inOrder(root);
} void NewNode(int &r,int far,int k)
{
r = ++TOT;
pre[r] = far;
ch[r][0] = ch[r][1] = 0;
key[r] = k;
siz[r] = 1;
rev[r] = 0;
} void rotat(int x,int kind)
{
int y = pre[x];
push_down(y);
push_down(x);
ch[y][!kind] = ch[x][kind];
pre[ch[x][kind]] = y;
if(pre[y])
ch[pre[y]][ch[pre[y]][1]==y] = x;
pre[x] = pre[y];
ch[x][kind] = y;
pre[y] = x;
push_up(y);
} void build(int &x,int l,int r,int far)
{
if(l > r) return ;
int mid = (l+r) >>1;
NewNode(x,far,mid);
build(ch[x][0],l,mid-1,x);
build(ch[x][1],mid+1,r,x);
push_up(x);
} void splay(int r,int goal)
{
push_down(r);
while(pre[r] != goal)
{
if(pre[pre[r]] == goal)
{
push_down(pre[r]);
push_down(r);
rotat(r,ch[pre[r]][0] == r);
}
else
{
push_down(pre[pre[r]]);
push_down(pre[r]);
push_down(r);
int y = pre[r];
int kind = ch[pre[y]][0] == y;
if(ch[y][kind] == r)
{
rotat(r,!kind);
rotat(r,kind);
}
else
{
rotat(y,kind);
rotat(r,kind);
}
}
}
push_up(r);
if(goal == 0)
root = r;
} int get_kth(int r,int k)
{
push_down(r);
int t = siz[ch[r][0]]+1;
if(t == k) return r;
if(t > k) return get_kth(ch[r][0],k);
else return get_kth(ch[r][1],k-t);
} int CUT(int l,int r,int b)
{
splay(get_kth(root,l),0);
splay(get_kth(root,r+2),root);
int tmp = key_value;
pre[key_value] = 0;
key_value = 0;
push_up(ch[root][1]);
push_up(root); splay(get_kth(root,b+1),0);
splay(get_kth(root,b+2),root);
pre[tmp] = ch[root][1];
key_value = tmp;
push_up(ch[root][1]);
push_up(root);
//debug();
} void Reverse(int l,int r)
{
splay(get_kth(root,l),0);
splay(get_kth(root,r+2),root);
update_rev(key_value);
push_up(ch[root][1]);
push_up(root);
//debug();
} void ini(int n)
{
TOT = root = 0;
ch[root][0] = ch[root][1] = pre[root] = siz[root] = 0;
rev[root] = 0;
NewNode(root,0,-1);
NewNode(ch[root][1],root,-1);
build(key_value,1,n,ch[root][1]);
//inOrder(root);
//printf("\n");
push_up(ch[root][1]);
push_up(root);
} int main()
{
int q;
while(scanf("%d%d",&n,&q) != EOF)
{ if(n == -1 && q == -1)
break;
ini(n);
for(int i =1; i <= q; i++)
{
char qry[10];
scanf("%s",qry);
int a,b,len;
if(qry[0] == 'C')
{
scanf("%d%d%d",&a,&len,&b);
CUT(a,len,b);
}
else
{
scanf("%d%d",&a,&b);
Reverse(a,b);
}
}
cnt = 0;
inOrder(root);
}
return 0;
}
hdu3487 splay树的更多相关文章
- Hdu-3487 Splay树,删除,添加,Lazy延迟标记操作
HDU_3487 题意:给出n和q,n代表1-n的序列,接下来q有两种操作,Cut a b c:表示把区间[a,b]截掉然后放在第c个数的后面,Flip a b 表示把区间[a,b]反转,经过一系列的 ...
- Splay树-Codevs 1296 营业额统计
Codevs 1296 营业额统计 题目描述 Description Tiger最近被公司升任为营业部经理,他上任后接受公司交给的第一项任务便是统计并分析公司成立以来的营业情况. Tiger拿出了公司 ...
- ZOJ3765 Lights Splay树
非常裸的一棵Splay树,需要询问的是区间gcd,但是区间上每个数分成了两种状态,做的时候分别存在val[2]的数组里就好.区间gcd的时候基本上不支持区间的操作了吧..不然你一个区间里加一个数gcd ...
- Splay树再学习
队友最近可能在学Splay,然后让我敲下HDU1754的题,其实是很裸的一个线段树,不过用下Splay也无妨,他说他双旋超时,单旋过了,所以我就敲来看下.但是之前写的那个Splay越发的觉得不能看,所 ...
- 暑假学习日记:Splay树
从昨天开始我就想学这个伸展树了,今天花了一个上午2个多小时加下午2个多小时,学习了一下伸展树(Splay树),学习的时候主要是看别人博客啦~发现下面这个博客挺不错的http://zakir.is-pr ...
- 1439. Battle with You-Know-Who(splay树)
1439 路漫漫其修远兮~ 手抄一枚splay树 长长的模版.. 关于spaly树的讲解 网上很多随手贴一篇 貌似这题可以用什么bst啦 堆啦 平衡树啦 等等 这些本质都是有共同点的 查找.删除特 ...
- 伸展树(Splay树)的简要操作
伸展树(splay树),是二叉排序树的一种.[两个月之前写过,今天突然想写个博客...] 伸展树和一般的二叉排序树不同的是,在每次执行完插入.查询.删除等操作后,都会自动平衡这棵树.(说是自动,也就是 ...
- [Splay伸展树]splay树入门级教程
首先声明,本教程的对象是完全没有接触过splay的OIer,大牛请右上角.. 首先引入一下splay的概念,他的中文名是伸展树,意思差不多就是可以随意翻转的二叉树 PS:百度百科中伸展树读作:BoGa ...
- hdu 3436 splay树+离散化*
Queue-jumpers Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) To ...
随机推荐
- 解决vue2.0路由 TypeError: Cannot read property 'matched' of undefined 的错误问题
刚开始使用vue-router2.0,虽然也用了vux,用起来却发现一个问题--具体如下: 正常情况下使用脚手架跑完之后,然后修改源项目,首先在main.js入口里把该import进去的vuex,vu ...
- javascript 中的类型
javascript 中的类型 js 是一门弱语言,各式各样的错误多种多样,特别是确定返回值有问题的时候,你会用什么来进行表示错误? 我一般有三个选择: null '' error {} 第一个选择 ...
- Angular组件——组件生命周期(二)
一.view钩子 view钩子有2个,ngAfterViewInit和ngAfterViewChecked钩子. 1.实现ngAfterViewInit和ngAfterViewChecked钩子时注意 ...
- Netty事件监听和处理(下)
上一篇 介绍了事件监听.责任链模型.socket接口和IO模型.线程模型等基本概念,以及Netty的整体结构,这篇就来说下Netty三大核心模块之一:事件监听和处理. 前面提到,Netty是一个NIO ...
- SLF4J - 一个允许你统一日志记录API的抽象层
一.什么是SLF4J 我们在做Java开发时,如果需要记录日志,有很多日志API可供选择,如: java.util.logging Apache log4j logback SLF4J又是个什么东东呢 ...
- 我做的python常用的小技巧
在python编码过程中,总会遇到各种各样的小问题,我想着记录下来,以备查用,总结过去,是为了更好的思考与进步. 一. 去除变量中(标题中)多余的字符 数据处理过程中,遇到这样的情况: y=['月份' ...
- Linux:nohub启动后台永久进程
nohup 命令运行由 Command参数和任何相关的 Arg参数指定的命令,忽略所有挂断(SIGHUP)信号.在注销后使用 nohup 命令运行后台中的程序.要运行后台中的 nohup 命令,添加 ...
- JProfiler简明使用教程
JProfile是一款性能瓶颈分析工具,监控粒度可以细化到某一个类包,堪称神器!我安装了一下9.11的版本,并简单说说使用方法. 1:创建一个监控任务 2:选择tomcat版本 3:监控远程服务器 4 ...
- HBase表创建、删除、清空
HBase shell窗口进入 执行命令hbase shell HBase表的创建 # 语法:create <table>, {NAME => <family>, VER ...
- LAMP+Wordpress搭建博客
作为一名技术控,看到别人都有专属于自己的博客,小夜也忍不住自己操刀做个自己的博客.有兴趣的童鞋接下来可要认真地练习比对哦.如果出现错误也不要着急上火,咱们可以交流学习.我们的口号是:遇水搭桥,遇山凿山 ...