Under construction

Still being built. Things may change

International Moonrabbit Organization

Time limit4sMemory limit1024 MB

Summary
Given a weighted tree, for each query (L, R, V) report the minimum, maximum, and sum of distances from base V to bases L through R.
Level

Hard8 of 10

Topics
Tree, Divide and conquer, Binary search, Prefix sum
Solved
No attempts yet

Problem

Oktokki2, a bear living on the Moon, is the head of the venue reservation office at the International Moonrabbit Organization (IMO). So far, NN countries have each built one base on the Moon. The countries are numbered 1 through NN. To trade on the Moon, the bears built bidirectional roads, each connecting two bases. To save costs, only N−1N-1 roads were built, and using only these roads, one can travel between any two bases. The roads are numbered 1 through N−1N-1. Road ii connects the base of country SiS_i and the base of country EiE_i, and walking along it takes DiD_i seconds.

To celebrate the 100th anniversary of the IMO, meetings will be held every day for one year, starting on January 1, 2020. Year 2020 has QQ days. The meeting on day ii is held at the base of country ViV_i, and one bear from each base of countries LiL_i through RiR_i attends. To book the venue, Oktokki2 needs to know how many seconds after the start of day ii the bears arrive at the meeting. Every bear is diligent. Each bear leaves as soon as the day starts, walks to the venue along a shortest path, and returns to its original base immediately after the meeting. On each meeting day, Oktokki2 wants the arrival time of the earliest bear, the arrival time of the latest bear, and the sum of the arrival times of all bears attending.

Input

The first line contains NN and QQ. (1≤N,Q≤1051 \le N, Q \le 10^5)

Each of the next N−1N-1 lines contains SiS_i, EiE_i, and DiD_i for road ii. (1≤Si,Ei≤N1 \le S_i, E_i \le N, 1≤Di≤1081 \le D_i \le 10^8)

It is guaranteed that any two bases can be reached from each other using only the given roads.

Each of the next QQ lines contains LiL_i, RiR_i, and ViV_i for the meeting on day ii. (1≤Li≤Ri≤N1 \le L_i \le R_i \le N, 1≤Vi≤N1 \le V_i \le N)

Output

For the meeting on day ii, let min⁡i\min_i be the arrival time in seconds of the earliest bear, max⁡i\max_i the arrival time in seconds of the latest bear, and sumi\text{sum}_i the sum in seconds of the arrival times of all attending bears. On line ii, print min⁡i\min_i, max⁡i\max_i, and sumi\text{sum}_i.

Examples2

  1. Example 1

    Input
    4 5
    4 3 34
    1 2 13
    2 4 56
    1 4 1
    1 3 3
    1 4 4
    1 2 3
    3 4 1
    
    Expected output
    0 103 185
    0 103 193
    0 69 159
    90 103 193
    69 103 172
    
  2. Example 2

    Input
    10 10
    10 1 54
    2 5 23
    4 10 2
    2 8 100
    6 3 44
    7 6 35
    9 5 18
    10 5 100
    5 3 72
    2 7 2
    4 10 1
    1 1 2
    1 10 2
    1 5 9
    1 3 8
    1 8 9
    6 7 2
    1 6 7
    8 8 7
    
    Expected output
    0 174 556
    54 305 1288
    177 177 177
    0 177 997
    18 172 441
    100 277 572
    18 172 885
    139 174 313
    35 305 997
    274 274 274