UVA 11922 Permutation Transformer(平衡二叉树)
Description
Write a program to transform the permutation 1, 2, 3,..., n according to m instructions. Each instruction (a, b) means to take out the subsequence from the a-th to the b-th element, reverse it, then append it to the end.
Input
There is only one case for this problem. The first line contains two integers n and m ( 1
n, m
100, 000). Each of the next m lines contains an instruction consisting of two integers a and b ( 1
a
b
n).
Output
Print n lines, one for each integer, the final permutation.
Explanation of the sample below
Instruction (2,5): Take out the subsequence {2,3,4,5}, reverse it to {5,4,3,2}, append it to the remaining permutation {1,6,7,8,9,10}
Instruction (4,8): The subsequence from the 4-th to the 8-th element of {1,6,7,8,9,10,5,4,3,2} is {8,9,10,5,4}. Take it out, reverse it, and you'll get the sample output.
题目大意:有一个1~n的序列,每次取出序列中第a~b的序列,翻转后排在序列的最后,求最后形成的序列
思路:用伸展树维护,先把1~a-1分裂出来,再把a~b分裂出来,翻转,然后接在最后。至于怎么翻转,标记一下就可以了。
PS:标记完翻转之后我脑子抽了一下每次都把标记传到最下面结果毫无疑问是TLE了……
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std; const int MAXN = ; int child[MAXN][], fa[MAXN], size[MAXN];
bool flip[MAXN]; inline void update(int &x) {
size[x] = size[child[x][]] + size[child[x][]] + ;
} inline void pushdown(int &x) {
if(flip[x]) {
flip[x] = ;
swap(child[x][], child[x][]);
flip[child[x][]] ^= ;
flip[child[x][]] ^= ;
}
} inline void rotate(int &x, int t) {
int y = child[x][t];
child[x][t] = child[y][t ^ ];
child[y][t ^ ] = x;
update(x); update(y);
x = y;
} //rotate the kth to root
void splay(int &x, int k) {
pushdown(x);
if(k == size[child[x][]] + ) return ;
int t = (k > size[child[x][]] ? : );
if(t == ) k -= (size[child[x][]] + );
int p = child[x][t];
pushdown(p);
int t2 = (k > size[child[p][]] ? : );
int k2 = (t2 == ? k : k - size[child[p][]] - );
if(k != size[child[p][]] + ) {
splay(child[p][t2], k2);
if(t == t2) rotate(x, t);
else rotate(child[x][t], t ^ );
}
rotate(x, t);
}
//left cannot be null
inline int merge(int left, int right) {
splay(left, size[left]);
child[left][] = right;
update(left);
return left;
} inline void split(int x, int k, int &left, int &right) {
splay(x, k);
left = x;
right = child[x][];
child[x][] = ;
update(left);
} void print(int x) {
if(x == ) return ;
pushdown(x);
print(child[x][]);
if(x != ) printf("%d\n", x - );
print(child[x][]);
} int cnt; int build(int l, int r) {
if(l > r) return ;
int mid = (l + r) >> ;
child[mid][] = build(l, mid - );
child[mid][] = build(mid + , r);
update(mid);
return mid;
} int root; int main() {
int n, m, a, b;
scanf("%d%d", &n, &m);
root = build(, n + );
while(m--) {
scanf("%d%d", &a, &b);
int left, mid, right, x;
split(root, a, left, x);
split(x, b - a + , mid, right);
flip[mid] ^= ;
root = merge(merge(left, right), mid);
//print(root); system("pause");
}
print(root);
}
UVA 11922 Permutation Transformer(平衡二叉树)的更多相关文章
- 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 Permutation Transformer(Splay Tree)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 [思路] 伸展树+打标记. 用伸展树维护这个序列,使得能 ...
- UVA - 11922 Permutation Transformer (splay)
题目链接 题意:你的任务是根据m条指令改变排列{!,2,3,...,n}.每条指令(a,b)表示取出第a~b个元素,翻转后添加到排列的尾部.输出最终序列. 解法:splay对区间分裂合并翻转,模板题. ...
- UVA 11922 Permutation Transformer (Splay树)
题意: 给一个序列,是从1~n共n个的自然数,接下来又m个区间,对于每个区间[a,b],从第a个到第b个从序列中分离出来,翻转后接到尾部.输出最后的序列. 思路: 这次添加了Split和Merge两个 ...
- UVA 11922 Permutation Transformer —— splay伸展树
题意:根据m条指令改变排列1 2 3 4 … n ,每条指令(a, b)表示取出第a~b个元素,反转后添加到排列尾部 分析:用一个可分裂合并的序列来表示整个序列,截取一段可以用两次分裂一次合并实现,粘 ...
- uva 11922 Permutation Transforme/splay tree
原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 伸展树的区间翻转剪切... 如下: #include< ...
- UVA 11922 Splay tree
UVA 11922 题意: 有n个数1~n 操作a,b表示取出第a~b个数,翻转后添加到数列的尾部 输入n,m 输入m条指令a,b 输出最终的序列 代码: #include<iostream&g ...
- UVA 12003 Array Transformer
Array Transformer Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Orig ...
随机推荐
- 搭建Extjs框架(一)
搭建Extjs框架 pc端 github https://github.com/Status400/Extjs-6.2.0-demo 欢迎start 准本工作: 官方下载Extjs ...
- SQLMAP使用详解
使用示例 python sqlmap.py -u "http://xx.com/member.php?id=XX" -p id --dbms "Mysql" ...
- vector,set常见方法
vector push_back() 压入元素 size()返回元素个数 swap()交换两个向量的位置 erase()任意位置删除元素 reverse(a.begin(),a.end())翻转 se ...
- jQuery中的easyui
一,easyui---datagrid绑定数据的简单测试: 1.数据库中的UserInfo表及数据测试: 2.DAL层: //分页,模糊查询(pageNum-1)*pageSize+1----从第几条 ...
- Jquery复选框的全选全不选及选择所有复选框实现全选的复选框
Jquery代码 $(function () { $(":checkbox.parentfunc").click(function () { //如何获取被点击的那个复选框 $(t ...
- jQuery的简单函数
1. jQuery函数的基本语法: $(document).ready(function(){ //代码块: }) 2.window.onload()和$(document).ready()的区分: ...
- BUG-jQuery提交表单submit方法-TypeError: e[h] is not a function
问题:button按钮设置id为submit后,表单jquery.submit()无法提交,报告异常TypeError: e[h] is not a function 源码: 解决:参考http:// ...
- MP3 编码解码 附完整c代码
近期一直不间断学习音频处理,一直也没想着要去碰音频编解码相关. 主要是觉得没什么实际的作用和意义. 不管视频编解码,图像编解码,音频编解码,都有很多组织基金在推动. 当然,在一些特定的情景下,需要用起 ...
- MQTT客户端
MQTT客户端 最近公司项目中使用到了一个MQTT的协议,用这个通讯协议将嵌入式端收集到的数据接入到物联网中,很是方便的解决了,嵌入式端存储空间小,也解决了用户需要自定义使用这些记录数据的需求.而且相 ...
- Shell环境变量与特殊变量详解
1)变量类型 1)变量可分为俩类:环境变量(全局变量),和普通变量(局部变量), 环境变量也称为全局变量,可以在创建他们的Shell及其派生出来的任意子进程Shell中使用,环境变量又可以分为自定义环 ...