Tree & Radius
Practice
0 (0 votes)
Medium
Problem
26% Success 115 Attempts 30 Points 1s Time Limit 256MB Memory 1024 KB Max Code

Given a tree with N nodes, and two integers A and B.
Every node has a magic value \(M_{i}\) (1<=i<=N) associated with it.

Consider Node 1 as root of the tree.

There are Q queries of type -

- X U V , where you have to find the number of nodes Y in the subtree of node X such that -
- A <= Distance ( X, Y ) <= B
- U <= Magic value of Y <= V

Note : Distance ( X, Y ) - Number of edges in path from X to Y.

Input Format

First line contain three integers N,A,B, number of nodes and A,B .
Next line contains N integers, \(M_{i}\) magic value of each Node from 1 to N.
Next line contains N-1 space separated integers, \(P_{i}\) parent of each Node from 2 to N .
Next line contain Q, number of queries.
Next Q lines contains queries of type as mentioned above.

Output Format

Answer corresponding to each query in a single line .

Constraints

1<= N, Mi, Q, A, B, U, V <= 10^5,
1<= Pi < i

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:30
2 votes
Tags:
Dynamic programmingAlgorithmsMediumTreesDynamic Programming
Points:50
22 votes
Tags:
Medium-Hard
Points:30
12 votes
Tags:
AlgorithmsDepth-first searchTreesDynamic Programming
Editorial

No editorial available for this problem.