#BST0000016. Nhiều truy vấn successor và predecessor (Multiple Predecessor and Successor Queries)
Nhiều truy vấn successor và predecessor (Multiple Predecessor and Successor Queries)
Multiple Predecessor and Successor Queries
Source: Phuoc Hung OJ
Version: Phuoc Hung OJ Extended
Problem Statement
Build a BST from in insertion order. If a key appears more than once, only its first occurrence creates a node; later duplicates are ignored.
For each query value , which does not have to exist in the tree, determine two strict neighboring keys by value:
- the predecessor is the largest stored key such that ;
- the successor is the smallest stored key such that .
If one side has no qualifying key, its result is NONE.
Input
The first line contains .
If , the next line contains keys . If , this line is absent.
The next line contains — the number of queries.
Each of the next lines contains one integer .
Output
For each query, print one line in the exact order predecessor successor.
Print NONE in the first field if the predecessor does not exist, and NONE in the second field if the successor does not exist.
Subtasks
Subtask 1 (20 points): , .
Subtask 2 (30 points): , .
Subtask 3 (50 points): , .
In all subtasks, every stored key and query value is a signed 64-bit integer.
Examples
Input
7
8 3 10 1 6 14 4
3
6
1
9
Output
4 8
NONE 3
8 10
Explanation
For , the largest key below is and the smallest key above is .
For , no key is smaller than , while the successor is , so the result is NONE 3.
For , the predecessor is and the successor is .