算法模板——splay区间反转 1
实现的功能:将序列区间反转,并维护
详见BZOJ3223
var
i,j,k,l,m,n,head,a1,a2:longint;
s1:ansistring;
a,b,c,d,fat,lef,rig:array[..] of longint;
procedure swap(var x,y:longint);inline;
var z:longint;
begin
z:=x;x:=y;y:=z;
end; procedure ext(x:longint);inline;
begin
if (x=) then exit;
if c[x]= then exit;
swap(lef[x],rig[x]);
c[x]:=;
c[lef[x]]:=-c[lef[x]];
c[rig[x]]:=-c[rig[x]];
c[]:=;
end;
procedure rt(x:longint);inline;
var f,l:longint;
begin
if x= then exit;
ext(x);
if lef[x]= then exit;
ext(lef[x]);
f:=x;l:=lef[x];
b[lef[x]]:=b[x];
b[x]:=b[rig[x]]++b[rig[l]];
lef[x]:=rig[l];
fat[rig[l]]:=x;
rig[l]:=x;
fat[l]:=fat[x];
fat[x]:=l;
if rig[fat[l]]=x then rig[fat[l]]:=l;
if lef[fat[l]]=x then lef[fat[l]]:=l;
fat[]:=;
end;
procedure lt(x:longint);inline;
var f,r:longint;
begin
if x= then exit;
ext(x);if rig[x]= then exit;
ext(rig[x]);
f:=x;r:=rig[x];
b[rig[x]]:=b[x];
b[x]:=+b[lef[x]]+b[lef[r]];
rig[x]:=lef[r];
fat[lef[r]]:=x;
lef[r]:=x;
fat[r]:=fat[x];
fat[x]:=r;
if rig[fat[r]]=x then rig[fat[r]]:=r;
if lef[fat[r]]=x then lef[fat[r]]:=r;
fat[]:=;
end;
procedure ins(x,y:longint);inline;
begin
if a[y]<a[x] then
begin
if lef[x]= then
begin
lef[x]:=y;
fat[y]:=x;
end
else ins(lef[x],y);
end
else
begin
if rig[x]= then
begin
rig[x]:=y;
fat[y]:=x;
end
else ins(rig[x],y);
end;
b[x]:=+b[lef[x]]+b[rig[x]];
end;
procedure up2(var x:longint);inline;
begin
if (fat[x]=) or (x=) then exit;
if lef[fat[x]]=x then
begin
if lef[fat[fat[x]]]=fat[x] then
begin
rt(fat[fat[x]]);
rt(fat[x]);
end
else
begin
rt(fat[x]);
lt(fat[x]);
end;
end
else
begin
if rig[fat[fat[x]]]=fat[x] then
begin
lt(fat[fat[x]]);
lt(fat[x]);
end
else
begin
lt(fat[x]);
rt(fat[x]);
end;
end;
end;
procedure up1(x:longint);inline;
begin
if (x=) or (fat[x]=) then exit;
if lef[fat[x]]=x then rt(fat[x]) else lt(fat[x]);
end;
procedure splay(x:longint);inline;
begin
if (x=) or (fat[x]=) then exit;
while fat[fat[x]]> do
up2(x);
if fat[x]> then up2(x);
head:=x;
end;
procedure splay2(x:longint);inline;
begin
if (x=) or (fat[x]=) then exit;
while fat[fat[fat[x]]]> do
up2(x);
if fat[fat[x]]> then up1(x);
end;
function getrank(x,y:longint):longint;inline;
begin
if (x=) then exit();
ext(x);
if (b[lef[x]]+)=y then exit(x);
if (b[lef[x]]+)>y then exit(getrank(lef[x],y)) else exit(getrank(rig[x],y--b[lef[x]]));
end;
procedure turn(x,y:longint);inline;
var a1,a2:longint;
begin
if (x=) and (y=n) then
c[head]:=-c[head]
else
begin
if (x=) then
begin
a1:=getrank(head,y+);
splay(a1);
ext(a1);
c[lef[a1]]:=-c[lef[a1]];
end
else
begin
if (y=n) then
begin
a2:=getrank(head,x-);
splay(a2);
ext(a2);
c[rig[a2]]:=-c[rig[a2]];
end
else
begin
a1:=getrank(head,x-);
a2:=getrank(head,y+);
splay(a2);splay2(a1);
ext(a2);ext(a1);
c[rig[a1]]:=-c[rig[a1]];
end;
end;
end;
end;
function showoff(x:longint):ansistring;inline;
var s1:ansistring;
begin
if x= then exit('');
ext(x);
str(x,s1);
exit(showoff(lef[x])+s1+' '+showoff(rig[x]));
end;
begin
readln(n,m);
for i:= to n do
begin
a[i]:=i;c[i]:=;b[i]:=;
end;
head:=;
for i:= to n do
begin
ins(head,i);
splay(random(i)+);
end;
for i:= to m do
begin
readln(a1,a2);
turn(a1,a2);
end;
s1:=showoff(head);
writeln(s1);
readln;
end.
算法模板——splay区间反转 1的更多相关文章
- 算法模板——splay区间反转 2
实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树) 这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善 这里面没 ...
- Splay 区间反转
同样的,我们以一道题来引入. 传送门 这次的任务比较少,只要求进行区间反转.区间反转? 这个好像用啥都是O(n)的吧……(这次vector,set也救不了你了) 我们来使用splay解决这个问题.我们 ...
- hdu 1890 Robotic Sort(splay 区间反转+删点)
题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的sp ...
- [bzoj3223]文艺平衡树(splay区间反转模板)
解题关键:splay模板题. #include<cstdio> #include<cstring> #include<algorithm> #include< ...
- HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...
- HDU3487 Play with Chain splay 区间反转
HDU3487 splay最核心的功能是将平衡树中的节点旋转到他的某个祖先的位置,并且维持平衡树的性质不变. 两个操作(数组实现) cut l,r, c把[l,r]剪下来放到剩下序列中第c个后面的位置 ...
- 2018牛客网暑期ACM多校训练营(第三场) H - Shuffle Cards - [splay伸展树][区间移动][区间反转]
题目链接:https://www.nowcoder.com/acm/contest/141/C 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...
- 算法笔记--Splay && Link-Cut-Tree
Splay 参考:https://tiger0132.blog.luogu.org/slay-notes 普通模板: ; ], val[N], cnt[N], fa[N], sz[N], lazy[N ...
- hdu1890 伸展树(区间反转)
对于大神来说这题是水题.我搞这题花了快2天. 伸展树的优点有什么,就是树不管你怎么旋转序列是不会改变得,并且你要使区间反转,只要把第k大的点转到根结点,那么它的左子树就是要交换的区间[l,r),然后交 ...
随机推荐
- Quercus
其实,我不确定Quercus是否可以被认定为一门JVM语言:其次Quercus这个东东分开源版与商业版,开源版只能解释执行.而商业版能编译成Java字节码. 但我知道国内,阿里巴巴很早就在使用它,当然 ...
- SVG在网页中的四种使用方式
1,直接打开simple.svg <svg xmlns="http://www.w3.org/2000/svg" width="200" height=& ...
- CCNA网络工程师学习进程(8)访问控制列表ACL
前面几节我们介绍了路由器的路由配置,接下来几节我们将介绍路由器的高级配置应用,包括ACL.NAT.DHCP.PPP.VPN和远程连接等的配置. (1)ACL概述: ACL(Access C ...
- Professional C# 6 and .NET Core 1.0 - What’s New in C# 6
本文为转载,学习研究 What's New in C# 6 With C# 6 a new C# compiler is available. It's not only that a source ...
- windows 下odoo 不同版本安装运行问题
在开发测试不同版本odoo时,总会遇到在同一浏览器下运行出错的状况.虽然可以把不同版本分属不同的端口,但是登录标识会入写用户本地浏览器cookie,由于cookie只匹配域名及路径但是不区分端口, 所 ...
- asp.net权限认证:HTTP基本认证(http basic)
asp.net权限认证系列 asp.net权限认证:Forms认证 asp.net权限认证:HTTP基本认证(http basic) asp.net权限认证:Windows认证 asp.net权限认证 ...
- 使用python制作ArcGIS插件(5)其他技巧
使用python制作ArcGIS插件(5)其他技巧 by 李远祥 使用python做插件开发,除了了解ArcToolBox工具之外,还需要在了解ArcPy的相关函数和接口.只有掌握了这些,才可以顺利的 ...
- java对获取的字节数组进行处理
java对获取的字节数组bytes[]进行处理: 第一种,直接将该字节数组转换为字符串(部分): String content = ,); //从位置0开始获取2个字节 这样,对获取的数据报进行全部转 ...
- 警惕Dictionary和SortedDictionary的顺序陷阱
/*我们查询资料得知Dictionary的遍历顺序和添加Add时的顺序是一致的,不像 HashTable 顺序不可知;于是我要依赖Dictionary的这种顺序一致特性做一个,固定大小400长度的队列 ...
- 5个Android开发中比较常见的内存泄漏问题及解决办法
android中一个对象已经不需要了,但是其他对象还持有他的引用,导致他不能回收,导致这个对象暂存在内存中,这样内存泄漏就出现了. 内存泄漏出现多了,会是应用占用过多的没存,当占用的内存超过了系统 ...