1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
const int N=4e5+10;
int n,m;
int a[N];
vector<pii>e[N],qs[N];
int dep[N];
ll f[N],dp[N];
int st[22][1111111],fa[N],tim,bg[N];
ll ans[N];
int Lg[1111111];
vector<ll>dv[N];
void dfs1(int u)
{
bg[u]=++tim;
st[0][tim]=u;
f[u]+=a[u];
int v;
for(auto qwq:e[u])
{
v=qwq.fi;
if(v==fa[u]) continue;
fa[v]=u;
dep[v]=dep[u]+1;
dfs1(v);
f[u]+=max(0ll,f[v]-2ll*qwq.se);
st[0][++tim]=u;
}
}
void dfs2(int u)
{
dp[u]=a[u];
int v;
ll cur;
for(auto qwq:e[u])
{
v=qwq.fi;
cur=max(0ll,f[v]-2ll*qwq.se);
dp[u]+=cur;
dv[u].pb(cur);
}
cur=f[u];
for(int i=0;i<(int)e[u].size();i++)
{
v=e[u][i].fi;
if(fa[u]==v) continue;
f[u]=dp[u]-dv[u][i];
dfs2(v);
}
f[u]=cur;
}
inline int get_lp(int x,int y) {return dep[x]<dep[y]?x:y;}
inline void init_ST()
{
R(i,2,tim+5) Lg[i]=Lg[i>>1]+1;
for(int i=1;(1<<i)<=tim;i++)
{
int w=(1<<i);
R(j,1,tim-w+1) st[i][j]=get_lp(st[i-1][j],st[i-1][j+(w>>1)]);
}
}
inline int get_LCA(int x,int y)
{
x=bg[x],y=bg[y];
if(x>y) Swap(x,y);
int i=Lg[y-x+1],w=(1<<i);
return get_lp(st[i][x],st[i][y-w+1]);
}
inline ll get_dv(int u)
{
int f=fa[u];
int pos=(lower_bound(e[u].begin(),e[u].end(),pii(f,-1))-e[u].begin());
if(pos>=(int)e[u].size()||e[u][pos].fi!=f) return 0;
return dv[u][pos];
}
ll BIT[N];
inline int lowbit(int x) {return x&(-x);}
inline void modify(int pos,ll val)
{
for(int i=pos;i<N;i+=lowbit(i)) BIT[i]+=val;
}
inline ll query(int pos)
{
ll ret=0;
for(int i=pos;i;i-=lowbit(i)) ret+=BIT[i];return ret;
}
inline ll query_SUM(int l,int r) {return query(r)-query(l-1);}
void dfs3(int u)
{
ll vadd=dp[u]-get_dv(u);
modify(dep[u],vadd);
int v;
ll cur;
for(auto qwq:qs[u])
{
v=qwq.fi;
ans[qwq.se]+=query_SUM(dep[v],dep[u])+get_dv(v);
}
for(int i=0;i<(int)e[u].size();i++)
{
v=e[u][i].fi;
if(fa[u]==v) continue;
cur=dv[u][i]+e[u][i].se;
modify(dep[u],-cur);
dfs3(v);
modify(dep[u],cur);
}
modify(dep[u],-vadd);
}
signed main()
{
n=read(),m=read();
R(i,1,n) a[i]=read();
int u,v;
ll d;
R(i,2,n)
{
u=read(),v=read(),d=read();
e[u].pb(mkp(v,d));
e[v].pb(mkp(u,d));
}
R(i,1,n) sort(e[i].begin(),e[i].end());
dfs1(1);
R(i,1,n) dep[i]++;
init_ST();
dfs2(1);
R(i,1,m)
{
u=read(),v=read();
int L_A=get_LCA(u,v);
ans[i]=-dp[L_A];
qs[u].pb(mkp(L_A,i));
qs[v].pb(mkp(L_A,i));
}
dfs3(1);
R(i,1,m) writeln(ans[i]);
}
|