Link Cut Tree (paint)

原题:SDOI 2011 染色

方法:LCT维护

program paint;
type
    link=^node; node=record x:longint; next:link; end;
const
    maxn=100000;
var
    q,f,l,r,c,lc,rc,rev,d:array[0..maxn]of longint;
    tp:array[0..maxn]of boolean;
    ge:array[0..maxn]of link;
procedure swap(var a,b:boolean);
begin
    a:=a xor b; b:=a xor b; a:=a xor b;
end;
procedure update(const x:longint);
begin
    if l[x]<>0 then lc[x]:=lc[l[x]] else lc[x]:=c[x];
    if r[x]<>0 then rc[x]:=rc[r[x]] else rc[x]:=c[x];
    d[x]:=d[l[x]]+d[r[x]]+1-ord(rc[l[x]]=c[x])-ord(lc[r[x]]=c[x]);
end;
procedure zig(x:longint);var y:longint;
begin
    y:=f[x]; f[x]:=f[y]; swap(tp[x],tp[y]);
    if y=l[f[y]] then l[f[y]]:=x else
    if y=r[f[y]] then r[f[y]]:=x;    
    l[y]:=r[x]; if l[y]<>0 then f[l[y]]:=y;
    r[x]:=y; f[y]:=x;
    update(y); update(x);
end;
procedure zag(x:longint);var y:longint;
begin
    y:=f[x]; f[x]:=f[y]; swap(tp[x],tp[y]);
    if y=l[f[y]] then l[f[y]]:=x else
    if y=r[f[y]] then r[f[y]]:=x;
    r[y]:=l[x]; if r[y]<>0 then f[r[y]]:=y;
    l[x]:=y; f[y]:=x;
    update(y); update(x);
end;
procedure modify(const x,k:longint);
begin
    if x=0 then exit; rev[x]:=k;
    c[x]:=k; lc[x]:=k; rc[x]:=k; d[x]:=1;
end;
procedure put(const x:longint);
begin
    if rev[x]=0 then exit;
    modify(l[x],rev[x]);
    modify(r[x],rev[x]);
    rev[x]:=0;
end;
procedure splay(x:longint);var y:longint;
begin
    put(x);
    while not tp[x] do begin
	y:=f[x];
	if (not tp[y])and(f[y]<>0) then put(f[y]);
	put(y); put(x);
	if tp[y] then begin
	    if x=l[y] then zig(x) else zag(x); exit;
	end;
	if x=l[y] then
	    if y=l[f[y]] then begin zig(y); zig(x); end else begin zig(x); zag(x); end
	else
	    if y=r[f[y]] then begin zag(y); zag(x); end else begin zag(x); zig(x); end;
    end;
end;
procedure color(x,y,k:longint); var i:longint;
begin
    i:=0;
    while x<>0 do begin
	splay(x);
	tp[r[x]]:=true; tp[i]:=false; r[x]:=i; f[i]:=x;
	update(x);
	i:=x; x:=f[x];
    end;
    i:=0;
    while y<>0 do begin
	splay(y);
	if f[y]=0 then begin
	    c[y]:=k; modify(i,k); modify(r[y],k); update(y); exit;
	end;
	tp[r[y]]:=true; tp[i]:=false; r[y]:=i; f[i]:=y;
	update(y);
	i:=y; y:=f[y];
    end;
end;
function query(x,y:longint):longint;var i:longint;
begin
    i:=0;
    while x<>0 do begin
	splay(x);
	tp[r[x]]:=true; tp[i]:=false; r[x]:=i; f[i]:=x;
	update(x);
	i:=x; x:=f[x];
    end;
    i:=0;
    while y<>0 do begin
	splay(y);
	if f[y]=0 then begin
	    query:=d[r[y]]+d[i]+1-ord(c[y]=lc[r[y]])-ord(c[y]=lc[i]);
	    writeln(query); exit;
	end;
	tp[r[y]]:=true; tp[i]:=false; r[y]:=i; f[i]:=y;
	update(y);
	i:=y; y:=f[y];
    end;
end;
procedure Bfs;var h,t:longint; p:link;
begin
    h:=0; t:=1; q[1]:=1;
    while h<t do begin
	inc(h); p:=ge[q[h]];
	while p<>nil do begin
	    if p^.x<>f[q[h]] then begin
		inc(t); q[t]:=p^.x; f[p^.x]:=q[h];
	    end;
	    p:=p^.next;
	end;
    end;
end;
procedure push(var x:link; const t:longint); var p:link;
begin
    new(p); p^.x:=t; p^.next:=x; x:=p;
end;
var n,m,x,y,z,i:longint; ch:char;
begin
    assign(input,'input.txt'); reset(input);
    assign(output,'output.txt'); rewrite(output);
    readln(n,m); 
    for i:=1 to n do begin
	read(c[i]); lc[i]:=c[i]; rc[i]:=i; d[i]:=1; tp[i]:=true; rev[i]:=0;
    end;
    for i:=1 to n-1 do begin
	readln(x,y); push(ge[x],y); push(ge[y],x);
    end;
    Bfs;
    for i:=1 to m do begin
	read(ch);
	case ch of
	    'C':begin readln(x,y,z); color(x,y,z); end;
	    'Q':begin readln(x,y); query(x,y); end;
	end;
    end;
    close(input); close(output);
end.


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值