#P4742. Tavas on the Path

Tavas on the Path

Tavas on the Path

题面翻译

给定一棵 nn 个节点的树,每条边有边权。

mm 个询问,形式为 (u,v,l)(u,v,l),求 uuvv 的路径,假设长度为 pp,第 ii 条边权值为 xix_i,构造一个长度为 pp 的 01串 ss,如果 xilx_i\ge l,那么 si=1s_i=1,否则 si=0s_i=0

对于得到的串 ss,假设它有 kk 段连续的 1,第 ii 段长度为 pip_i,那么要你输出所有 fpif_{p_i} 的和,其中 ff 数组一开始就给出。

题目描述

Tavas lives in Tavaspolis. Tavaspolis has n n cities numbered from 1 1 to n n connected by n1 n-1 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 s=s1s2... sk s=s_{1}s_{2}...\ s_{k} , T(s) T(s) is its Goodness Goodness . T(s) T(s) can be calculated as follows:

Consider there are exactly m m blocks of 1 1 s in this string (a block of 1 1 s in s s is a maximal consecutive substring of s s that only contains 1 1 ) with lengths x1,x2,...,xm x_{1},x_{2},...,x_{m} .

Define where f f is a given sequence (if m=0 m=0 , then T(s)=0 T(s)=0 ).

Tavas loves queries. He asks you to answer q q queries. In each query he gives you numbers v,u,l v,u,l and you should print following number:

Consider the roads on the path from city v v to city u u : e1,e2,...,ex e_{1},e_{2},...,e_{x} .

Build the binary string b b of length x x such that: bi=1 b_{i}=1 if and only if l<=w(ei) l<=w(e_{i}) where w(e) w(e) is the length of road e e .

You should print T(b) T(b) for this query.

输入格式

The first line of input contains integers n n and q q ( 2<=n<=105 2<=n<=10^{5} and 1<=q<=105 1<=q<=10^{5} ).

The next line contains n1 n-1 space separated integers f1,f2,...,fn1 f_{1},f_{2},...,f_{n-1} ( fi<=1000 |f_{i}|<=1000 ).

The next n1 n-1 lines contain the details of the roads. Each line contains integers v,u v,u and w w and it means that there's a road between cities v v and u u of length w w ( 1<=v,u<=n 1<=v,u<=n and 1<=w<=109 1<=w<=10^{9} ).

The next q q lines contain the details of the queries. Each line contains integers v,u,l v,u,l ( 1<=v,u<=n 1<=v,u<=n , vu v≠u and 1<=l<=109 1<=l<=10^{9} ).

输出格式

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