原题链接: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. 【PL/SQL练习】DML语句的处理(可以处理多行数据)

    1.Insert (在表中插入一行数据,并查看) SQL> desc t1; Name Type Nullable Default Comments ---- ------------ ---- ...

  2. 解决visual studio已安装的问题

    使用Windows Install Clean Up(用管理员身份打开),找到相应的软件

  3. WPF学习系列之七 (样式与行为)

    样式(Styles)是组织和重用格式化选项的重要工具.不是使用重复的标记填充XAML,以设置诸如边距.颜色及字体等细节,而可以创建一系列封装所有这些细节的样式.然后可以在需要之处通过一个属性应用样式. ...

  4. Ossim主要功能实战

    Ossim主要功能实战 OSSIM通过将开源产品进行集成,从而提供一种能够实现安全监控功能的基础平台将Nagiso,Ntop,Snort,Nmap等开源工具集成在一起提供综合的安全保护功能,而不必在各 ...

  5. MFC六大核心机制之一:MFC程序的初始化

    很多做软件开发的人都有一种对事情刨根问底的精神,例如我们一直在用的MFC,很方便,不用学太多原理性的知识就可以做出各种窗口程序,但喜欢钻研的朋友肯定想知道,到底微软帮我们做了些什么,让我们在它的框架下 ...

  6. SQL2005中使用identity_insert向自动增量字段中写入内

    摘自: http://www.aspbc.com/tech/showtech.asp?id=1117 SQL2005以前的数据库是不允许向自动增量字段中写入内容的,ACCESS也不行,但在SQL200 ...

  7. 浅谈HTTPS安全性

    各位可曾有过使用智能手机App在网络商店购物的经验,想必是有的,那你/妳会不会担心不够安全呢?有人会说放心吧,购物网站有使用SSL/TLS加密传输,我们就来聊聊HTTPS好了. 客户端与服务器端的交握 ...

  8. Volume serial number could associate file existence on certain volume

    When it comes to lnk file analysis, we should put more emphasis on the volume serial number. It coul ...

  9. Excel 统计IP

    参考资料: 1:http://zhidao.baidu.com/question/127624244.html 其中的公式改成<1就可以了. 2:http://support.office.mi ...

  10. Oracle小数点格式化

    1. select to_char(123456789.12345,'fm999999990.99') from dual; 如果fm后位数不足则会显示 ## select to_char(12345 ...