#P4742. Tavas on the Path
Tavas on the Path
Tavas on the Path
题面翻译
给定一棵 个节点的树,每条边有边权。
有 个询问,形式为 ,求 到 的路径,假设长度为 ,第 条边权值为 ,构造一个长度为 的 01串 ,如果 ,那么 ,否则 。
对于得到的串 ,假设它有 段连续的 1,第 段长度为 ,那么要你输出所有 的和,其中 数组一开始就给出。
题目描述
Tavas lives in Tavaspolis. Tavaspolis has cities numbered from to connected by bidirectional roads. There exists a path between any two cities. Also each road has a length.
Tavas' favorite strings are binary strings (they contain only 0 and 1). For any binary string like , is its . can be calculated as follows:
Consider there are exactly blocks of s in this string (a block of s in is a maximal consecutive substring of that only contains ) with lengths .
Define where is a given sequence (if , then ).
Tavas loves queries. He asks you to answer queries. In each query he gives you numbers and you should print following number:
Consider the roads on the path from city to city : .
Build the binary string of length such that: if and only if where is the length of road .
You should print for this query.
输入格式
The first line of input contains integers and ( and ).
The next line contains space separated integers ( ).
The next lines contain the details of the roads. Each line contains integers and and it means that there's a road between cities and of length ( and ).
The next lines contain the details of the queries. Each line contains integers ( , and ).
输出格式
Print the answer of each query in a single line.
样例 #1
样例输入 #1
2 3
10
1 2 3
1 2 2
1 2 3
1 2 4
样例输出 #1
10
10
0
样例 #2
样例输入 #2
6 6
-5 0 0 2 10
1 2 1
2 3 2
3 4 5
4 5 1
5 6 5
1 6 1
1 6 2
1 6 5
3 6 5
4 6 4
1 4 2
样例输出 #2
10
-5
-10
-10
-5
0