Vacation
Practice
4.5 (2 votes)
Trees
Binary search
Data structures
Lowest common ancestor
Sparse table
Problem
45% Success 632 Attempts 50 Points 1s Time Limit 256MB Memory 1024 KB Max Code

There is a country consisting of \(N\) cities numbered with integer numbers from \(0\) to \(N - 1\) with values \(A_i \; (0 \leq i \leq N-1)\), and \(N-1\) bidirectional roads connecting them, such that we can travel between any two cities using these roads. In other words, these cities and roads form a tree.

Arya is currently on vacation and plans to visit either city \(U\) or city \(V\). He wants to choose some starting points on the shortest path between \(U\) and \(V\) (excluding city \(U, V\)) so that he can visit at least one of the cities, \(U\) or \(V\), without passing through a city with a value less than \(K \). This means he must not stay or pass through a city (including destition city \(U\) or city \(V\)) that has a value less than \(K\).

You will be given \(Q\) queries. Every query contains three integers denoting two cities \(U, V\) and value \(K\). Your task is to help him find the count of the possible starting points for each query.

Input format

  • The first line contains an integer \(N\) denotes the number of cities.
  • The second line contains \(N\) space-separated integers denoting the values of cities \(A_i\).
  • The next \(N - 1\) line contains \(2\) space-separated integers \(u\) and \(v\) denoting the connection between cities.
  • The fourth line contains \(Q\) denoting the number of queries.
  • Each of the next \(Q\) lines contains three space-separated integers \(U, V\) and \(K\).

Output format

For each query, print the count of possible cities in a new line.

Constraints

\(2 \lt N \le 10 ^ 5\)
\(0 \leq u, v \leq N-1\)
\(0 \lt Q \le 10 ^ 5\)
\(0 \leq U, V \le N-1\)
\(U \neq V\)
\(0 \lt A_i\ ,\ K \le 10 ^ 9\)

 

Please login to use the editor

You need to be logged in to access the code editor

Loading...

Please wait while we load the editor

Loading...
Results
Custom Input
Run your code to see the output
Submissions
Please login to view your submissions
Similar Problems
Points:50
5 votes
Tags:
Sparse TableTreeData StructuresMedium-HardLoopsSegment tree
Points:50
1 votes
Tags:
Dynamic ProgrammingData StructuresTreesLowest Common Ancestor
Points:50
7 votes
Tags:
Dynamic ProgrammingXORImplementationTreeTreesCombinatoricsData StructuresLowest Common AncestorMathematics