3223: Tyvj 1729 文艺平衡树 - BZOJ
Description
您需要写一种数据结构(可参考题目标题),来维护一个有序数列,其中需要提供以下操作:翻转一个区间,例如原有序序列是5 4 3 2 1,翻转区间是[2,4]的话,结果是5 2 3 4 1
Input
第一行为n,m n表示初始序列有n个数,这个序列依次是(1,2……n-1,n) m表示翻转操作次数
接下来m行每行两个数[l,r] 数据保证 1<=l<=r<=n
Output
输出一行n个数字,表示原始序列经过m次变换后的结果
Sample Input
5 3
1 3
1 3
1 4
Sample Output
4 3 2 1 5
HINT
N,M<=100000
好久没写,水一发splay。。。。。。
const
maxn=;
type
node=record
son:array[..]of longint;
a,fa,size:longint;
sw:boolean;
end;
var
f:array[..maxn]of node;
n,m,root:longint; function build(l,r,ff:longint):longint;
var
mid:longint;
begin
mid:=(l+r)>>;
f[mid].fa:=ff;
f[mid].size:=r-l+;
if mid>l then f[mid].son[]:=build(l,mid-,mid);
if (mid>) and (mid<n+) then f[mid].a:=mid-;
if mid<r then f[mid].son[]:=build(mid+,r,mid);
exit(mid);
end; procedure swap(var x,y:longint);
var
t:longint;
begin
t:=x;x:=y;y:=t;
end; procedure new(x:longint);
begin
with f[x] do
begin
size:=f[son[]].size+f[son[]].size+;
if sw then
begin
swap(son[],son[]);
f[son[]].sw:=not f[son[]].sw;
f[son[]].sw:=not f[son[]].sw;
sw:=not sw;
end;
end;
end; function wh(x:longint):longint;
begin
if f[f[x].fa].son[]=x then exit();
exit();
end; procedure rotate(x,w:longint);
var
y:longint;
begin
y:=f[x].fa;
f[y].son[w]:=f[x].son[w xor ];
if f[x].son[w xor ]<> then f[f[x].son[w xor ]].fa:=y;
f[x].son[w xor ]:=y;
if root=y then root:=x
else f[f[y].fa].son[wh(y)]:=x;
f[x].fa:=f[y].fa;
f[y].fa:=x;
new(y);
end; procedure splay(x,z:longint);
var
y:longint;
begin
while f[x].fa<>z do
begin
y:=f[x].fa;
if f[y].fa<>z then
begin
if wh(x)=wh(y) then rotate(y,wh(y))
else rotate(x,wh(x));
end;
rotate(x,wh(x));
end;
new(x);
end; function find(k:longint):longint;
begin
find:=root;
while true do
begin
new(find);
if k=f[f[find].son[]].size+ then exit(find);
if k<=f[f[find].son[]].size then find:=f[find].son[]
else
begin
dec(k,f[f[find].son[]].size+);
find:=f[find].son[];
end;
end;
end; var
aa:array[..maxn]of longint;
tot:longint; procedure dfs(x:longint);
begin
new(x);
with f[x] do
begin
if son[]<> then dfs(son[]);
inc(tot);
aa[tot]:=a;
if son[]<> then dfs(son[]);
end;
end; procedure main;
var
i,l,r:longint;
begin
read(n,m);
root:=build(,n+,);
for i:= to m do
begin
read(l,r);
splay(find(l),);
splay(find(r+),root);
f[f[f[root].son[]].son[]].sw:=not f[f[f[root].son[]].son[]].sw;
end;
splay(find(),);
splay(find(n+),root);
dfs(f[f[root].son[]].son[]);
for i:= to n- do
write(aa[i],' ');
write(aa[n]);
end; begin
main;
end.
3223: Tyvj 1729 文艺平衡树 - BZOJ的更多相关文章
- BZOJ 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 3628 Solved: 2052[Submit][Sta ...
- BZOJ 3223: Tyvj 1729 文艺平衡树(splay)
速度居然进前十了...第八... splay, 区间翻转,用一个类似线段树的lazy标记表示是否翻转 ------------------------------------------------- ...
- bzoj 3223: Tyvj 1729 文艺平衡树 (splay)
链接: https://www.lydsy.com/JudgeOnline/problem.php?id=3223 题面: 3223: Tyvj 1729 文艺平衡树 Time Limit: 10 S ...
- BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 6881 Solved: 4213[Submit][Sta ...
- 3223: Tyvj 1729 文艺平衡树
3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 1347 Solved: 724[Submit][Stat ...
- fhq_treap || BZOJ 3223: Tyvj 1729 文艺平衡树 || Luogu P3391 【模板】文艺平衡树(Splay)
题面: [模板]文艺平衡树(Splay) 题解:无 代码: #include<cstdio> #include<cstring> #include<iostream> ...
- [BZOJ 3223 & Tyvj 1729]文艺平衡树 & [CodeVS 3243]区间翻转
题目不说了,就是区间翻转 传送门:BZOJ 3223 和 CodeVS 3243 第一道题中是1~n的区间翻转,而第二道题对于每个1~n还有一个附加值 实际上两道题的思路是一样的,第二题把值对应到位置 ...
- 【BZOJ】3223: Tyvj 1729 文艺平衡树(splay)
http://www.lydsy.com/JudgeOnline/problem.php?id=3223 默默的.. #include <cstdio> #include <cstr ...
- bzoj 3223/tyvj 1729 文艺平衡树 splay tree
原题链接:http://www.tyvj.cn/p/1729 这道题以前用c语言写的splay tree水过了.. 现在接触了c++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...
随机推荐
- Swiper之初识
何为Swiper?Swiper是一款免费以及轻量级的移动设备触控滑块的框架,使用硬件加速过渡(如果该设备支持的话).主要使用与移动端的网站.网页应用程序(web apps),以及原生的应用程序(nat ...
- 第九篇、自定义底部UITabBar
国际惯例先上图: 代码实现(在UITabBarViewController设置): - (void)setUpTabBar { LHLTabBar *tabBar = [[LHLTabBar allo ...
- Objective-C 【@property 的参数问题】
------------------------------------------- @property参数 总的来说,这是一种编译器的特性(在生成@property的时候为@property添加相 ...
- php折线图 布局图
例子1: 1 <?php require_once("../conf.php"); ?> <!DOCTYPE HTML> <html> < ...
- php面向对象的多态
多态是指使用类的上下文来重新定义或改变类的性质或行为,或者说接口的多种不同的实现方式即为多态.把不同的子类对象都当成父类来看,可以屏蔽不同子类对象之间的差异,写出通用的代码,做出通用的编程,以适应需要 ...
- android 网络_网络源码查看器
xml设计 <?xml version="1.0"?> -<LinearLayout tools:context=".MainActivity" ...
- Windows下安装GnuRadio最简单的方法(没有之一)
作者在Windows XP SP3 32位下亲测通过,理论上Win7也没问题. 1. 如果系统中安装有Python,请先把Python卸载. 2. 下载安装Python(x,y) 2.7.5.0, 下 ...
- Windows Phone 8.1开发:如何让ListView滚动到顶部,回到第一条?
Windows Phone 8.1开发中,ListView向下滑动了半天,用户如果突然想回头看看第一条数据怎么办? 如何让listView滚动到顶部,回到第一条? 很简单,一行代码.调用ListVie ...
- 转载:Linux内核探索之路——关于书
转自http://blog.chinaunix.net/uid-20608849-id-3029223.html 在学习Linux内核代码的过程中,定会参考很多书籍以及网路资源,但是并不是所有的书籍和 ...
- Reveal 配置与使用
http://www.th7.cn/Program/IOS/201608/939231.shtml http://www.jianshu.com/p/abac941c2e8e 这个比较好.http:/ ...