算法模板——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),然后交 ...
随机推荐
- mongoDB文档操作
数据库操作无非就是增.删.改.查.这篇主要介绍增.删.改. 1.增 Mongodb插入操作很简单,使用关键字“insert”.实例: > db.test.blog.insert({"h ...
- jQuery css,position,offset,scrollTop,scrollLeft用法
jQuery css,position,offset,scrollTop,scrollLeft用法: <%@ page language="java" import=&quo ...
- Java NIO------基础理论之缓存区
1.概述:NIO我的理解就是 New IO,是API1.4里提供的新的API,为所有的原始类型做缓存支持. NIO主要的核心组成部分: Buffer(缓存) Channels(通道) Selector ...
- Oracle-11g 基于 NBU 的 rman 冷备份及恢复
html,body { font-size: 15px } body { font-family: Helvetica, "Hiragino Sans GB", "微软雅 ...
- .Net多线程编程—使用Visual Studio 2012进行调试
1 相关概念 1)栈帧 C语言中,每个栈帧对应着一个未运行完的函数.栈帧中保存了该函数的返回地址和局部变量. 栈帧也叫过程活动记录,是编译器用来实现过程/函数调用的一种数据结构. 2)单步执行与单步函 ...
- EntityFramework Core 1.1有哪些新特性呢?我们需要知道
前言 在项目中用到EntityFramework Core都是现学现用,及时发现问题及时测试,私下利用休闲时间也会去学习其他未曾遇到过或者用过的特性,本节我们来讲讲在EntityFramework C ...
- u3d脚本生命周期
- SQL Server 安装报错找不到vc_red.msi
问题描述: 今天给 WIN 7 SP1 操作系统安装 SQL Server 2014 ,报错找不到vc_red.msi (图片来源网络,请忽略2012字样..) 问题解决: 1.由于安装程序提 ...
- for语句输出图形
一.输出以下图形 ******************************** 用for...for...嵌套循环,内循环控制每一行的个数(即列数),外循环控制行数 class ForDraw { ...
- iOS真机测试友盟碰到错误linker command failed with exit code 1 (use -v to see invocation) 百度地图的检索失败 sqlite 错误码
因为友盟不支持bitcode 在模拟器上运行正常,但是在模拟器上就会报错,这是因为xocde7之后增加了一个bitcode,bitcode是被编译程序的一种中间形式的代 码.包含bitcode配置的程 ...