树链剖分在边上的应用
比维护点稍微麻烦一点,是对每条边标号,并且要记录每个点父亲边的编号和重儿子
然后注意各种细节
线段树上和bzoj1858的维护方法类似,覆盖的优先级高于加
具体见程序,完全是为了提升状态的练习
type node=record
po,next,num:longint;
end; var son,a,d,fa,fp,f2,b,c,p,size,top:array[..] of longint;
w:array[..] of node;
tree,laz1,laz2:array[..*] of longint;
x,y,z,i,n,t:longint;
ch:char;
s:string; function max(a,b:longint):longint;
begin
if a>b then exit(a) else exit(b);
end; procedure add(x,y,c:longint);
begin
inc(t);
w[t].po:=y;
w[t].num:=c;
w[t].next:=p[x];
p[x]:=t;
end; procedure swap(var a,b:longint);
var c:longint;
begin
c:=a;
a:=b;
b:=c;
end; procedure push(i:longint);
begin
if laz1[i]<>- then
begin
tree[i*]:=laz1[i];
tree[i*+]:=laz1[i];
laz1[i*]:=laz1[i];
laz1[i*+]:=laz1[i];
laz1[i]:=-;
laz2[i]:=;
end;
if laz2[i]<> then
begin
inc(tree[i*],laz2[i]);
inc(tree[i*+],laz2[i]);
if laz1[i*]<>- then inc(laz1[i*],laz2[i])
else inc(laz2[i*],laz2[i]);
if laz1[i*+]<>- then inc(laz1[i*+],laz2[i])
else inc(laz2[i*+],laz2[i]);
laz2[i]:=;
end;
end; procedure dfs1(x:longint);
var i,y:longint;
begin
i:=p[x];
size[x]:=;
while i<> do
begin
y:=w[i].po;
if fa[x]<>y then
begin
fp[y]:=w[i].num;
fa[y]:=x;
d[y]:=d[x]+;
dfs1(y);
size[x]:=size[x]+size[y];
end;
i:=w[i].next;
end;
end; procedure dfs2(x:longint);
var i,y,q:longint;
begin
i:=p[x];
q:=;
while i<> do
begin
if fa[w[i].po]=x then
if size[w[q].po]<size[w[i].po] then q:=i;
i:=w[i].next;
end;
if q= then exit;
top[w[q].po]:=top[x];
son[x]:=w[q].num;
inc(t);
c[t]:=w[q].num;
b[w[q].num]:=t;
dfs2(w[q].po);
i:=p[x];
while i<> do
begin
y:=w[i].po;
if (i<>q) and (fa[y]=x) then
begin
inc(t);
c[t]:=w[i].num;
b[w[i].num]:=t;
top[y]:=y;
dfs2(y);
end;
i:=w[i].next;
end;
end; procedure build(i,l,r:longint);
var m:longint;
begin
laz1[i]:=-;
laz2[i]:=;
if l=r then tree[i]:=a[c[l]]
else begin
m:=(l+r) shr ;
build(i*,l,m);
build(i*+,m+,r);
tree[i]:=max(tree[i*],tree[i*+]);
end;
end; procedure change(i,l,r,x,y,z:longint);
var m:longint;
begin
if x>y then exit;
if (x<=l) and (y>=r) then
begin
laz1[i]:=z;
tree[i]:=z;
laz2[i]:=;
end
else begin
push(i);
m:=(l+r) shr ;
if x<=m then change(i*,l,m,x,y,z);
if y>m then change(i*+,m+,r,x,y,z);
tree[i]:=max(tree[i*],tree[i*+]);
end;
end; procedure fadd(i,l,r,x,y:longint);
var m:longint;
begin
if x>y then exit;
if (x<=l) and (y>=r) then
begin
if laz1[i]<>- then inc(laz1[i],z)
else inc(laz2[i],z);
inc(tree[i],z);
end
else begin
push(i);
m:=(l+r) shr ;
if x<=m then fadd(i*,l,m,x,y);
if y>m then fadd(i*+,m+,r,x,y);
tree[i]:=max(tree[i*],tree[i*+]);
end;
end; function getans(i,l,r,x,y:longint):longint;
var m,s:longint;
begin
if x>y then exit();
if (x<=l) and (y>=r) then exit(tree[i])
else begin
push(i);
m:=(l+r) shr ;
s:=;
if x<=m then s:=getans(i*,l,m,x,y);
if y>m then s:=max(s,getans(i*+,m+,r,x,y));
exit(s);
end;
end; procedure cover(x,y:longint);
var f1,f2:longint;
begin
f1:=top[x];
f2:=top[y];
while f1<>f2 do
begin
if d[f1]>=d[f2] then
begin
change(,,n-,b[fp[f1]],b[fp[x]],z);
x:=fa[f1];
end
else begin
change(,,n-,b[fp[f2]],b[fp[y]],z);
y:=fa[f2];
end;
f1:=top[x];
f2:=top[y];
end;
if x=y then exit
else if b[fp[x]]>b[fp[y]] then swap(x,y);
change(,,n-,b[son[x]],b[fp[y]],z);
end; procedure add(x,y:longint);
var f1,f2:longint;
begin
f1:=top[x];
f2:=top[y];
while f1<>f2 do
begin
if d[f1]>=d[f2] then
begin
fadd(,,n-,b[fp[f1]],b[fp[x]]);
x:=fa[f1];
end
else begin
fadd(,,n-,b[fp[f2]],b[fp[y]]);
y:=fa[f2];
end;
f1:=top[x];
f2:=top[y];
end;
if x=y then exit
else if b[fp[x]]>b[fp[y]] then swap(x,y);
fadd(,,n-,b[son[x]],b[fp[y]]);
end; function ask(x,y:longint):longint;
var s,f1,f2:longint;
begin
s:=;
f1:=top[x];
f2:=top[y];
while f1<>f2 do
begin
if d[f1]>=d[f2] then
begin
s:=max(s,getans(,,n-,b[fp[f1]],b[fp[x]]));
x:=fa[f1];
end
else begin
s:=max(s,getans(,,n-,b[fp[f2]],b[fp[y]]));
y:=fa[f2];
end;
f1:=top[x];
f2:=top[y];
end;
if x=y then exit(s)
else if b[fp[x]]>b[fp[y]] then swap(x,y);
s:=max(s,getans(,,n-,b[son[x]],b[fp[y]]));
exit(s);
end; begin
readln(n);
for i:= to n- do
begin
readln(x,y,a[i]);
add(x,y,i);
add(y,x,i);
end;
t:=;
dfs1();
top[]:=;
dfs2();
while true do
begin
read(ch);
s:='';
while ch<>' ' do
begin
s:=s+ch;
if s='Stop' then halt;
read(ch);
end;
read(x,y);
if s='Max' then
writeln(ask(x,y))
else if s='Change' then
change(,,n-,b[x],b[x],y)
else if s='Cover' then
begin
read(z);
cover(x,y);
end
else if s='Add' then
begin
read(z);
add(x,y);
end;
readln;
end;
end.