Under construction

Still being built. Things may change

Regular Meeting

Time limit2sMemory limit512 MB

Summary
Given a weighted tree, for each query range of vertices S to E, find the meeting vertex that minimizes the largest bottleneck distance to the people in that range.
Level

Medium7 of 10

Topics
Tree, Union-find, Segment tree
Solved
No attempts yet

Problem

A tree with NN vertices is given. Each vertex is numbered from 1 to NN in order. The ii-th edge connects vertex AiA_i and vertex BiB_i, and its weight is CiC_i (1≤i<N1 \le i < N).

The distance between two vertices in the tree is defined as the maximum edge weight on the shortest path connecting them. The distance between the same two vertices is defined as 0.

People living in the tree plan to hold QQ meetings. The ii-th meeting is attended by people living at vertices numbered from SiS_i to EiE_i, inclusive. Each meeting takes place at some vertex vv in the tree. Choose vv so that the maximum distance from vv to any attendee is as small as possible.

Write a program that finds this minimum value.

Input

The first line contains two integers NN and QQ, separated by a space. The next N−1N-1 lines describe the edges. Line i+1i+1 contains three integers AiA_i, BiB_i, and CiC_i, separated by spaces, which means there is an edge of weight CiC_i between vertex AiA_i and vertex BiB_i.

The next QQ lines describe the meetings. Line N+iN+i contains two integers SiS_i and EiE_i, separated by a space, which describe the ii-th meeting.

Output

Print QQ lines. The ii-th line contains the answer for the ii-th meeting.

Limits

  • 1≤N≤300,0001 \le N \le 300,000
  • 1≤Q≤300,0001 \le Q \le 300,000
  • 1≤Ai≤N1 \le A_i \le N (1≤i<N1 \le i < N)
  • 1≤Bi≤N1 \le B_i \le N (1≤i<N1 \le i < N)
  • Ai≠BiA_i \ne B_i (1≤i<N1 \le i < N)
  • 1≤Ci≤1091 \le C_i \le 10^9 (1≤i<N1 \le i < N)
  • 1≤Si≤Ei≤N1 \le S_i \le E_i \le N (1≤i≤Q1 \le i \le Q)

Examples1

  1. Example 1

    Input
    3 3
    1 2 1
    2 3 2
    1 2
    2 3
    1 3
    
    Expected output
    1
    2
    2