Stock market tree

0

0 votes
Graph, Hard, Graphs, Algorithms, Algorithms, Graph Representation, Graph Representation
Problem

You are very curious to know about the frequency of stocks. The stocks are represented as nodes of a tree with prices of the stocks as their value. 

You are given a tree with N nodes (each node represents a stock) numbered from 1 to N (rooted at 1). Each stock has a value that is denoted by Pi. You are given queries of the form U L R. For each of the queries, you are required to determine the number of different stock prices or values that are available in the subtree of U for which frequency is between L and R (both inclusive).

Input format

  • First line: Two space-separated integers N and Q denoting the number of nodes in the tree and the number of queries respectively
  • Next N1 lines: Two integers a and b denoting an edge between a and b
  • Next line: N space-separated integers denoting the value of each node
  • Next Q lines: Three space-separated integers U, L, and R

Output format

Print Q lines containing the answer to each query.

Constraints

1N,Q,U1051LR1051Pi105

Sample Input
6 3
1 2
1 3
2 4
2 5
5 6
2 1 2 1 1 2
2 2 2
1 2 3
3 1 4
Sample Output
0
2
1
Time Limit: 5
Memory Limit: 256
Source Limit:
Explanation

0 (1 has frequency 3 and 2 has frequency 1 in the subtree of 2)

2 (1 has freq 3 and 2 has freq 3)

1 (1 has freq 2 and 2 has freq 0)

Contributers:
Editor Image

?