Tree queries

4.8

4 votes
Advanced Data Structures, Segment Trees, C++, Data Structures
Problem

You are given a tree T with N nodes rooted at node 1. Each node has a value A[i] associated with it.

You are required to perform Q queries where each query is of type:

  • 1 u x: Increment the value associated with node u by x.
  • 2 p: Find the shortest distance of any node from the root that has a value strictly greater than p. If no such node exists, then print -1.

Note: There exists at least 1 query of type 2.

Input format

  • The first line contains an integer T denoting the number of test cases.
  • The first line of each test case contains an integer N.
  • The second line of each test case contains N space-separated integers denoting array A.
  • Next N1 lines of each test case contain two space-separated integers denoting the edge.
  • The next line of each test case contains an integer Q denoting the number of queries.
  • Next Q lines of each test case contain queries.

Output format

For each test case, print the answer of queries of type 2 in a new line.

Constraints

1T102N,Q1051A[i]1061x1061p1012

Sample Input
1
5
4 2 5 6 1
1 2
1 3
2 4
2 5
3
2 5
1 5 6
2 6
Sample Output
2
2
Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation
  • Query 1 :-
    • Only value of node 4 is greater than 5, and it is at a distance of 2 units from root node.
  • Query 2 :-
    • Increase the value of node 5 by 6. Thus, A[5]=1+6=7.
  • Query 3 :-
    • Only value of node 5 is greater than 6 and it is at a distance of 2 units from root node.
Editor Image

?