bzoj3307: 雨天的尾巴

3307: 雨天的尾巴

Time Limit: 10 Sec  Memory Limit: 128 MB
Submit: 479  Solved: 214
[Submit][Status][Discuss]

Description

N个点,形成一个树状结构。有M次发放,每次选择两个点x,y
对于x到y的路径上(含x,y)每个点发一袋Z类型的物品。完成
所有发放后,每个点存放最多的是哪种物品。

Input

第一行数字N,M
接下来N-1行,每行两个数字a,b,表示a与b间有一条边
再接下来M行,每行三个数字x,y,z.如题

Output

 

输出有N行
每i行的数字表示第i个点存放最多的物品是哪一种,如果有
多种物品的数量一样,输出编号最小的。如果某个点没有物品
则输出0

 

题解

这题有点奥妙重重啊……我数组开大了不到十分之一就卡着内存过了

考虑每种颜色,我们可以树上差分,x,y处+1,xy的lca处-1,lca的father-1,然后子树和就是颜色在这个点的数量。然后我们用一个线段树来维护每个节点所有的颜色的值(这个用差分做)和他们的最大值,然后从底向上一路线段树合并上去就行了

  1 program j01;
  2 const maxn=100086;
  3 type xx=record l,r,mx,id:longint; end;
  4      lsh=record id,w:longint; end;
  5 var head:array[0..maxn]of longint;
  6     q,next:array[0..2*maxn]of longint;
  7     t:array[0..65*maxn]of xx;
  8     st:array[0..20,0..2*maxn]of longint;
  9     dep,fir,dfn,fa,root:array[0..maxn]of longint;
 10     tot,dfnt,tt,n,m,u,v,w,cnt,lc,p,i,mx:longint;
 11     op:array[0..maxn]of record u,v,w:longint;end;
 12     ls:array[0..maxn]of lsh;
 13     bin,ps:array[0..maxn]of longint;
 14     ans:array[0..maxn]of longint;
 15 
 16 procedure swap(var a,b:longint);
 17 var c:longint;
 18 begin
 19   c:=a;a:=b;b:=c;
 20 end;
 21 
 22 procedure add(u,v:longint);
 23 begin
 24   inc(tt);q[tt]:=v;next[tt]:=head[u];head[u]:=tt;
 25 end;
 26 
 27 procedure dfs(i,pre:longint);
 28 var j:longint;
 29 begin
 30   inc(tot);st[0,tot]:=i;fir[i]:=tot;
 31   inc(dfnt);dfn[dfnt]:=i;
 32   j:=head[i];
 33   while j>0 do
 34   begin
 35     if(q[j]<>pre)then
 36     begin
 37       dep[q[j]]:=dep[i]+1;fa[q[j]]:=i;
 38       dfs(q[j],i);
 39       inc(tot);st[0,tot]:=i;
 40     end;
 41     j:=next[j];
 42   end;
 43 end;
 44 
 45 function min_st(a,b:longint):longint;
 46 begin
 47   if dep[a]<dep[b] then exit(a) else exit(b);
 48 end;
 49 
 50 procedure getst;
 51 var i,j:longint;
 52 begin
 53   bin[1]:=0;
 54   for i:=2 to tot do
 55     if i and(i-1)=0 then bin[i]:=bin[i-1]+1 else bin[i]:=bin[i-1];
 56   for i:=1 to bin[tot] do
 57     for j:=1 to tot+1-(1 shl i) do
 58       st[i,j]:=min_st(st[i-1,j],st[i-1,j+(1 shl(i-1))]);
 59 end;
 60 
 61 function lca(u,v:longint):longint;
 62 var k:longint;
 63 begin
 64   if u=v then exit(u);
 65   u:=fir[u];v:=fir[v];
 66   if u>v then swap(u,v);k:=bin[v-u];
 67   exit(min_st(st[k,u],st[k,v+1-(1 shl k)]));
 68 end;
 69 
 70 procedure update(i:longint);
 71 begin
 72   if t[t[i].l].mx>=t[t[i].r].mx then
 73   begin
 74     t[i].mx:=t[t[i].l].mx;t[i].id:=t[t[i].l].id;
 75   end else
 76   begin
 77     t[i].mx:=t[t[i].r].mx;t[i].id:=t[t[i].r].id;
 78   end;
 79   if t[i].mx=0 then t[i].id:=0;
 80 end;
 81 
 82 procedure ins(var i:longint;l,r,ps,dd:longint);
 83 var mid:longint;
 84 begin
 85   if i=0 then
 86   begin
 87     inc(cnt);i:=cnt;
 88   end;
 89   if l=r then
 90   begin
 91     inc(t[i].mx,dd);t[i].id:=l;exit;
 92   end;
 93   mid:=(l+r)div 2;
 94   if ps<=mid then ins(t[i].l,l,mid,ps,dd) else ins(t[i].r,mid+1,r,ps,dd);
 95   update(i);
 96 end;
 97 
 98 function merge(x,y,l,r:longint):longint;
 99 var mid:Longint;
100 begin
101   if(x=0)or(y=0)then exit(x+y);
102   if l=r then
103   begin
104     inc(t[x].mx,t[y].mx);t[x].id:=l;exit(x);
105   end;
106   mid:=(l+r)div 2;
107   t[x].l:=merge(t[x].l,t[y].l,l,mid);
108   t[x].r:=merge(t[x].r,t[y].r,mid+1,r);
109   update(x);exit(x);
110 end;
111 
112 procedure qsort(l,r:longint);
113 var i,j,x:longint;y:lsh;
114 begin
115   i:=l;j:=r;x:=ls[(i+j)div 2].w;
116   repeat
117     while ls[i].w<x do inc(i);
118     while x<ls[j].w do dec(j);
119     if i<=j then
120     begin
121       y:=ls[i];ls[i]:=ls[j];ls[j]:=y;
122       inc(i);dec(j);
123     end;
124   until i>j;
125   if i<r then qsort(i,r);
126   if l<j then qsort(l,j);
127 end;
128 
129 procedure disc;
130 var i:longint;
131 begin
132   qsort(1,m);
133   op[ls[1].id].w:=1;mx:=1;
134   ps[1]:=ls[1].w;
135   for i:=2 to m do
136   begin
137     if ls[i].w<>ls[i-1].w then inc(mx);
138     op[ls[i].id].w:=mx;
139     ps[mx]:=ls[i].w;
140   end;
141 end;
142 
143 begin
144   readln(n,m);
145   fillchar(head,sizeof(head),0);tt:=0;
146   for i:=1 to n-1 do
147   begin
148     readln(u,v);add(u,v);add(v,u);
149   end;
150   dfs(1,0);
151   getst;
152   for i:=1 to m do
153   begin
154     readln(op[i].u,op[i].v,op[i].w);
155     ls[i].w:=op[i].w;ls[i].id:=i;
156   end;
157   disc;
158   for i:=1 to m do
159   begin
160     u:=op[i].u;v:=op[i].v;w:=op[i].w;
161     lc:=lca(u,v);
162     ins(root[u],1,mx,w,1);
163     ins(root[v],1,mx,w,1);
164     ins(root[lc],1,mx,w,-1);
165     if fa[lc]<>0 then ins(root[fa[lc]],1,mx,w,-1);
166   end;
167   for i:=n downto 1 do
168   begin
169     p:=dfn[i];
170     ans[p]:=ps[t[root[p]].id];
171     if fa[p]>0 then root[fa[p]]:=merge(root[fa[p]],root[p],1,mx);
172   end;
173   for i:=1 to n do writeln(ans[i]);
174 end.

 

posted @ 2017-04-03 22:13  OldJang  阅读(239)  评论(0编辑  收藏  举报