#P3241. [HNOI2015] 开店

    ID: 2290 远端评测题 6000ms 500MiB 尝试: 0 已通过: 0 难度: 8 上传者: 标签>2015湖南分治最近公共祖先,LCA动态树

[HNOI2015] 开店

Description

Kazami Yuuka has a good friend named Yakumo Yukari. They often watch the stars and the moon together, chatting from poetry and songs to life philosophy. Recently, they had a sudden idea to open a small shop in Gensokyo to do business and make some money.

This idea is certainly great, but they also found a problem: where to open the shop and which group of customers to target. Interestingly, the map of Gensokyo is a tree. There are nn places in total, numbered from 11 to nn, connected by n1n-1 weighted edges. Each place is inhabited by a youkai, and the age of the youkai at place ii is xix_i.

Youkai are relatively quiet, so they do not want to be adjacent to too many youkai. Therefore, the degree of every vertex in this tree is at most 33. Interests change naturally with age. For example, our 1818-year-old girls Yuuka and Yukari may prefer cute things. Yuuka discovered through research that a youkai’s interests basically depend only on age. So Yuuka plans to choose a place uu (a vertex id), and open a shop at uu targeting youkai whose ages are between LL and RR (that is, ages greater than or equal to LL and less than or equal to RR).

It is possible that place uu is far from these youkai. Thus, Yuuka wants to know the sum of distances from all youkai with ages in [L,R][L, R] to uu (the distance from a youkai to uu is the sum of edge weights along the path from its place to uu). Yuuka calls this the convenience value of the shop plan.

They have not yet decided where to open the shop, and Yukari has prepared many plans. For each plan, what is the convenience value?

Input Format

  • The first line contains three space-separated integers nn, QQ, and AA, representing the size of the tree, the number of shop plans (queries), and the upper bound of youkai ages.
  • The second line contains nn space-separated integers x1,x2,,xnx_1, x_2, \ldots, x_n. Here xix_i is the age of the youkai at place ii, satisfying 0xi<A0 \le x_i \lt A. (Age can be 00, for example, a newborn youkai has age 00.)
  • The next n1n-1 lines each contain three space-separated integers aa, bb, and cc, indicating that there is an edge of weight cc (1c10001 \le c \le 1000) between vertices aa and bb, where aa and bb are vertex ids.
  • The next QQ lines each contain three space-separated integers uu, aa, and bb.

For each of these QQ lines, compute LL and RR from aa, bb, and AA. This specifies the query “open a shop at place uu, targeting youkai with ages in [L,R][L, R], and ask for the convenience value.”

  • For the first query (the 11-st line), compute: L=min(amodA,bmodA)L=\min(a \bmod A, b \bmod A), R=max(amodA,bmodA)R=\max(a \bmod A, b \bmod A).
  • For the 22-nd to the QQ-th queries, suppose the convenience value obtained from the previous query is ansans. Then compute: L=min((a+ans)modA,(b+ans)modA)L=\min((a+ans) \bmod A, (b+ans) \bmod A), R=max((a+ans)modA,(b+ans)modA)R=\max((a+ans) \bmod A, (b+ans) \bmod A).

Output Format

For each plan, output one line containing the convenience value.

10 10 10
0 0 7 2 1 4 7 7 7 9
1 2 270
2 3 217
1 4 326
2 5 361
4 6 116
3 7 38
1 8 800
6 9 210
7 10 278
8 9 8
2 8 0
9 3 1
8 0 8
4 2 7
9 7 3
4 7 0
2 2 7
3 2 1
2 3 4
1603 
957 
7161 
9466 
3232 
5223 
1879 
1669 
1282 
0

Hint

Constraints: n1.5×105n \le 1.5 \times 10^5, Q2×105Q \le 2 \times 10^5. For all testdata, A109A \le 10^9.

Translated by ChatGPT 5