bzoj 2002 LCT

时间:2022-04-24 15:51:45

LCT最基础的题,就用到了一个ACCESS操作

首先我们将这个绵羊弹飞的情况看成一颗树,那么假设X点被弹飞到

Y点,那么Y为X的父亲节点,弹飞的话父亲节点为n+1(虚设)

那么每个询问就是询问X点到根节点n+1的路径长度(节点数)

每个修改操作就是将以X为根节点的子树和X的父亲断开,连接到Y上

这样简单的维护森林连通性的问题,动态树中的LCT解决就行了

/**************************************************************
Problem:
User: BLADEVIL
Language: Pascal
Result: Accepted
Time: ms
Memory: kb
****************************************************************/ //By BLADEVIL
var
n, m :longint;
father, size :array[-..] of longint;
son :array[-..,..] of longint;
root :array[-..] of boolean; procedure update(x:longint);
begin
size[x]:=size[son[x,]]+size[son[x,]]+;
end; procedure left_rotate(x:longint);
var
y :longint;
begin
y:=son[x,];
son[x,]:=son[y,];
father[son[x,]]:=x;
son[y,]:=x;
if x=son[father[x],] then
son[father[x],]:=y else
if x=son[father[x],] then
son[father[x],]:=y;
father[y]:=father[x];
father[x]:=y;
root[y]:=root[x] xor root[y];
root[x]:=root[x] xor root[y];
update(x); update(y);
end; procedure right_rotate(x:longint);
var
y :longint;
begin
y:=son[x,];
son[x,]:=son[y,];
father[son[x,]]:=x;
son[y,]:=x;
if x=son[father[x],] then
son[father[x],]:=y else
if x=son[father[x],] then
son[father[x],]:=y;
father[y]:=father[x];
father[x]:=y;
root[y]:=root[y] xor root[x];
root[x]:=root[y] xor root[x];
update(x); update(y);
end; procedure splay(x:longint);
begin
while not root[x] do
if x=son[father[x],] then
left_rotate(father[x]) else
right_rotate(father[x]);
end; procedure access(x:longint);
var
y :longint;
begin
splay(x);
while father[x]<> do
begin
y:=father[x];
splay(y);
root[son[y,]]:=true;
root[x]:=false;
son[y,]:=x;
update(y);
splay(x);
end;
end; procedure init;
var
i :longint;
begin
read(n);
for i:= to n do
begin
read(father[i]);
father[i]:=father[i]+i;
if father[i]>n then father[i]:=n+;
end;
read(m);
end; procedure main;
var
i :longint;
x, y, z :longint;
begin
for i:= to n+ do size[i]:=;
fillchar(root,sizeof(root),true);
for i:= to m do
begin
read(x);
if x= then
begin
read(y); inc(y);
access(y);
writeln(size[son[y,]]);
end else
begin
read(y,z); inc(y);
splay(y);
father[son[y,]]:=father[y];
root[son[y,]]:=true;
son[y,]:=;
size[y]:=size[son[y,]]+;
father[y]:=y+z;
if father[y]>n then father[y]:=n+;
end;
end;
end; begin
init;
main;
end.