Codeforces Round 299 (Div. 1)
E
Tavas on the Path
Codeforces
3100
原题
← D→
翻译中…

Tavas lives in Tavaspolis. Tavaspolis has $*n*$ cities numbered from $1$ to $*n*$ connected by $*n* - 1$ bidirectional roads. There exists a path between any two cities. Also each road has a length.

翻译中…

image

翻译中…

Tavas' favorite strings are binary strings (they contain only 0 and 1). For any binary string like $*s* = *s*_{1}*s*_{2}... *s*_{*k*}$, $*T*(*s*)$ is its $*Goodness*$. $*T*(*s*)$ can be calculated as follows:

翻译中…

Consider there are exactly $*m*$ blocks of $1$s in this string (a block of $1$s in $*s*$ is a maximal consecutive substring of $*s*$ that only contains $1$) with lengths $*x*_{1}, *x*_{2}, ..., *x*_{*m*}$.

翻译中…

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

翻译中…

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

翻译中…

Consider the roads on the path from city $*v*$ to city $*u*$: $*e*_{1}, *e*_{2}, ..., *e*_{*x*}$.

翻译中…

Build the binary string $*b*$ of length $*x*$ such that: $*b*_{*i*} = 1$ if and only if $*l* ≤ *w*(*e*_{*i*})$ where $*w*(*e*)$ is the length of road $*e*$.

翻译中…

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

翻译中…

Input

翻译中…

The first line of input contains integers $*n*$ and $*q*$ ($2 ≤ *n* ≤ 10^{5}$ and $1 ≤ *q* ≤ 10^{5}$).

翻译中…

The next line contains $*n* - 1$ space separated integers $*f*_{1}, *f*_{2}, ..., *f*_{*n* - 1}$ ($|*f*_{*i*}| ≤ 1000$).

翻译中…

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

翻译中…

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

翻译中…

Output

翻译中…

Print the answer of each query in a single line.

翻译中…

样例

Input #1
2 3
10
1 2 3
1 2 2
1 2 3
1 2 4
Output #1
10
10
0
InputCopy OutputCopy
Input #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
Output #2
10
-5
-10
-10
-5
0
InputCopy OutputCopy
题目信息
题号
536E
时间限制
3 s
内存限制
256 MB
标签
数据结构
分治
树

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。