Description

Write a program to transform the permutation 1, 2, 3,..., n according to m instructions. Each instruction (ab) 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 ( 1nm100, 000). Each of the next m lines contains an instruction consisting of two integers a and b ( 1abn).

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(平衡二叉树)的更多相关文章

  1. UVa 11922 - Permutation Transformer 伸展树

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

  2. uva 11922 - Permutation Transformer

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

  3. UVA 11922 Permutation Transformer(Splay Tree)

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

  4. UVA - 11922 Permutation Transformer (splay)

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

  5. UVA 11922 Permutation Transformer (Splay树)

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

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

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

  7. uva 11922 Permutation Transforme/splay tree

    原题链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18902 伸展树的区间翻转剪切... 如下: #include< ...

  8. UVA 11922 Splay tree

    UVA 11922 题意: 有n个数1~n 操作a,b表示取出第a~b个数,翻转后添加到数列的尾部 输入n,m 输入m条指令a,b 输出最终的序列 代码: #include<iostream&g ...

  9. UVA 12003 Array Transformer

    Array Transformer Time Limit: 5000ms Memory Limit: 131072KB This problem will be judged on UVA. Orig ...

随机推荐

  1. Linux中文件函数(一)

    一.stat.fstat.fstatat.lstat函数 函数的原型为: #include <sys/stat.h> int stat(const char *restrict pathn ...

  2. sort的用法

    早一段时间一直没有理解sort的用法,在早几天终于是研究的明白的,所以就来分享一下,如果你也被这个方法困扰,没懂原理,可以看一下这遍文章,希望有所帮助. 第一种,最简单的排序,纯数字排序: var a ...

  3. HTML 5 audio标签

    audio标签的介绍 定义: <audio> 标签定义声音,比如音乐或其他音频流. <audio></audio>是HTML5中的新标签 能够在浏览器中播放音频, ...

  4. SRM32(8)——ADC和DAC

    1.ADC简介 STM32 拥有 1~3 个 ADC(STM32F101/102 系列只有 1 个 ADC)STM32F103至少拥有2个ADC,STM32F103ZE包含3个ADC,这些 ADC 可 ...

  5. Android 串口蓝牙通信开发Java版本

    Android串口BLE蓝牙通信Java版 0. 导语 Qt on Android 蓝牙通信开发 我们都知道,在物联网中,BLE蓝牙是通信设备的关键设备.在传统的物联网应用中,无线WIFI.蓝牙和Zi ...

  6. SQL盲注

    一.首先输入1和-1 查看输入正确和不正确两种情况 二.三种注入POC LOW等级 ... where user_id =$id 输入      真  and  假 = 假 (1)...where u ...

  7. 《PHP发送邮件PHPMailer》系列分享专栏

    <PHP发送邮件PHPMailer>已整理成PDF文档,点击可直接下载至本地查阅https://www.webfalse.com/read/201726.html 文章 PHPMailer ...

  8. java语言描述 用递归打印字符串

    public class Test{ static private int n; public static void main(String[] args) { Test.n=76234; if(n ...

  9. [Jmeter并发报错解决方案]org.apache.http.NoHttpResponseException: 10.0.4.147:8000 failed to respond

    背景:公司模型框架是Nginx+uwsgi+Django+nginx,一开始使用Jmeter进行高并发请求测试,发现成功率只有50%,换用postman,成功率100%,代码进行高并发一样不会报错. ...

  10. PS 拉伸大长腿

    1.打开一个图片工具栏--图像--画布大小 2.选择矩形选框工具--框住要拉升退的位置--然后在按Ctrl+T,进行拉伸即可