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++重写一遍... 只涉及区间翻转,由于没有删除操作故不带垃圾回收,具体 ...
随机推荐
- Mac下kernel_task占用大量CPU怎么办?
我们都知道要想让电脑运行的快速,那么就要尽量的保持内存和CPU的充足.不过一些MAC用户发现MAC系统中的Kernel_task会占用大量的CPU,导致电脑发热变卡.这个问题该怎么解决呢? 具体操作步 ...
- Python类和实例
面向对象最重要的概念就是类(Class)和实例(Instance),必须牢记类是抽象的模板,比如Student类,而实例是根据类创建出来的一个个具体的“对象”,每个对象都拥有相同的方法,但各自的数据可 ...
- SQLSERVER 启用跨库查询脚本
启用Ad Hoc Distributed Queries的方法,执行下面的查询语句就可以了: exec sp_configure 'show advanced options',1reconfigur ...
- (转)国内外三个不同领域巨头分享的Redis实战经验及使用场景
随着应用对高性能需求的增加,NoSQL逐渐在各大名企的系统架构中生根发芽.这里我们将为大家分享社交巨头新浪微博.传媒巨头Viacom及图片分享领域佼佼者Pinterest带来的Redis实践,首先我们 ...
- get the runing time of C++ console program.
// 获取程序运行时间.cpp : 定义控制台应用程序的入口点.// #include "stdafx.h"#include <time.h>#include < ...
- Android开发:碎片Fragment完全解析fragment_main.xml/activity_main.xml(转)
注明:这个转的,见谅未能标明原始出处 我们都知道,Android上的界面展示都是通过Activity实现的,Activity实在是太常用了,我相信大家都已经非常熟悉了,这里就不再赘述. 但是Activ ...
- C# 中有关 using 关键字
关于 C# 中的 using 关键字 我们往往只在代码的开头使用 using 关键字来引入名称空间,这是 using 的一个最常见的使用. 但是,using 关键字是否只有这么一处用武之地吗? 下面, ...
- 躲避球游戏ios源码
躲避球游戏源码,有限源码是一个基于cocos2d的躲避球游戏源码的,并且还引用了大家熟悉google广告的,进行推广,已经还有带game center等,游戏操作很简单,用手指按住物体,然后移动物体避 ...
- 4.Servlet_Form表单处理
1.建项目"3Servlet_Form",src下建包“com.amaker.servlet”,web-root下建Register.html <!DOCTYPE html& ...
- visual studio中创建单元测试
1 打开 工具--自定义 2 选择 上下文菜单--编辑器上下文菜单|代码窗口 3 在这里我们可以看到“创建单元测试”这个菜单了,将它移到运行测试菜单下面 4 关闭VS并重启 重启后再对着类名,点击右 ...