实现功能:同splay区间反转 1(基于BZOJ3223 文艺平衡树)

这次改用了一个全新的模板(HansBug:琢磨了我大半天啊有木有),大大简化了程序,同时对于splay的功能也有所完善

这里面没有像一般二叉排序树那样子用一个参量进行排序,而是直接以中序遍历来构建了一个普通的二叉树(当然也可以把每个点的中序遍历排名视作参量),然后插入的时候就是指定位置插入(这个就比较像是文本插入了)

总之得到了较大的提升,代码优美程度也提高不少

 var
i,j,k,l,m,n,head,tot,ll:longint;
a,lef,rig,b,c:array[..] of longint;
procedure swap(var x,y:longint);
var z:longint;
begin
z:=x;x:=y;y:=z;
end;
procedure ext(x:longint); //经典的下推操作,有木有很像线段树呢= =
begin
if x= then exit;
if c[x]= then exit;
if lef[x]<> then c[lef[x]]:=-c[lef[x]];
if rig[x]<> then c[rig[x]]:=-c[rig[x]];
swap(lef[x],rig[x]);
c[x]:=;
end;
procedure rt(var x:longint); //lt、rt函数和Treap的极其相似!!!
var f,l:longint;
begin
ext(x);ext(lef[x]);
if (x=) or (lef[x]=) then exit;
f:=x;l:=lef[x];
b[lef[x]]:=b[x];
b[x]:=+b[rig[x]]+b[rig[lef[x]]];
lef[f]:=rig[l];
rig[l]:=f;
x:=l;
end;
procedure lt(var x:longint);
var f,r:longint;
begin
ext(x);ext(rig[x]);
if (x=) or (rig[x]=) then exit;
f:=x;r:=rig[x];
b[rig[x]]:=b[x];
b[x]:=+b[lef[x]]+b[lef[rig[x]]];
rig[f]:=lef[r];
lef[r]:=f;
x:=r;
end;
procedure splay(var head:longint;x:longint); //萌萌哒伸展——而且还可以支持伸展的任意位置,只要设定head的位置,比如像后面的splay(rig[head],x);
begin
if head= then exit;
ext(head);
if (b[lef[head]]+)=x then exit;
if x<(b[lef[head]]+) then
begin
ext(lef[head]);
if x=(b[lef[lef[head]]]+) then rt(head) else
if x<(b[lef[lef[head]]]+) then
begin
splay(lef[lef[head]],x);
rt(head);rt(head);
end
else
begin
splay(rig[lef[head]],x--b[lef[lef[head]]]);
lt(lef[head]);rt(head);
end;
end
else
begin
ext(rig[head]);
x:=x--b[lef[head]];
if x=(b[lef[rig[head]]]+) then lt(head) else
if x<(b[lef[rig[head]]]+) then
begin
splay(lef[rig[head]],x);
rt(rig[head]);lt(head);
end
else
begin
splay(rig[rig[head]],x--b[lef[rig[head]]]);
lt(head);lt(head);
end;
end;
end;
procedure ins(x,y:longint); //这里的ins操作已经可以支持指定位置插入子伸展树了
begin
if head= then
begin
head:=y;
exit;
end;
if x=b[head] then
begin
splay(head,b[head]);
rig[head]:=y;
inc(b[head],b[y]);
end
else if x= then
begin
splay(head,);
lef[head]:=y;
inc(b[head],b[y]);
end
else begin
splay(head,x);
splay(rig[head],x+);
lef[rig[head]]:=y;
inc(b[rig[head]],b[y]);
inc(b[head],b[y]);
end;
end;
procedure putinvalue(x,y:longint); //加入单个值
begin
inc(tot);
A[TOT]:=x;b[tot]:=;
lef[tot]:=;rig[tot]:=;
ins(y,tot);
end; procedure turnover(x,y:longint); //翻转,核心思想还是打标记
begin
if x=y then exit;
if (x=) and (y=n) then
begin
c[head]:=-c[head];
exit;
end;
if (x=) then
begin
splay(head,y+);
c[lef[head]]:=-c[lef[head]];
end
else if (y=n) then
begin
splay(head,x-);
c[rig[head]]:=-c[rig[head]];
end
else begin
splay(head,x-);
splay(rig[head],y-x+);
c[lef[rig[head]]]:=-c[lef[rig[head]]];
end;
end;
function showtree(head:longint):ansistring;
var s1,s2,s3,s4:ansistring;
begin
if head= then exit('');
str(a[head],s1);
s2:=showtree(lef[head]);
s3:=showtree(rig[head]);
if c[head]= then
begin
s4:=s2;
s2:=s3;
s3:=s4;
end;
if s3<>'' then s3:=','+s3;
s2:=s2+s3;
if s2<>'' then s2:='('+s2+')';
exit(s1+s2);
end;
procedure putout(x:longint);
begin
if x= then exit;
ext(x);
putout(lef[x]);
write(a[x],' ');
putout(rig[x]);
end;
begin
readln(n,m);head:=;tot:=;
for i:= to n do putinvalue(i,i-);
fillchar(c,sizeof(c),);
for i:= to m do
begin
readln(j,k);
turnover(j,k);
l:=; end;
putout(head);
writeln;
readln;
end.

算法模板——splay区间反转 2的更多相关文章

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

    实现的功能:将序列区间反转,并维护 详见BZOJ3223 var i,j,k,l,m,n,head,a1,a2:longint; s1:ansistring; a,b,c,d,fat,lef,rig: ...

  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. C# 程序只能执行一次

    应用程序的主入口点. //每一个程序只能运行一个实例 bool isRun = false; System.Threading.Mutex m = new System.Threading.Mutex ...

  2. 基于Doubango的iOS客户端开源框架

    一.ios-ngn-statck工程 1.Tests ---功能测试 2.底层模块(c和c++) Doubango --- 基于3GPP IMS/RCS 并能用于嵌入式和桌面系统的开源框架 1) ti ...

  3. MVC + AngularJS 初体验(实现表单操作)

    AngularJS AngularJS 通过新的属性和表达式扩展了 HTML. AngularJS 可以构建一个单一页面应用程序(SPAs:Single Page Applications). Ang ...

  4. TypeScript教程3

    1.快速回顾一下这JavaScript中的命名函数和匿名函数: 纯文本查看 复制代码 1 2 3 4 5 //Named functionfunction add(x, y) {     return ...

  5. Python学习--13 文件I/O

    Python内置了读写文件的函数,用法和C是兼容的. 读写文件前,我们先必须了解一下,在磁盘上读写文件的功能都是由操作系统提供的,现代操作系统不允许普通的程序直接操作磁盘,所以,读写文件就是请求操作系 ...

  6. [Echarts]用Echarts绘制饼状图

    在项目网站的网页中,有这样一幅图: 心血来潮,想使用百度Echarts来绘制一下,可是没能绘制得完全一样,Echarts饼状图的label不能在图形下面放成一行,最后的效果是这样子的: 鼠标移动到it ...

  7. [nodejs] day1-创建服务器

    一.使用匿名函数(新建文件service.js)创建一个服务器: var http = require("http"); //Node.js自带的 http 模块,并且把它赋值给 ...

  8. Professional C# 6 and .NET Core 1.0 - 38 Entity Framework Core

    本文内容为转载,重新排版以供学习研究.如有侵权,请联系作者删除. 转载请注明本文出处:Professional C# 6 and .NET Core 1.0 - 38 Entity Framework ...

  9. 正则表达式中的match,test,exec,search的返回值

    今天突然被问到了正则表达式,因为长时间不用突然不知道怎么用了,只知道有这么个东西.然后去网上查了一下,感觉写的不少,但解释的有点模糊,今天我来浅谈一下. 1,match的用法 A,在不加全局“g”的情 ...

  10. springMVC整合Junit4进行单元测试

    springMVC整合Junit4进行单元测试 标签: springMVC整合Junit4junit单元测试教程springMVC入门教程   spring(10)  版权声明:本文为博主原创文章,未 ...