TOLLS

시간 제한0.25초메모리 제한1024 MB

요약
가중치가 있는 트리에서 각 질의 [l, r]마다 최대 간선 가중치가 [l, r]에 속하는 모든 단순 경로의 최대 간선 가중치 합을 구한다.
난이도

어려움10점 중 8점

유형
트리, DFS, 유니온 파인드, 정렬
정답자
아직 제출이 없습니다

문제

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 u_iu\_i and v_iv\_i and it has a toll tax w_iw\_i, 1≤i≤N−11 \leq i \leq N-1. 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.

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 \[l_j,r_j]\[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. l_1=1l\_1=1 and r_1=max_1≤i≤N−1w_ir\_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 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 u_i,v_i,w_iu\_i, v\_i, w\_i, which describe a highway between the cities u_iu\_i and v_iv\_i with toll w_iw\_i. Each of the rest QQ lines contains two positive integers l_j,r_jl\_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]\[l\_j, r\_j].

제한

  • 1≤N,Q≤1051 \leq N, Q \leq 10^5
  • 1≤u_i,v_i≤N−11 \leq u\_i, v\_i \leq N-1
  • 1≤w_i≤1091 \leq w\_i \leq 10^9
  • 1≤l_j≤r_j≤1091 \leq l\_j \leq r\_j \leq 10^9

힌트

Illustration of the cities and the highways:

  • The count of the trips with cost 11 is 33: 1−2,1−4,2−41-2,1-4,2-4.
  • The count of the trips with cost 22 is 44: 1−5,2−5,3−6,4−51-5,2-5,3-6,4-5.
  • The count of the trips with cost 33 is 88: 1−3,1−6,2−3,2−6,3−4,3−5,4−6,5−61-3,1-6,2-3,2-6,3-4,3-5,4-6,5-6.
  • The count of the trips with cost 44 is 66: 1−7,2−7,3−7,4−7,5−7,6−71-7,2-7,3-7,4-7,5-7,6-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 \times 1+4 \times 2+8 \times 3+6 \times 4=59.
  • The answer to the second question is: 4×2+8×3=324 \times 2+8 \times 3=32.
  • The answer to the third question is: 4×2+8×3+6×4=564 \times 2+8 \times 3+6 \times 4=56.
  • The answer to the fourth question is: 3×1+4×2+8×3=353 \times 1+4 \times 2+8 \times 3=35.
  • The answer to the fifth question is: 4×2+8×3+6×4+0×5=564 \times 2+8 \times 3+6 \times 4+0 \times 5=56.

예제1

  1. 예제 1

    입력
    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