F.A.Q
Hand In Hand
Online Acmers
Problem Archive
Realtime Judge Status
Authors Ranklist
 
     C/C++/Java Exams     
ACM Steps
Go to Job
Contest LiveCast
ICPC@China
Best Coder beta
VIP | STD Contests
    DIY | Web-DIY beta
Author ID 
Password 
 Register new ID

Travel on Tree

Time Limit: 60000/30000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)
Total Submission(s): 202    Accepted Submission(s): 34


Problem Description
In the world of $\textit{The Three-Body Problem}$, about 200 years later, people will live in huge underground tree buildings. In this problem, we use the tree data structure to describe tree buildings.

There is a tree with $n$ nodes and $n - 1$ edges with length, and $n$ of your friends live in the tree, one at each node. You are planning to visit your friends in the next $m$ days. Each day you choose an interval $[l, r]$ and plan to visit friends living in the nodes numbered from $l$ to $r$. You can choose an arbitrary node $u$ to start the day's visit, then travel on the tree along the edges, and finally go back to $u$. During the travel, you should visit all your friends living in the nodes numbered from $l$ to $r$. You can visit these friends $\textbf{in any order}$ and you can pass a node without visiting the friend. Please calculate the minimum total distance of the travel for each day.
 

Input
The first line of the input contains an integer $T \ (1 \leq T \leq 2 \times 10^4)$ --- the number of test cases.

The first line of each test case contains two integers $n, m \ (1 \leq n, m \leq 10^5)$ --- the number of nodes and the number of days.

Each of the following $n - 1$ lines of each test case contains three integers $u, v, w \ (1 \leq u, v \leq n, 1 \leq w \leq 10^4)$ --- an edge connecting nodes $u$ and $v$ with length $w$.

Each of the following $m$ lines of each test case contains two integers $l, r \ (1 \leq l \leq r \leq n)$ --- a plan to visit your friends living in the nodes numbered from $l$ to $r$.

It is guaranteed that for all test cases, $\sum n \leq 10^6, \ \sum m \leq 10^6$.
 

Output
For each day's plan, output the answer in a single line.
 

Sample Input
2 3 3 1 2 1 2 3 1 1 1 1 2 1 3 5 5 1 2 1 1 3 2 3 4 3 3 5 4 1 3 1 4 1 5 2 5 3 5
 

Sample Output
0 2 4 6 12 20 20 14
 

Source
 

Statistic | Submit | Discuss | Note
Hangzhou Dianzi University Online Judge 3.0
Copyright © 2005-2024 HDU ACM Team. All Rights Reserved.
Designer & Developer : Wang Rongtao LinLe GaoJie GanLu
Total 0.000000(s) query 1, Server time : 2024-09-30 11:04:09, Gzip enabled