#P17450. 同频回声 / Same Frequency Echo

同频回声 / Same Frequency Echo

Problem Description

You are given a weighted tree with nn nodes rooted at node 11.

Node ii has:

  • frequency band cic_i;
  • transmission time aia_i.

Each frequency band cc has an importance value wcw_c.

For two different nodes u,vu, v that use the same frequency band, define their synchronization cost as

D(u,v)=∣au−av∣+dist⁡(u,v),D(u,v)=|a_u-a_v|+\operatorname{dist}(u,v),

where dist⁡(u,v)\operatorname{dist}(u,v) is the sum of edge weights on the simple path between uu and vv.

Each query provides a node xx and a non-negative integer KK.

We say that frequency band cc produces an echo in the region governed by xx if and only if there exist two different nodes

u,v∈subtree⁡(x)u,v\in\operatorname{subtree}(x)

such that

cu=cv=c,D(u,v)≤K.c_u=c_v=c,\qquad D(u,v)\le K.

Here, subtree⁡(x)\operatorname{subtree}(x) denotes the subtree rooted at xx.

For each query, compute the sum of importance values of all frequency bands that produce an echo:

$$\sum_{c=1}^{m} w_c \bigl[\exists\,u\ne v\in\operatorname{subtree}(x),\ c_u=c_v=c,\ D(u,v)\le K\bigr].$$

The square brackets denote the Iverson bracket: it equals 11 if the condition holds, and 00 otherwise.

Input Format

The first line contains three integers n,m,qn,m,q (1≤n≤1061\le n\le 10^6, 1≤m≤1051\le m\le 10^5, 1≤q≤1061\le q\le 10^6), representing the number of nodes, the number of frequency bands, and the number of queries, respectively.

The second line contains mm integers w1,w2,…,wmw_1,w_2,\ldots,w_m (0≤wc≤1090\le w_c\le 10^9), representing the importance value of each frequency band.

The third line contains nn integers c1,c2,…,cnc_1,c_2,\ldots,c_n (1≤ci≤m1\le c_i\le m), representing the frequency band used by each node.

The fourth line contains nn integers a1,a2,…,ana_1,a_2,\ldots,a_n (0≤ai≤1090\le a_i\le 10^9), representing the transmission time of each node.

The next n−1n-1 lines each contain three integers u,v,du,v,d (1≤u,v≤n1\le u,v\le n, 0≤d≤1090\le d\le 10^9), indicating an undirected edge between nodes uu and vwithweightv with weight d$.

The next qq lines each contain two integers x,Kx,K (1≤x≤n1\le x\le n, 0≤K≤4×10180\le K\le 4\times 10^{18}), representing one query.

It is guaranteed that the edges form a tree, and that the answer fits in a signed 6464-bit integer.

Output Format

For each query, output one integer per line, representing the answer.

7 3 5
5 7 11
1 2 1 3 2 1 3
10 4 13 8 9 20 7
1 2 2
1 3 3
2 4 4
2 5 1
3 6 2
3 7 5
1 5
1 6
2 6
3 9
1 15
0
12
7
5
23

Hint

The synchronization costs between nodes with the same frequency are as follows:

  • Nodes on frequency band 11 are 1,3,61,3,6: D(1,3)=∣10−13∣+3=6D(1,3)=|10-13|+3=6, D(1,6)=∣10−20∣+5=15D(1,6)=|10-20|+5=15, D(3,6)=∣13−20∣+2=9D(3,6)=|13-20|+2=9.
  • Nodes on frequency band 22 are 2,52,5: D(2,5)=∣4−9∣+1=6D(2,5)=|4-9|+1=6.
  • Nodes on frequency band 33 are 4,74,7: D(4,7)=∣8−7∣+14=15D(4,7)=|8-7|+14=15.

Therefore:

  • In query (1,5)(1,5), no frequency band produces an echo, so the answer is 00.
  • In query (1,6)(1,6), frequency bands 1,21,2 produce echoes, so the answer is 5+7=125+7=12.
  • In query (2,6)(2,6), only frequency band 22 produces an echo, so the answer is 77.
  • In query (3,9)(3,9), only frequency band 11 produces an echo, so the answer is 55.
  • In query (1,15)(1,15), all three frequency bands produce echoes, so the answer is 5+7+11=235+7+11=23.

Translated by ChatGPT 5