#P3241. [HNOI2015] 开店
[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 places in total, numbered from to , connected by weighted edges. Each place is inhabited by a youkai, and the age of the youkai at place is .
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 . Interests change naturally with age. For example, our -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 (a vertex id), and open a shop at targeting youkai whose ages are between and (that is, ages greater than or equal to and less than or equal to ).
It is possible that place is far from these youkai. Thus, Yuuka wants to know the sum of distances from all youkai with ages in to (the distance from a youkai to is the sum of edge weights along the path from its place to ). 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 , , and , representing the size of the tree, the number of shop plans (queries), and the upper bound of youkai ages.
- The second line contains space-separated integers . Here is the age of the youkai at place , satisfying . (Age can be , for example, a newborn youkai has age .)
- The next lines each contain three space-separated integers , , and , indicating that there is an edge of weight () between vertices and , where and are vertex ids.
- The next lines each contain three space-separated integers , , and .
For each of these lines, compute and from , , and . This specifies the query “open a shop at place , targeting youkai with ages in , and ask for the convenience value.”
- For the first query (the -st line), compute: , .
- For the -nd to the -th queries, suppose the convenience value obtained from the previous query is . Then compute: , .
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: , . For all testdata, .
Translated by ChatGPT 5
京公网安备 11011102002149号