3223: Tyvj 1729 文艺平衡树 - BZOJ
2024-10-12 03:56:45
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.
最新文章
- Entity Framework 基于方法的查询语法
- PostrgreSQL 表名大小些问题(public.";tablename";)
- 网页中显示pdf
- 连接英文字符集的ORACLE和调用存储过程问题及64位服务器连接ORACLE问题
- Android安卓知识点
- libGraphicsMagickWand.so: cannot open shared object file: No such file or directory stack traceback:
- 用HTML代码加载Unity内容
- 一个公网地址部署LVS/DR模式
- Android 之 Socket 通信
- (简单) HDU 1698 Just a Hook , 线段树+区间更新。
- Struts2技术内幕 读书笔记二 web开发的基本模式
- IDEA配置github并上传项目
- MapReduce-FileInputFormat
- 【转】c语言中的#号和##号的作用
- AWS DevOps – 配合Jenkins和CodeDeploy实现代码自动化部署
- (转)Java程序员的面试经历和题库
- luogu P1593 因子和
- Android /data/local/tmp目录的好处
- 可选的binlog解析组件
- 外网电脑配置8G运行内存,运行Android Studio,速度很轻松
热门文章
- Process Stats:了解你的APP如何使用内存(转)
- Part 59 to 60 Difference between Convert ToString and ToString,String and StringBuilder
- iOS 最新版 CocoaPods 的安装使用
- Apple Watch开发之界面之间的正向传值
- mysql插入表中的中文显示为乱码或问号的解决方法
- python:笔记for循环中的else
- lua中pairs和ipairs的区别
- sort()的多种用法
- 《锋利的jQuery》心得笔记--Two Sections
- 《LDAP服务器的配置与客户端的测试》RHEL6——第一篇 运维工程师必考