Nyehuing
Time limit1sMemory limit1024 MB
Given a sequence of N values, count for each ordered pair whether one value appears after another, then answer queries for the K-th smallest valid pair.
- Level
Medium7 of 10
- Topics
- Combinatorics, Prefix sum, Binary search, Sorting
- Solved
- No attempts yet
Problem
Haejo wants to pre-register for 'Pass of Yero', a major MMORPG whose open beta begins on August 3, 2019. Haejo wanted a pretty two-character nickname, but quicker users had already taken them, so he could not have one.
Examining the rule behind the taken nicknames, Haejo found that nicknames formed by taking two characters from a certain novel in order were all registered, and every other two-character nickname could be created. For example, suppose the novel reads 'Naratmalsseumidunggugedara'. By the rule Haejo discovered, 'Nami' already exists, while 'Assa' is a nickname that can be created, since in the quoted text 'ssa' comes before 'a', so the novel cannot form it.
Haejo will pick as his nickname the K-th lexicographically smallest among the two-character nicknames he can create. He chooses Q values of K, finds the nicknames, and will select the prettiest one among them. Help Haejo by writing a program that, given the text of the novel, finds the K-th lexicographically smallest nickname among the creatable ones.
For convenience, every character is represented by an integer between 1 and M, and a nickname AB being lexicographically smaller than a nickname CD means A < C, or A = C and B < D.
Input
The first line gives the length of the novel N (1 ≤ N ≤ 100,000), the number of character types M (1 ≤ M ≤ 1,000,000), and the number of queries Q (1 ≤ Q ≤ 100,000).
The second line gives N integers between 1 and M, the content of the novel.
Starting from the third line, Q lines each give the lexicographic index K (1 ≤ K ≤ M2) of the desired nickname.
Output
Over Q lines, print the K-th lexicographically smallest two-character nickname among the creatable ones. Separate the first and second characters with a space.
If the number of creatable nicknames is fewer than K, print -1 -1.
An earlier query does not affect later queries.