Intellectual Prefix Maxima
Time limit4sMemory limit512 MB
For each query on a weighted tree, compute the sum of prefix maxima of the edge weights along the path from u to v.
- Level
Hard9 of 10
- Topics
- Tree, Divide and conquer, Binary search
- Solved
- No attempts yet
Problem
A tree is an undirected graph in which any two vertices are connected by exactly one path. You are given a weighted tree with vertices, where is the weight of the edge between vertices and . Consider a simple path from vertex to vertex . Denote the sequence of edge weights on path by , where , , , .
Let be the sum of prefix maxima of . You are given queries, each described by two integers and . For each query, compute .
Input
The first line contains two integers and (, ), separated by a single space: the number of vertices in the tree and the number of queries.
Each of the next lines contains three integers , , and (, , ): the vertices connected by the -th edge and its weight. It is guaranteed that the given edges form a tree.
Each of the next lines contains two integers and (): the -th query.
Output
Print lines. The -th line contains a single integer: the answer to the -th query.