RoAlgo Contest #10 | arborel

This was the problem page during the contest. Access the current page here.
Time limit: 0.5s Memory limit: 256MB Input: Output:

Returning to Moldova, Alexandru Lăpușneanu wants to take revenge on the boyars in a draconic manner, and the only way they can escape the wrath of the ruler is by solving the following problem:

Task

Given a tree with NN nodes rooted at node 11, each node ii having an associated value viv_i. There are QQ queries of the form: x,l,rx, l, r (lrl \leq r), which ask how many nodes in the subtree of node xx have values between ll and rr (inclusive).

Input data

The first line contains the natural number NN. The second line contains NN natural numbers v1,v2,...,vNv_1, v_2, ..., v_N - the values associated with the NN nodes. The next N1N-1 lines contain two natural numbers uu and vv, indicating that there is an edge in the tree between nodes uu and vv. The next line contains the natural number QQ, representing the number of queries. The next QQ lines contain three natural numbers x,l,rx, l, r as described in the statement.

Output data

The output will consist of QQ lines, where the ii-th line contains the answer to the ii-th query.

Constraints and clarifications

  • 1N150 0001 \leq N \leq 150\ 000
  • 1Q150 0001 \leq Q \leq 150\ 000
  • 1vi1091 \leq v_i \leq 10^9 for 1iN1 \leq i \leq N
  • For tests worth 2020 points, 1N10001 \leq N \leq 1000 and 1Q10001 \leq Q \leq 1000
  • Additional tests were added after the contest

Example

stdin

5
10 17 10 19 4
2 1
1 3
2 4
2 5
3
2 6 19
1 11 17
1 4 19

stdout

2
1
5

Log in or sign up to be able to send submissions!