#P12861. [NERC 2020 Online] Lookup Performance

[NERC 2020 Online] Lookup Performance

题目描述

A binary search tree\emph{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)\verb|lookup(root, L, R)|, where root\verb|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 $\verb|v.left|, \verb|v.right|, \verb|v.min|, \verb|v.max|, \verb|v.key|$, and v.count\verb|v.count| are the fields of the nodes of the binary search tree.

  • v.left\verb|v.left| and v.right\verb|v.right| are the left and the right children of node vv, respectively.
  • v.min\verb|v.min| and v.max\verb|v.max| are the minimum and the maximum keys in the subtree rooted at node vv.
  • v.key\verb|v.key| is the key of node vv.
  • v.count\verb|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\verb|lookup| function that are made when lookup(root, L, R)\verb|lookup(root, L, R)| is called, including the initial lookup(root, L, R)\verb|lookup(root, L, R)| call itself.

输入格式

The first line contains an integer nn (1n2000001 \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 lil_i, rir_i, and kik_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 (li=0l_i = 0 or i<lini < l_i \le n; ri=0r_i = 0 or i<rini < r_i \le n; 109ki109-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\verb|v.min|, \verb|v.max| and v.count\verb|v.count| are not given in the input explicitly, since they can be deduced from lil_i, rir_i and kik_i.

The next line contains an integer qq (1q2000001 \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\verb|lookup| function.

输出格式

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

7
2 3 4
4 0 2
5 6 8
0 0 1
0 7 5
0 0 9
0 0 6
5
2 7
0 10
2 8
4 4
3 3
7
1
7
3
3