#P6778. [Ynoi2009] rpdq

[Ynoi2009] rpdq

Problem Description

Given an undirected tree with nn nodes and weighted edges. Each node has a label, and the labels form a permutation of 1n1 \sim n.

There are mm queries. Each query gives l,rl, r. You need to find the sum of distances on the tree over all pairs of node labels (i,j)(i, j) that satisfy li<jrl \le i < j \le r. The distance between two nodes is defined as the sum of edge weights along the simple path connecting them.

Input Format

The first line contains two space-separated integers nn and mm.

The next n1n-1 lines each contain three space-separated integers uu vv dd, meaning there is an edge between uu and vv with weight dd.

The next mm lines each contain two space-separated integers ll rr, representing one query.

Output Format

Output mm lines. Each line is the answer for the corresponding query, taken modulo 2322^{32}.

6 6
2 1 1
5 1 1
3 1 3
4 5 1
6 3 3
2 5
1 5
1 4
3 6
2 6
1 1
19
26
18
28
44
0

Hint

Idea: nzhtl1477, Solution: nzhtl1477, Code: zx2003, Data: nzhtl1477.

For 100%100\% of the testdata, 1n,m,d21051 \le n, m, d \le 2 \cdot 10^5, and all values are integers.

Translated by ChatGPT 5