# [bzoj1767][Ceoi2009]harbingers (树上斜率优化）

Input
N 以下N-1行A,B,C三个数表示A,B之间有一条长为C的边。 再N行每行两数Wi,Vi 输出有一行N-1个数表示如题所述。

Output
Sample Input
5
1 2 20
2 3 12
2 4 1
4 5 3
26 9
1 10
500 2
2 30

Sample Output

206 321 542 328

solution：

树上斜率优化，设dep[j]>dep[k]，当前询问的点为i，若j比k优，

CODE：

 1 # include <iostream>
2 # include <cstdio>
3 # include <cstring>
4 using namespace std;
5 const int N = 3e5 + 12;
6 typedef long long LL;
8 LL f[N],d[N],v[N],w[N];
9 struct Edge
10 {
11     int to,nex;
12     LL w;
13 } edge[N << 1];
14 void AddEdge(int u,int v,LL w)
15 {
16     edge[++dt] = (Edge)
17     {
19     };
21 }
22 LL x(int i)
23 {
24     return d[i];
25 }
26 LL y(int i)
27 {
28     return f[i];
29 }
30 LL Get(int A,int B)
31 {
32     return f[A] + (d[B] - d[A]) * v[B] + w[B];
33 }
34 double slope(int A,int B)
35 {
36     return ((double)(y(B) - y(A))) / ((double)(x(B) - x(A)));
37 }
38 bool Cross(int A,int B,int C)
39 {
40     return slope(B,C) <= slope(A,B);
41 }
42 int find(int x,int tp)
43 {
44     int l=1,r=tp-1,ret=tp,mid;
45     // 注意ret初始值
46     while(l <= r)
47     {
48         mid = l + r >> 1;
49         if(slope(que[mid],que[mid+1]) > (double)v[x]) ret=mid,r=mid-1;
50         else l = mid + 1;
51     }
52     return ret;
53 }
54
55 int Find(int z,int tp)
56 {
57     int l=0,r=tp-1,ret=tp+1;
58     // 注意ret初始值
59     while(l<=r)
60     {
61         int mid =l+r>>1;
62         if(slope(que[mid],z)<=slope(que[mid],que[mid+1]))ret=mid+1,r=mid-1;
63         else l=mid+1;
64     }
65     return ret;
66 }
67
68 void dfs(int u,int pos,int fa)
69 {
70     int qpos,qtop;
71     f[u] = Get(que[find(u,pos)],u);
72     qpos = Find(u,pos);
73     qtop = que[qpos];
74     que[qpos] = u;
75     for(int i = head[u]; i; i = edge[i].nex)
76     {
77         if(edge[i].to == fa)continue;
78         d[edge[i].to] = d[u] + edge[i].w;
79         dfs(edge[i].to,qpos,u);
80     }
81     que[qpos] = qtop;
82 }
83 int main()
84 {
85     scanf("%d",&n);
86     int x,y,z;
87     for(int i = 1; i < n; i++)
88     {
89         scanf("%d %d %d",&x,&y,&z);
97 }