tolls

Time limit: 0.25s Memory limit: 256MB Input: Output:

In the country named Ivo, there are NN cities connected by Nβˆ’1N βˆ’ 1 bidirectional highways and you can get from any city to any other city using the highways. Each highway connects two different cities uiu_i and viv_i and it has a toll tax wiw_i. We will call "trip" a simple path (not containing duplicate cities) along the highways between two different cities uu and vv. 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.

Task

Ivaylo is responsible for the profits in the country. The government asked Ivaylo QQ questions for the sum of the costs of all the trips with costs in the interval [lj,rj][l_j, r_j], 1≀j≀Q1 \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. l1=1l_1 = 1 and r1=max⁑1≀i≀Nβˆ’1{wi}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, which calculates the answers to the questions.

Input data

The first line of the standard input contains two positive integers NN and QQ β€” the number of cities in the country Ivo and the number of questions given to Ivaylo. Each of the next Nβˆ’1N - 1 lines contains three positive integers uiu_i, viv_i, wiw_i, which describe a highway between the cities uiu_i and viv_i with toll wiw_i. Each of the rest QQ lines contains two positive integers ljl_j, rjr_j, which describe the questions given to Ivaylo.

Output data

For each question, in input order, output on a separate line the sum of the costs of the trips that are in the interval [lj,rj][l_j, r_j].

Constraints and clarifications

  • 1≀N,Q≀1051 \leq N, Q \leq 10^5
  • 1≀ui,vi≀Nβˆ’11 \leq u_i, v_i \leq N - 1
  • 1≀wi≀1091 \leq w_i ≀ 10^9
  • 1≀lj≀rj≀1091 \leq l_j \leq r_j \leq 10^9
# Score Constraints
1 0 Example
2 5 N,Q≀50N, Q \leq 50
3 5 Q=1Q = 1, N≀1Β 000N \leq 1 \ 000
4 10 Q≀100Q \leq 100, N≀1Β 000N \leq 1 \ 000
5 20 Q=1Q = 1
6 10 Q≀100Q \leq 100
7 20 lj=1l_j = 1
8 30 No additional constraints

Example

stdin

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

stdout

59
32
56
35
56

Explanation

Illustration of the cities and the highways:

The count of the trips with cost 11 is 33: 1βˆ’21 - 2, 1βˆ’41 - 4, 2βˆ’42 - 4.
The count of the trips with cost 22 is 44: 1βˆ’51 - 5, 2βˆ’52 - 5, 3βˆ’63 - 6, 4βˆ’54 - 5.
The count of the trips with cost 33 is 88: 1βˆ’31 - 3, 1βˆ’61 - 6, 2βˆ’32 - 3, 2βˆ’62 - 6, 3βˆ’43 - 4, 3βˆ’53 - 5, 4βˆ’64 - 6, 5βˆ’65 - 6.
The count of the trips with cost 44 is 66: 1βˆ’71 - 7, 2βˆ’72 - 7, 3βˆ’73 - 7, 4βˆ’74 - 7, 5βˆ’75 - 7, 6βˆ’76 - 7.
The count of the trips with cost 55 is 00.

The answer to the first question is: 3β‹…1+4β‹…2+8β‹…3+6β‹…4=593 \cdot 1 + 4 \cdot 2 + 8 \cdot 3 + 6 \cdot 4 = 59.
The answer to the second question is: 4β‹…2+8β‹…3=324 \cdot 2 + 8 \cdot 3 = 32.
The answer to the third question is: 4β‹…2+8β‹…3+6β‹…4=564 \cdot 2 + 8 \cdot 3 + 6 \cdot 4 = 56.
The answer to the fourth question is: 3β‹…1+4β‹…2+8β‹…3=353 \cdot 1 + 4 \cdot 2 + 8 \cdot 3 = 35.
The answer to the fifth question is: 4β‹…2+8β‹…3+6β‹…4+0β‹…5=564 \cdot 2 + 8 \cdot 3 + 6 \cdot 4 + 0 \cdot 5 = 56.

Log in or sign up to be able to send submissions!