Lookup Performance

아직 제출이 없습니다시간 제한2초메모리 제한512 MB

문제

A binary search tree is a rooted binary tree whose nodes store keys so that each node's key is greater than all the keys in the node's left subtree and less than those in its right subtree.

A binary search tree can be used to maintain sorted sets and allows to perform different types of queries on the set. A query that we are considering in this problem is finding the number of values in the range \[L,R]\[L, R] within the set. 

Let SS be the set of all keys in the binary search tree. You are given two values LL and RR. The query is to find the number of such xSx \in S so that LxRL \le x \le R. The following recursive function computes this value when called with the parameters lookup(root, L, R), where root is the root of the binary search tree.

function lookup(v, L, R):
  if v == null:
    return 0
  if L <= v.min and v.max <= R:
    return v.count
  if v.max < L or R < v.min:
    return 0
  res = 0
  if L <= v.key and v.key <= R:
    res += 1
  res += lookup(v.left, L, R)
  res += lookup(v.right, L, R)
  return res

Values v.left, v.right, v.min, v.max, v.key, and  v.count are the fields of the nodes of the binary search tree. 

  • v.left and v.right are the left and the right children of node vv, respectively.
  • v.min and v.max are the minimum and the maximum keys in the subtree rooted at node vv.
  • v.key is the key of node vv.
  • v.count is the number of nodes in the subtree rooted at node vv.

You are given a binary search tree with integer keys. You are also given queries, each query consisting of two integers LL and RR. Find the number of calls of the lookup function that are made when lookup(root, L, R) is called, including the initial lookup(root, L, R) call itself.

입력

The first line contains an integer nn (1n200,0001 \le n \le 200\\,000) --- the number of nodes in the binary search tree.

The next nn lines describe the nodes of the tree. The ii-th of these lines contains three integers l_il\_i, r_ir\_i, and k_ik\_i denoting the left child, the right child and the key of the ii-th node. If the node does not have the left or/and the right child, the corresponding value is 0 (l_i=0l\_i = 0 or i<l_ini < l\_i \le n; r_i=0r\_i = 0 or i<r_ini < r\_i \le n; 109k_i109-10^9 \le k\_i \le 10^9). The root of the tree is at node 1 and it is guaranteed to be a well-formed binary search tree.

Note that the values v.min, v.max and v.count are not given in the input explicitly, since they can be deduced from l_il\_i, r_ir\_i and k_ik\_i.

The next line contains an integer qq (1q200,0001 \le q \le 200\\,000) --- the number of the queries.

Each of the next qq lines contains two integers LL and RR (109LR109-10^9 \le L \le R \le 10^9) --- the parameters given to the lookup function.

출력

Output qq lines, the ii-th line containing a single integer --- the number of calls of the lookup function that are made for the ii-th query.