原题链接: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的更多相关文章

  1. UVA - 11922 Permutation Transformer (splay)

    题目链接 题意:你的任务是根据m条指令改变排列{!,2,3,...,n}.每条指令(a,b)表示取出第a~b个元素,翻转后添加到排列的尾部.输出最终序列. 解法:splay对区间分裂合并翻转,模板题. ...

  2. UVA 11922 Permutation Transformer —— splay伸展树

    题意:根据m条指令改变排列1 2 3 4 … n ,每条指令(a, b)表示取出第a~b个元素,反转后添加到排列尾部 分析:用一个可分裂合并的序列来表示整个序列,截取一段可以用两次分裂一次合并实现,粘 ...

  3. UVA 11922 Permutation Transformer(Splay Tree)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 [思路] 伸展树+打标记. 用伸展树维护这个序列,使得能 ...

  4. UVA 11922 Permutation Transformer (Splay树)

    题意: 给一个序列,是从1~n共n个的自然数,接下来又m个区间,对于每个区间[a,b],从第a个到第b个从序列中分离出来,翻转后接到尾部.输出最后的序列. 思路: 这次添加了Split和Merge两个 ...

  5. UVA 11922 Permutation Transformer(平衡二叉树)

    Description Write a program to transform the permutation 1, 2, 3,..., n according to m instructions. ...

  6. UVa 11922 - Permutation Transformer 伸展树

    第一棵伸展树,各种调试模板……TVT 对于 1 n 这种查询我处理的不太好,之前序列前后没有添加冗余节点,一直Runtime Error. 后来加上冗余节点之后又出了别的状况,因为多了 0 和 n+1 ...

  7. uva 11922 - Permutation Transformer

    splay的题: 学习白书上和网上的代码敲的: #include <cstdio> #include <cstring> #include <cstdlib> #i ...

  8. UVA 11922 伸展树Splay 第一题

    上次ZOJ月赛碰到一个题目要求对序列中的某个区间求gcd,并且还要随时对某位数字进行修改 插入 删除,当时马上联想到线段树,但是线段树不支持增删,明显还是不可以的,然后就敲了个链表想暴力一下,结果TL ...

  9. bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 2202  Solved: 1226[Submit][Sta ...

随机推荐

  1. 创建母版页导致js出现“ 'document.getElementById(...)' 为空或不是对象”错误

    导读:一个控件在设计时的ID往往不同于生成页面后的ID,为了获得控件客户端ID,我们可以从生成的页面入手,冷静思考,把握主次,从底层框架入手 本文将为大家介绍一下 ASP.NET中在创建母版页时引来的 ...

  2. vb 随机获取6个1-33的数

    Private Sub random(ByVal num As Integer, ByVal min As Integer, ByVal max As Integer) Dim i As Intege ...

  3. Xcode去除某种类型的警告

      首先来看下当有警告时,怎么找到警告类型,在某条警告上,右键—>Reveal in Log     下面 [ ] 中间就是警告信息     去除警告信息的几种方式:   一.使用编译器提供的宏 ...

  4. html中button的type属性

         接触web开发不久,今天遇到了一个问题,点击button按钮,浏览器没有反应,尝试了自己可以想到的所有办法,还是无果.只得请教他人,才发现是button的type属性搞得怪,原来:     ...

  5. 洛谷P1613 跑路

    P1613 跑路 176通过 539提交 题目提供者该用户不存在 标签倍增动态规划 难度普及+/提高 提交该题 讨论 题解 记录 最新讨论 这个题的数据.. 题意问题 表意 题目描述 小A的工作不仅繁 ...

  6. centos atomic host第一次启动

    centos atomic host安装完成会,第一次启动时会调用cloud-init等服务.这是个什么东东? cloud-init用于在创建虚拟机时通过元数据服务对虚拟机基本配置,包括常见的主机名, ...

  7. http://www.cnblogs.com/vowei/archive/2012/08/24/2654287.html

    原创开源项目 - 扩展iQuery - 知平软件 - 博客园 return node.getProperty("mBottom").getValue();

  8. js动态生成JSON树

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. play framework 框架安装及myeclipse 导入项目

    下载 play framework 框架. 解压你你要解压的目录 E:\play-1.2.7 相对其他的WEB框架.play的配置是相当简单的.没有那么多配置文件的搞法.上手比较快,就是相关的资料比较 ...

  10. 添加favicon.ico网站文件

    <link rel="shortcut icon" type="image/x-icon" href="favicon.ico" me ...