UVA11922 Permutation Transformer
思路
直接使用FHQ Treap维护即可
代码
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
struct Node{
int lson,rson,val,ran,sz,inv;
}FHQ[100100];
int Nodecnt=0,a[100100],n,m,root;
void pushup(int o){
FHQ[o].sz=FHQ[FHQ[o].lson].sz+FHQ[FHQ[o].rson].sz+1;
}
void pushdown(int o){
if(FHQ[o].inv){
FHQ[FHQ[o].lson].inv^=1;
FHQ[FHQ[o].rson].inv^=1;
swap(FHQ[o].lson,FHQ[o].rson);
FHQ[o].inv=0;
}
}
int build(int l,int r){
if(l>r)
return 0;
int o=++Nodecnt;
FHQ[o].inv=FHQ[o].lson=FHQ[o].rson=0;
int mid=(l+r)>>1;
FHQ[o].ran=rand();
FHQ[o].sz=1;
FHQ[o].val=a[mid];
FHQ[o].lson=build(l,mid-1);
FHQ[o].rson=build(mid+1,r);
pushup(o);
return o;
}
void split(int now,int val,int &x,int &y){
if(!now){
x=y=0;
return;
}
pushdown(now);
if(FHQ[FHQ[now].lson].sz>=val){
y=now;
split(FHQ[now].lson,val,x,FHQ[now].lson);
pushup(now);
}
else{
x=now;
split(FHQ[now].rson,val-FHQ[FHQ[now].lson].sz-1,FHQ[now].rson,y);
pushup(now);
}
}
int merge(int x,int y){
if(x*y==0)
return x+y;
pushdown(x);
pushdown(y);
if(FHQ[x].ran<FHQ[y].ran){
FHQ[x].rson=merge(FHQ[x].rson,y);
pushup(x);
return x;
}
else{
FHQ[y].lson=merge(x,FHQ[y].lson);
pushup(y);
return y;
}
}
void output(int o){
if(!o)
return;
pushdown(o);
output(FHQ[o].lson);
printf("%d\n",FHQ[o].val);
output(FHQ[o].rson);
}
int main(){
// freopen("test.in","r",stdin);
// freopen("test.out","w",stdout);
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++)
a[i]=i;
root=build(1,n);
for(int i=1;i<=m;i++){
int l,r;
scanf("%d %d",&l,&r);
int lroot,midroot,tmproot,rroot;
split(root,l-1,lroot,tmproot);
split(tmproot,r-l+1,midroot,rroot);
FHQ[midroot].inv^=1;
pushdown(midroot);
root=merge(merge(lroot,rroot),midroot);
}
output(root);
return 0;
}
UVA11922 Permutation Transformer的更多相关文章
- UVA_11922 Permutation Transformer 【splay树】
一.题目 UVA11922 二.分析 为什么会有伸展树? 伸展树与AVL的区别除了保持平衡的方式不同外,最重要的是在每次查找点时,让该点旋转到根结点,这里可以结合计算机里的局部性原理思考. 伸展树有什 ...
- UVA 11922 Permutation Transformer(平衡二叉树)
Description Write a program to transform the permutation 1, 2, 3,..., n according to m instructions. ...
- 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个元素,反转后添加到排列尾部 分析:用一个可分裂合并的序列来表示整个序列,截取一段可以用两次分裂一次合并实现,粘 ...
- bzoj3223 Tyvj 1729 文艺平衡树(Splay Tree+区间翻转)
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2202 Solved: 1226[Submit][Sta ...
随机推荐
- 用node在本机搭建一个极其简单的服务器
首先安装node, 建一个文件夹server, 在里面创建一个server.js,内容如下: var http = require("http"); http.createServ ...
- 使用pushstate,指定回退地址
history.pushState(null,"testname", window.location.href); window.addEventListener('popstat ...
- Apache2 httpd.conf配置文件中文版详解
Apache2 httpd.conf配置文件中文版详解## 基于 NCSA 服务的配置文件.##这是Apache服务器主要配置文件.#它包含服务器的影响服务器运行的配置指令.#参见以取得关于这些指令的 ...
- js优化 前端小白适用
注意啦,前端初学者适合看的js优化,当你看我的优化认为太low,那么恭喜,你已经脱离初学者了. 首先这边我觉得分享的还是以js为主,前端性能优化,我认为最重要的还是js,因为js是一门解释型的语言,相 ...
- 在vue中使用echarts图表
在vue中使用echarts图表 转载请注明出处:https://www.cnblogs.com/wenjunwei/p/9815290.html 安装vue依赖 使用npm npm instal ...
- vim : 依赖: vim-common (= 2:7.3.429-2ubuntu2) 但是 2:7.3.429-2ubuntu2.1 正要被安装
sudo apt-get purge vim-common sudo apt-get update sudo apt-get upgrade sudo apt-get install vim Just ...
- python3.*的一些笔记
因为使用python越来越频繁,有一些细节的东西经常用后一段时间没去用就会忘记,做些简单的笔记吧. 1.break和continue和pass a = 0 while 1: a+=1 if(a%3== ...
- openwrt路由器进入安全模式
openwrt路由器型号:WNDR3800 一.实验背景 在pc机上通过xshell软件登录openwrt路由器,pc机通过网线与openwrt路由器的LAN接口相连.openwrt路由器自带两块无线 ...
- jdk5升8问题记录-Spring2升4
Spring2.x升4.x Hibernate3.x升5.0 jdbcOperations.queryForInt 替换为 queryForObject(sql, parameters, Intege ...
- Why choose Nexiq USB-link 125032 Diesel Truck Diagnose
Nexiq 125032 usb link is Diesel Truck diagnostic Interface. Nexiq truck scanner can compatible with ...