UVA 11922 Permutation Transformer —— splay伸展树
题意:根据m条指令改变排列1 2 3 4 … n ,每条指令(a, b)表示取出第a~b个元素,反转后添加到排列尾部
分析:用一个可分裂合并的序列来表示整个序列,截取一段可以用两次分裂一次合并实现,粘贴到末尾可以用一次合并实现。
翻转可以采用在每个结点上做标记的方法,flip = 1意味着将这棵子树翻转,可以类似线段树用一个pushdown()实现标记向下传递。
可以发现当前排列就是伸展树的中序遍历序列。中序遍历打印结果即可。
注意代码中设置了虚拟首结点0的技巧。
代码如下:
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <vector>
using namespace std; struct Node {
Node* ch[];
int r, v, s;
int flip;
Node(int vv): v(vv) {
r = rand();
s = ;
ch[] = ch[] = NULL;
flip = ;
}
bool cmp(const int &x) const {
if(x == v) return -;
return x < v ? : ;
}
void maintain() {
s = ;
if(ch[] != NULL) s += ch[]->s;
if(ch[] != NULL) s += ch[]->s;
}
void pushdown() {
if(flip) {
flip = ;
swap(ch[], ch[]);
if(ch[] != NULL) ch[]->flip = !ch[]->flip;
if(ch[] != NULL) ch[]->flip = !ch[]->flip;
}
}
};
void rotate(Node* &o, int d) {
Node* k = o->ch[d^]; o->ch[d^] = k->ch[d]; k->ch[d] = o;
o->maintain(); k->maintain(); o = k;
}
void insert(Node* &o, int x) {
if(o == NULL) o = new Node(x);
else {
int d = o->cmp(x);
insert(o->ch[d], x);
if(o->ch[d]->r > o->r) rotate(o, d^);
}
o->maintain();
} void splay(Node* &o, int k) {
o->pushdown();
int s = o->ch[] == NULL ? : o->ch[]->s;
int d = k <= s ? : (k == s+ ? - : );
if(d == ) k -= s+;
if(d != -) {
splay(o->ch[d], k);
rotate(o, d^);
}
} Node* merge(Node* left, Node* right) {
splay(left, left->s);
left->ch[] = right;
left->maintain();
return left;
} void split(Node* o, int k , Node* &left, Node* &right) {
splay(o, k);
left = o;
right = o->ch[];
o->ch[] = NULL;
left->maintain();
}
vector<int> seq;
void dfs(Node* o) {
if(o == NULL) return;
o->pushdown();
dfs(o->ch[]);
if(o->v) {
//printf("%d ", o->v);
seq.push_back(o->v);
}
dfs(o->ch[]);
}
int n, m;
int main() {
cin >> n >> m;
Node* root = new Node(); //虚拟首结点0,方便分裂操作
for(int i = ; i <= n; i++) insert(root, i);
while(m--) {
int a, b;
cin >> a >> b;
Node *left, *mid, *right, *o;
split(root, a, left, o);
split(o, b-a+, mid, right);
mid->flip ^= ;
root = merge(merge(left, right), mid);
}
dfs(root);
for(int i = 0; i < seq.size(); i++) cout<<seq[i]<<endl;
return ;
}
UVA 11922 Permutation Transformer —— splay伸展树的更多相关文章
- UVA - 11922 Permutation Transformer (splay)
题目链接 题意:你的任务是根据m条指令改变排列{!,2,3,...,n}.每条指令(a,b)表示取出第a~b个元素,翻转后添加到排列的尾部.输出最终序列. 解法:splay对区间分裂合并翻转,模板题. ...
- UVa 11922 - Permutation Transformer 伸展树
第一棵伸展树,各种调试模板……TVT 对于 1 n 这种查询我处理的不太好,之前序列前后没有添加冗余节点,一直Runtime Error. 后来加上冗余节点之后又出了别的状况,因为多了 0 和 n+1 ...
- UVA 11922 Permutation Transformer(Splay Tree)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 [思路] 伸展树+打标记. 用伸展树维护这个序列,使得能 ...
- UVA 11922 Permutation Transformer(平衡二叉树)
Description Write a program to transform the permutation 1, 2, 3,..., n according to m instructions. ...
- uva 11922 Permutation Transforme/splay tree
原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 伸展树的区间翻转剪切... 如下: #include< ...
- UVA 11922 Permutation Transformer (Splay树)
题意: 给一个序列,是从1~n共n个的自然数,接下来又m个区间,对于每个区间[a,b],从第a个到第b个从序列中分离出来,翻转后接到尾部.输出最后的序列. 思路: 这次添加了Split和Merge两个 ...
- uva 11922 - Permutation Transformer
splay的题: 学习白书上和网上的代码敲的: #include <cstdio> #include <cstring> #include <cstdlib> #i ...
- Splay伸展树学习笔记
Splay伸展树 有篇Splay入门必看文章 —— CSDN链接 经典引文 空间效率:O(n) 时间效率:O(log n)插入.查找.删除 创造者:Daniel Sleator 和 Robert Ta ...
- 【学时总结】◆学时·VI◆ SPLAY伸展树
◆学时·VI◆ SPLAY伸展树 平衡树之多,学之不尽也…… ◇算法概述 二叉排序树的一种,自动平衡,由 Tarjan 提出并实现.得名于特有的 Splay 操作. Splay操作:将节点u通过单旋. ...
随机推荐
- python输出最大公约数和最小公倍数
def myfun(): num1 = int(input('输入num1')) num2 = int(input('输入num2')) list1=[] for i in range(1, max( ...
- 学习JDK1.8集合源码之--HashSet
1. HashSet简介 HashSet是一个不可重复的无序集合,底层由HashMap实现存储,故HashSet是非线程安全的,由于HashSet使用HashMap的Key来存储元素,而HashMap ...
- 理解nodejs的module
module 在 Node.js 模块系统中,每个文件都视为独立的模块,node在运行某个模块儿时会生成一个module对象 Module { id: '.', exports: 2, parent: ...
- thinkphp5.1学习总结
1.修改应用根目录名称 (1)重新定义入口文件如下namespace think; // 定义应用目录define('APP_PATH', __DIR__ . '/../app/'); // 加载基础 ...
- 【To Read】
Maximal Rectangle Given a 2D binary matrix filled with 0's and 1's, find the largest rectangle conta ...
- Spring表达式语言:SpEl
概念: 是一个支持运行时查询和操作的对象图的强大的表达式语言. 语法类似于EL:SpEl使用#{ ...}作为定界符,所有在大括号中的 字符都将被认为是SpEl SpEl为bean的属性进行动态赋值提 ...
- 【转载】CPU相关总结
What is the difference between Processor, Core, Logical Processor ? Processor : It’s the physical co ...
- InteractiveHtmlBom 在手机上无法显示 BOM List 和装配图的问题
InteractiveHtmlBom 在手机上无法显示 BOM List 和装配图的问题 InteractiveHtmlBom 插件是一款用于 KiCad BOM 装配图生成插件. 最近新生成的 文件 ...
- sql —— like
用于在 WHERE 子句中搜索列中的指定模式. 原表: 一.% %表示任何字符出现任意次数. 1.以某个字符串开头的数据 2.包含某个字符串的数据 3.以某个字符串结尾的数据 二._ 只适用于匹配单个 ...
- Java成员方法,构造方法
//作者:qingfeng//2017/2/17//功能:理解类的成员方法和构造方法class CreatWays{ public static void main(String args[]){ ...