# bzoj3307: 雨天的尾巴

## 3307: 雨天的尾巴

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

## Description

N个点，形成一个树状结构。有M次发放，每次选择两个点x,y

## Output

  1 program j01;
2 const maxn=100086;
3 type xx=record l,r,mx,id:longint; end;
4      lsh=record id,w:longint; end;
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
23 begin
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;
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
146   for i:=1 to n-1 do
147   begin
149   end;
150   dfs(1,0);
151   getst;
152   for i:=1 to m do
153   begin
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  阅读(254)  评论(0编辑  收藏  举报