实现的功能:将序列区间反转,并维护

详见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的更多相关文章

  1. 算法模板——splay区间反转 2

    实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树) 这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善 这里面没 ...

  2. Splay 区间反转

    同样的,我们以一道题来引入. 传送门 这次的任务比较少,只要求进行区间反转.区间反转? 这个好像用啥都是O(n)的吧……(这次vector,set也救不了你了) 我们来使用splay解决这个问题.我们 ...

  3. hdu 1890 Robotic Sort(splay 区间反转+删点)

    题目链接:hdu 1890 Robotic Sort 题意: 给你n个数,每次找到第i小的数的位置,然后输出这个位置,然后将这个位置前面的数翻转一下,然后删除这个数,这样执行n次. 题解: 典型的sp ...

  4. [bzoj3223]文艺平衡树(splay区间反转模板)

    解题关键:splay模板题. #include<cstdio> #include<cstring> #include<algorithm> #include< ...

  5. HDU 1890 - Robotic Sort - [splay][区间反转+删除根节点]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1890 Time Limit: 6000/2000 MS (Java/Others) Memory Li ...

  6. HDU3487 Play with Chain splay 区间反转

    HDU3487 splay最核心的功能是将平衡树中的节点旋转到他的某个祖先的位置,并且维持平衡树的性质不变. 两个操作(数组实现) cut l,r, c把[l,r]剪下来放到剩下序列中第c个后面的位置 ...

  7. 2018牛客网暑期ACM多校训练营(第三场) H - Shuffle Cards - [splay伸展树][区间移动][区间反转]

    题目链接:https://www.nowcoder.com/acm/contest/141/C 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言524288K ...

  8. 算法笔记--Splay && Link-Cut-Tree

    Splay 参考:https://tiger0132.blog.luogu.org/slay-notes 普通模板: ; ], val[N], cnt[N], fa[N], sz[N], lazy[N ...

  9. hdu1890 伸展树(区间反转)

    对于大神来说这题是水题.我搞这题花了快2天. 伸展树的优点有什么,就是树不管你怎么旋转序列是不会改变得,并且你要使区间反转,只要把第k大的点转到根结点,那么它的左子树就是要交换的区间[l,r),然后交 ...

随机推荐

  1. React快速入门教程

    简介 Facebook官网介绍:React 是一个用来构建用户界面的 JavaScript 库.相当于 MVC 架构的 V 层. React 的核心思想是:封装组件,各个组件维护自己的状态和UI,当状 ...

  2. Word常用实用知识2

      纯手打,可能有错别字,使用的版本是office Word 2013  转载请注明出处 http://www.cnblogs.com/hnnydxgjj/p/6296863.html,谢谢. 批注和 ...

  3. Swift资料大全

    简单看了一下swift的语法,个人感觉相对于object-c很是简单明了.Swift的出现并不能说明iOS开发简单了很多,有可能会变得复杂,你需要学习两门编程语言,因为在程序员的世界里,务实并不是最重 ...

  4. Biztalk AS2开发经验总结

    一.    准备证书    4 1.    升级win2008 R2证书服务    4 2.    申请证书    6 二.    配置证书    13 1.    为AS2配置证书    13 2. ...

  5. HDU 2080 夹角有多大II

    夹角有多大II Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Sub ...

  6. QML Object Attributes QML对象属性

    QML Object Attributes Every QML object type has a defined set of attributes. Each instance of an obj ...

  7. cordova调用本地SQLite数据库的方法

    第一篇技术博客,写下来和大家分享今天所学,其次自己也巩固一下. 整个下午的时间用来钻研如何用cordova调用移动端本地SQLite数据库.首先我并不是用eclipse来编程的,而是用cordova建 ...

  8. 负笈前行--DPDK代码风格

    每个公司都会有自己代码风格或者编程规范,都旨在防范编程语言的一些陷阱或者提高代码效率,还有就是保持一致编码风格来提高代码可读性,方便code review: 或者说代码的一种美学,比如python也就 ...

  9. Java丨博客系统

    后台界面: 前台界面: 这是一个由我带着刚入门的几位实习生做的一个博客系统,希望给java入门的学习者一个例子,这个系统还需要完善,主体功能是实现了,还差一些细节,如果需要源码的话可以在下方给我留言! ...

  10. Fiddler教程【转】

    阅读目录 Fiddler的基本介绍 Fiddler的工作原理 同类的其它工具 Fiddler如何捕获Firefox的会话 Fiddler如何捕获HTTPS会话 Fiddler的基本界面 Fiddler ...