3223: Tyvj 1729 文艺平衡树

时间:2023-03-08 15:34:58

3223: Tyvj 1729 文艺平衡树

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 1347  Solved: 724
[Submit][Status]

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

Source

平衡树

题解:一道经典的区间反转问题(既然网上的各个解说大多不尽详细,那么就由我来重复一遍吧么么哒)——如题目的Source所言,这里面要用到splay树(但是个人对平衡树这一定义持怀疑态度——毕竟平衡树是二叉查找树,当你区间反转来反转去后还符合二叉查找树的性质么?)首先把区间的右界再往右一个的点splay到树顶,然后把左界再往左一个的点splay到树顶的子树位置(详见我的过程splay2),这样子此子树根的右子树则刚刚好由我们所需要反转的节点构成,起初我一个个一层层反转,于是不出所料——程序运行速度还没有O(n^2)的暴力快(phile:那不是显然的么 HansBug:TT)于是我们的重头戏——lazytag上场——这里面的懒标记可以类比线段树里面的,它需要支持一个向下扩展的操作(详见过程ext),即反转此点左右子树,然后把标记蔓延到左右子树上,自己的标记解除,然后注意的是每次左转或者右转节点时需要保证关键的节点先去除掉lazytag,求第N号节点、最终求序列时也是如此,然后很嗨皮的AC么么哒(HansBug:代码太长求原谅TT)

 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.