| 시간 제한 | 메모리 제한 | 제출 | 정답 | 맞힌 사람 | 정답 비율 |
|---|---|---|---|---|---|
| 0.25 초 | 1024 MB | 34 | 16 | 16 | 47.059% |
In the country named Ivo, there are $N$ cities connected by $N-1$ bidirectional highways and you can get from any city to any other city using the highways. Each highway connects two different cities $u_i$ and $v_i$ and it has a toll tax $w_i,ドル 1ドル \leq i \leq N-1$. We will call "trip" a simple path (not containing duplicate cities) along the highways between two different cities $u$ and $v$. The costs for the trips in the country Ivo have been reduced and instead of paying the sum of the tolls along the trip, only one toll is paid, which is a maximal toll for a highway along the trip.
Ivaylo is responsible for the profits in the country. The government asked Ivaylo $Q$ questions for the sum of the costs of all the trips with costs in the interval $[l_j, r_j],ドル 1ドル \leq j \leq Q$. It is guaranteed that the first question is for the sum of the costs of the trips between every two different cities, i.e. $l_1=1$ and $r_1=max_{1 \leq i \leq N-1}\{w_i\}$. Ivaylo cannot handle this task, calculating by hand, and because he cannot work with computers he requires that you write a program tolls, which calculates the answers to the questions.
The first line of the standard input contains two positive integers $N$ and $Q$ -- the number of cities in the country Ivo and the number of questions given to Ivaylo. Each of the next $N-1$ lines contains three positive integers $u_i, v_i, w_i,ドル which describe a highway between the cities $u_i$ and $v_i$ with toll $w_i$. Each of the rest $Q$ lines contains two positive integers $l_j, r_j,ドル which describe the questions given to Ivaylo.
For each question, in input order, output on a separate line the sum of the costs of the trips that are in the interval $[l_j, r_j]$.
| Subtasks | Points | Required subtasks | $N$ | $Q$ | Other constraints |
|---|---|---|---|---|---|
| 1 | 5 | $\leq 50$ | $\leq 50$ | ||
| 2 | 5 | $\leq 10^3$ | $=1$ | ||
| 3 | 10 | 1, 2 | $\leq 10^3$ | $\leq 100$ | |
| 4 | 20 | 2 | $\leq 10^5$ | $=1$ | |
| 5 | 10 | 1, 2, 3, 4 | $\leq 10^5$ | $\leq 100$ | |
| 6 | 20 | $\leq 10^5$ | $\leq 10^5$ | $l_j=1, 1 \leq j \leq Q$ | |
| 7 | 30 | 1, 2, 3, 4, 5, 6 | $\leq 10^5$ | $\leq 10^5$ |
7 5 1 2 1 3 1 3 1 4 1 4 5 2 5 7 4 3 6 2 1 4 2 3 2 4 1 3 2 5
59 32 56 35 56
Illustration of the cities and the highways: