Unique Encryption Keys
Time limit2sMemory limit128 MB
For each of up to a million range queries over a sequence of keys, decide if the range has a duplicate and report the smallest repeated key value.
- Level
Medium7 of 10
- Topics
- Segment tree, Binary search, Array, Sorting
- Solved
- No attempts yet
Problem
The security of many ciphers strongly depends on the fact that the keys are unique and are never re-used. This can be vitally important, since even a relatively strong cipher may be broken if the same key is used to encrypt several different messages.
In this problem, we detect repeated (duplicate) usage of keys. Given a sequence of keys used to encrypt messages, your task is to determine, for a specified interval, whether any key has been used more than once.
Input
The input contains several cipher descriptions. Each description starts with one line containing two integers and separated by a space. () is the number of encrypted messages, and () is the number of queries.
Each of the following lines contains one integer (), the identifier of the key used to encrypt the -th message. The next lines each contain one query, given by two integers and (), the interval of messages to check.
There is one empty line after each description. The input is terminated by a line containing two zeros in place of and .
Output
For each query, print one line. Print the string OK if all keys used to encrypt messages between and (inclusive) are pairwise distinct. Otherwise, print the smallest key identifier that occurs at least twice within that interval.
Separate the answers belonging to consecutive descriptions by one empty line.