#P15943. [JOI Final 2026] JOI 之旅 2 / JOI Tour 2

[JOI Final 2026] JOI 之旅 2 / JOI Tour 2

Problem Description

There are NN towns in the country of JOI, numbered from 11 to NN. Also, there are N1N-1 roads in the country of JOI, numbered from 11 to N1N-1. Road jj (1jN11 \leq j \leq N-1) connects town UjU_j and town VjV_j in both directions. It is possible to travel from any town to any other town by using some number of roads.

There is one shop in each town in the country of JOI. In the shop in town ii (1iN1 \leq i \leq N), a souvenir is sold for price AiA_i.

This year, MM tours are planned in the country of JOI. The kk-th tour (1kM1 \leq k \leq M) starts from town SkS_k and travels by roads to town TkT_k without visiting the same town twice. Note that the kk-th tour visits both towns SkS_k and TkT_k. It is guaranteed that SkTkS_k \neq T_k. Note that, from the structure of the country of JOI, the sequence of towns visited by a tour is uniquely determined.

You are planning to participate in one of these tours and buy one souvenir in each of exactly two of the towns visited on the tour. Moreover, you want to use up exactly the entire budget prepared for souvenirs, so for each of QQ candidate budgets, you decided to investigate in how many ways this can be done.

Given the roads in the country of JOI, the prices of the souvenirs, the information on the tours, and the candidate budgets B1,B2,,BQB_1, B_2, \dots, B_Q, write a program that computes the number of ways to choose a tour and the towns in which to buy souvenirs. More formally, for each qq (1qQ1 \leq q \leq Q), write a program that computes the number of triples of integers (k,u,v)(k, u, v) satisfying all of the following conditions.

  • 1kM1 \leq k \leq M.
  • 1u<vN1 \leq u < v \leq N.
  • The kk-th tour visits towns uu and vv.
  • Au+Av=BqA_u + A_v = B_q.

Input Format

Read the following data from the standard input.

NN
A1A2ANA_1 A_2 \cdots A_N
U1V1U_1 V_1
U2V2U_2 V_2
\vdots
UN1VN1U_{N-1} V_{N-1}
MM
S1T1S_1 T_1
S2T2S_2 T_2
\vdots
SMTMS_M T_M
QQ
B1B2BQB_1 B_2 \cdots B_Q

Output Format

Write QQ lines to the standard output. The qq-th line (1qQ1 \leq q \leq Q) should contain the number of ways to choose a tour and the towns in which to buy souvenirs so that the budget BqB_q is used up exactly.

8
1 2 3 2 1 2 3 2
2 3
7 8
4 3
1 2
7 3
2 5
6 1
4
1 4
1 6
2 5
3 8
7
1 2 3 4 5 6 16
0
0
4
2
4
1
0
8
8 2 3 6 1 4 1 7
1 2
2 3
3 4
4 5
5 6
6 7
7 8
1
1 8
5
2 4 5 10 15
1
2
3
3
1

Hint

Sample 1

First, the towns visited by each tour are as follows.

  • The 11st tour visits towns 1,2,3,41,2,3,4.
  • The 22nd tour visits towns 1,61,6.
  • The 33rd tour visits towns 2,52,5.
  • The 44th tour visits towns 3,7,83,7,8.

Represent a method of participating in the kk-th tour and buying souvenirs in towns uu and vv by (k,u,v)(k,u,v). Then, for each candidate budget, the ways to use up the budget exactly are as follows.

  • There are 00 ways to use up budget 11.
  • There are 00 ways to use up budget 22.
  • There are 44 ways to use up budget 33: (1,1,2)(1,1,2), (1,1,4)(1,1,4), (2,1,6)(2,1,6), (3,2,5)(3,2,5).
  • There are 22 ways to use up budget 44: (1,1,3)(1,1,3), (1,2,4)(1,2,4).
  • There are 44 ways to use up budget 55: (1,2,3)(1,2,3), (1,3,4)(1,3,4), (4,3,8)(4,3,8), (4,7,8)(4,7,8).
  • There is 11 way to use up budget 66: (4,3,7)(4,3,7).

This input example satisfies the constraints of subtasks 1,3,7,9,111,3,7,9,11.

Sample 2

This input example satisfies the constraints of subtasks 1,2,3,6,7,8,9,10,111,2,3,6,7,8,9,10,11.

Constraints

  • 2N1000002 \leq N \leq 100000.
  • 1AiN1 \leq A_i \leq N (1iN1 \leq i \leq N).
  • 1UjN1 \leq U_j \leq N (1jN11 \leq j \leq N-1).
  • 1VjN1 \leq V_j \leq N (1jN11 \leq j \leq N-1).
  • It is possible to travel from any town to any other town by using some number of roads.
  • 1M2000001 \leq M \leq 200000.
  • 1SkN1 \leq S_k \leq N (1kM1 \leq k \leq M).
  • 1TkN1 \leq T_k \leq N (1kM1 \leq k \leq M).
  • SkTkS_k \neq T_k (1kM1 \leq k \leq M).
  • 1Q20001 \leq Q \leq 2000.
  • 1B1<B2<<BQ2N1 \leq B_1 < B_2 < \cdots < B_Q \leq 2N.
  • All input values are integers.

Subtasks

  1. (3 points) N100,M100,Q100N \leq 100,M \leq 100,Q \leq 100.
  2. (4 points) N5000,Uj=jN \leq 5000,U_j = j (1jN11 \leq j \leq N-1), Vj=j+1V_j = j + 1 (1jN11 \leq j \leq N-1).
  3. (5 points) N5000N \leq 5000.
  4. (6 points) Q=1,Uj=jQ = 1,U_j = j (1jN11 \leq j \leq N-1), Vj=j+1V_j = j + 1 (1jN11 \leq j \leq N-1).
  5. (10 points) Q=1Q = 1.
  6. (7 points) M1000,Uj=jM \leq 1000,U_j = j (1jN11 \leq j \leq N-1), Vj=j+1V_j = j + 1 (1jN11 \leq j \leq N-1).
  7. (12 points) M1000M \leq 1000.
  8. (10 points) N50000,M50000,Uj=jN \leq 50000,M \leq 50000,U_j = j (1jN11 \leq j \leq N-1), Vj=j+1V_j = j + 1 (1jN11 \leq j \leq N-1).
  9. (15 points) N50000,M50000N \leq 50000,M \leq 50000.
  10. (11 points) Uj=jU_j = j (1jN11 \leq j \leq N-1), Vj=j+1V_j = j + 1 (1jN11 \leq j \leq N-1).
  11. (17 points) No additional constraints.