APIO2012-Dispatching[左偏树]

来源:互联网 发布:zerohedge怎么样 知乎 编辑:程序博客网 时间:2024/05/16 12:29

如果会左偏树的话,这题就是个模版题。可惜比赛时侯不会左偏树,做排序链表的了90.

program dispatching;typelink=^node;node=record           key,height,size:int64;           sum:int64;           ch:array[0..1] of link;end;varn,m,ans:int64;B,C,L:array[1..100000] of int64;tree:array[1..100000] of link;i:longint;function Ht(r:link):int64;begin     if r=nil then Exit(-1) else Exit(r^.height);end;function Sz(r:link):int64;begin     if r=nil then Exit(0) else Exit(r^.size);end;function Sm(r:link):int64;begin     if r=nil then Exit(0) else Exit(r^.sum);end;procedure lnkSwp(var a,b:link);var tmp:link;begin     tmp:=a; a:=b; b:=tmp;end;function Merge(a,b:link):link;begin     if a=nil then Exit(b);     if b=nil then Exit(a);     if a^.key<b^.key then lnkSwp(a,b);     a^.ch[1]:=Merge(a^.ch[1],b);     if Ht(a^.ch[0])<Ht(a^.ch[1]) then lnkSwp(a^.ch[0],a^.ch[1]);     a^.height:=Ht(a^.ch[1])+1;     a^.size:=Sz(a^.ch[0])+1+Sz(a^.ch[1]);     a^.sum:=Sm(a^.ch[0])+a^.key+Sm(a^.ch[1]);     Exit(a);end;function lnkNew(key:int64):link;var p:link;begin     new(p);     p^.ch[0]:=nil;     p^.ch[1]:=nil;     p^.key:=key;     p^.sum:=key;     p^.size:=1;     p^.height:=0;     Exit(p);end;function Max(a,b:int64):int64;begin     if a>b then Exit(a) else Exit(b);end;begin      assign(input,'dispatching.in'); reset(input);     assign(output,'dispatching.out'); rewrite(output);     readln(n,m);     for i:=1 to n do begin         readln(B[i],C[i],L[i]);         tree[i]:=lnkNew(C[i]);     end;     ans:=0;     for i:=n downto 1 do begin         while Sm(tree[i])>m do tree[i]:=Merge(tree[i]^.ch[0],tree[i]^.ch[1]);         ans:=Max(ans,Sz(tree[i])*L[i]);         if B[i]<>0 then tree[B[i]]:=Merge(tree[B[i]],tree[i]);     end;     write(ans);     close(input); close(output);end.


 

AC代码:

 

原创粉丝点击