uva 11922 Permutation Transforme/splay tree
原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902
伸展树的区间翻转剪切。。。
如下:
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<algorithm>
const int Max_N = ;
struct Node{
int v, s, rev;
Node *pre, *ch[];
inline void set(int _v = -, int _s = , Node *p = NULL){
v = _v, s = _s, rev = ;
pre = ch[] = ch[] = p;
}
inline void push_up(){
s = ch[]->s + ch[]->s + ;
}
inline void update(){
rev ^= ;
std::swap(ch[], ch[]);
}
inline void push_down(){
if (rev != ){
rev ^= ;
ch[]->update();
ch[]->update();
}
}
};
struct SplayTree{
int N = ;
Node *tail, *root, *null, stack[Max_N];
void init(int n){
N = n, tail = &stack[];
null = tail++;
null->set();
root = newNode(-);
root->ch[] = newNode(-);
root->ch[]->pre = root;
Node *x = built(, n);
root->ch[]->ch[] = x;
x->pre = root->ch[];
root->ch[]->push_up();
root->push_up();
}
inline Node *newNode(int v){
Node *p = tail++;
p->set(v, , null);
return p;
}
Node *built(int l, int r){
if (l > r) return null;
int mid = (l + r) >> ;
Node *p = newNode(mid);
p->ch[] = built(l, mid - );
if (p->ch[] != null) p->ch[]->pre = p;
p->ch[] = built(mid + , r);
if (p->ch[] != null) p->ch[]->pre = p;
p->push_up();
return p;
}
inline void rotate(Node *x, int c){
Node *y = x->pre;
y->push_down(), x->push_down();
y->ch[!c] = x->ch[c];
x->pre = y->pre;
if (x->ch[c] != null) x->ch[c]->pre = y;
if (y->pre != null) y->pre->ch[y->pre->ch[] != y] = x;
x->ch[c] = y;
y->pre = x;
y->push_up();
if (y == root) root = x;
}
inline void splay(Node *x, Node *f){
if (x == root) return;
for (; x->pre != f; x->push_down()){
if (x->pre->pre == f){
rotate(x, x->pre->ch[] == x);
} else {
Node *y = x->pre, *z = y->pre;
if (z->ch[] == y){
if (y->ch[] == x)
rotate(y, ), rotate(x, );
else rotate(x, ), rotate(x, );
} else {
if (y->ch[] == x)
rotate(y, ), rotate(x, );
else rotate(x, ), rotate(x, );
}
}
}
x->push_up();
}
inline Node *select(Node *x, int k){
for (int t = ; x != null;){
x->push_down();
t = x->ch[]->s;
if (t == k) break;
else if (k < t) x = x->ch[];
else k -= t + , x = x->ch[];
}
return x;
}
inline Node *get_range(int l, int r){
splay(select(root, l - ), null);
splay(select(root, r + ), root);
return root->ch[]->ch[];
}
inline Node *reverse(int l, int r){
Node *x = get_range(l, r);
x->update();
return x;
}
inline void cut_link(int l, int r){
Node *x = reverse(l, r);
root->ch[]->ch[] = null;
root->ch[]->push_up();
root->push_up();
int k = N - r + l - ;
get_range(, k);
splay(select(root, k), null);
splay(select(root, k + ), root);
root->ch[]->ch[] = x;
x->pre = root->ch[];
root->ch[]->push_up();
root->push_up();
}
inline void travle(Node *x){
if (x != null){
x->push_down();
travle(x->ch[]);
printf("%d\n", x->v);
travle(x->ch[]);
}
}
inline void travle(){
get_range(, N);
Node *x = root->ch[]->ch[];
travle(x);
}
}Splay;
int main(){
#ifdef LOCAL
freopen("in.txt", "r", stdin);
freopen("out.txt", "w+", stdout);
#endif
int a, b, n, m;
while (~scanf("%d %d", &n, &m)){
Splay.init(n);
while (m--){
scanf("%d %d", &a, &b);
Splay.cut_link(a, b);
}
Splay.travle();
}
return ;
}
uva 11922 Permutation Transforme/splay tree的更多相关文章
- UVA - 11922 Permutation Transformer (splay)
题目链接 题意:你的任务是根据m条指令改变排列{!,2,3,...,n}.每条指令(a,b)表示取出第a~b个元素,翻转后添加到排列的尾部.输出最终序列. 解法:splay对区间分裂合并翻转,模板题. ...
- UVA 11922 Permutation Transformer —— splay伸展树
题意:根据m条指令改变排列1 2 3 4 … n ,每条指令(a, b)表示取出第a~b个元素,反转后添加到排列尾部 分析:用一个可分裂合并的序列来表示整个序列,截取一段可以用两次分裂一次合并实现,粘 ...
- UVA 11922 Permutation Transformer(Splay Tree)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 [思路] 伸展树+打标记. 用伸展树维护这个序列,使得能 ...
- UVA 11922 Permutation Transformer (Splay树)
题意: 给一个序列,是从1~n共n个的自然数,接下来又m个区间,对于每个区间[a,b],从第a个到第b个从序列中分离出来,翻转后接到尾部.输出最后的序列. 思路: 这次添加了Split和Merge两个 ...
- UVA 11922 Permutation Transformer(平衡二叉树)
Description Write a program to transform the permutation 1, 2, 3,..., n according to m instructions. ...
- UVa 11922 - Permutation Transformer 伸展树
第一棵伸展树,各种调试模板……TVT 对于 1 n 这种查询我处理的不太好,之前序列前后没有添加冗余节点,一直Runtime Error. 后来加上冗余节点之后又出了别的状况,因为多了 0 和 n+1 ...
- uva 11922 - Permutation Transformer
splay的题: 学习白书上和网上的代码敲的: #include <cstdio> #include <cstring> #include <cstdlib> #i ...
- UVA 11922 伸展树Splay 第一题
上次ZOJ月赛碰到一个题目要求对序列中的某个区间求gcd,并且还要随时对某位数字进行修改 插入 删除,当时马上联想到线段树,但是线段树不支持增删,明显还是不可以的,然后就敲了个链表想暴力一下,结果TL ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
随机推荐
- springMVC导入excel案例poi
直接上代码: 第一步,controller 引入 private static final String CHECK_FILE = "checkExceFile"; /** * 对 ...
- Facebook等使用苹果源生分享
1.Facebook官方的SDK分享 2.ShareSDK,第三方集成的分享方式 3.网页分享方式分享 4.IOS6之后,苹果自己集成了对于F ...
- MySql 日期函数
在 MySql 中经常会用到日期,关于常用的日期函数,做了以下的总结: 1 . now() 作用; 获取当前的日期 除此之外,获取当前日期的函数还有: current_timestamp(); cur ...
- php 调用.net的webservice 需要注意的
首先 SoapClient类这个类用来使用Web services.SoapClient类可以作为给定Web services的客户端.它有两种操作形式:* WSDL 模式* Non-WSDL 模式在 ...
- Vmware ESX5i 环境下部署Windows Storage Server 2008 R2
ESX5i 环境下部署Windows Storage Server 2008 R2 Windows Storage Server 2008 这款产品微软早已发布,WSS2008是基于Win ...
- webstorm & phpstorm破解
webstorm: http://idea.qinxi1992.cn/ http://idea.goxz.gq http://v2mc.net:1017 http://idea.imsxm.com h ...
- UML的9种图例解析
摘自: http://blog.csdn.net/fatherican/article/details/44966891 UML图中类之间的关系:依赖,泛化,关联,聚合,组合,实现 类与类图 1) 类 ...
- how to extract and decrypt WeChat EnMicromsg.db on Android phone
One of my friend came to me with an Android phone. She saild somehting wrong with the hardware of he ...
- Android IOS WebRTC 音视频开发总结(九)-- webrtc入门001
下面这篇介绍webrtc的文章不错,我花了大半天翻译了一下. 翻译的时候不是逐字逐句的,而是按照自己的理解翻译的,同时为了便于理解,也加入一些自己组织的语言. 本文主要介绍webrtc的信令,stun ...
- CICS的database中R D中参数的含义
见链接 http://blog.163.com/ajj_star/blog/static/1626772542010328113513429/ Region Definitions (RD) 定义了所 ...