原题链接: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. 洛谷P1465

    P1465 序言页码 Preface Numbering 74通过 111提交 题目提供者该用户不存在 标签USACO 难度普及/提高- 提交  讨论  题解 最新讨论 暂时没有讨论 题目描述 一类书 ...

  2. Visual Studio 2010 C++ 工程文件解读

    在 VS2010 中,C++ 的工程文件已经和 2005 / 2008 有了很大的不同,而是完全采用 MSBUILD 的属性方式进行表达,并且可以让用户通过一次性的配置而对所有的属性进行自定义: 根据 ...

  3. 使用FFmpeg解码H264-2016.01.14

    使用jni方式调用FFmepg项目中接口,对H264裸码进行解码. 该Demo主要实现从文件中读取H264编码的视频流,然后使用FFmpeg解码,将解码后的码流保存到文件. 工程目录结构如图所示: A ...

  4. rel=nofollow

    nofollow是什么意思? nofollow是html标签的一个属性值,Google推荐使用nofollow,告诉机器(爬虫)无需追踪目标页,是指禁止蜘蛛爬行和传递权重,但是如果你是通过sitema ...

  5. ASP.NET的SEO:使用.ashx文件——排除重复内容

    本系列目录 不同的链接指向的页面如果具有大量相同的内容,这种现象就会被称为"重复内容",如果一个网站的重复内容很多,搜索引擎就会认为这个网站的价值不高.所以我们应尽量避免各种重复内 ...

  6. 使用NPOI操纵Excle,并输入到客户端

    NPOI下载:http://files.cnblogs.com/files/gosky/NPOI_2.2.0.0.zip 导入以下5个引用: ICSharpCode.SharpZipLib.dll N ...

  7. SQLServer附加数据库5120错误

    装有MSSQL的电脑 需要附加的数据库文件(*.mdf)及其日志文件(*.ldf) 1. 打开SQL Server Management Studio,并连接上数据库.右键"数据库" ...

  8. 调用WCF Data Service的几点Tips

    使用Linq实现sql in statement的时候,用EF的时候可以通过Contains.Exists的方法实现.但是在使用WCF Data Service的context的时候,会报不支持该方法 ...

  9. Knockout.Js官网学习(数组observable)

    前言 如果你要探测和响应一个对象的变化,你应该用observables. 如果你需要探测和响应一个集合对象的变化,你应该用observableArray . 在很多场景下,它都非常有用,比如你要在UI ...

  10. 配置Nginx服务

    一,安装之前准备1.nginx依赖: gcc openssl-devel pcre-devel zlib-devel    安装依赖:yum install gcc openssl-devel pcr ...