Minimum Queries
Обмеження: 2 сек., 512 МіБ
This is an interactive task, where your program and the judge interact via standard input and output.
The judge has a permutation pp of integers (1,2,…,n)(1,2,…,n).
You are given two integers nn and mm. The permutation pp is not given to you.
You can ask the judge at most n−1n−1 following questions.
Choose two integers ll and rr such that 1≤l≤r≤n1≤l≤r≤n and ask the index ii such that l≤i≤rl≤i≤r and pi≤pjpi≤pj for all l≤j≤rl≤j≤r. In other words, ask the position of the minimum element in the range p[l,r]p[l,r].
Find the index kk such that pk=mpk=m. If it is impossible to find it, report this.
Вхідні дані
First, receive two integers nn and mm from standard input.
Then, you get to ask the judge at most n−1n−1 questions as described in the problem statement.
Print each question to standard output in the format "?
l r l r", where l,rl,r are integers satisfying 1≤l≤r≤n1≤l≤r≤n.
In response to this, the answer to your question – the index ii – will be given from standard input. Here, l≤i≤rl≤i≤r.
Вихідні дані
When you find the index kk
satisfying pk=mpk=m, print it in the
format "!
k k".
If it is impossible to find this index, print ! -1
.
Обмеження
1≤m≤n≤1041≤m≤n≤104.
Примітки
Print a newline and flush standard output at the end of each message.
After printing the answer, immediately quit the program.
The permutation pp will be fixed at the start of the interaction and will not be changed according to your questions or other factors.
In the following interaction, n=7n=7, m=3m=3, and p=(1,3,5,6,4,7,2)p=(1,3,5,6,4,7,2).
Input | Output | Description |
---|---|---|
7 3 |
nn and mm are given. | |
? 1 7 |
Ask the position of the minimum in the range p[1,7]p[1,7]. | |
1 |
The judge responds with i=1i=1. | |
? 4 4 |
Ask the position of the minimum in the range p[4,4]p[4,4]. | |
4 |
The judge responds with i=4i=4. | |
? 2 7 |
Ask the position of the minimum in the range p[2,7]p[2,7]. | |
7 |
The judge responds with i=7i=7. | |
? 2 6 |
Ask the position of the minimum in the range p[2,6]p[2,6]. | |
2 |
The judge responds with i=2i=2. | |
! 2 |
Print k=2k=2 as the answer. |
For k=2k=2, we have pk=mpk=m. Thus, if the program immediately quits here, this case will be judged as correctly solved.
Here is another example with n=4n=4, m=2m=2, and p=(2,1,4,3)p=(2,1,4,3).
Input | Output | Description |
---|---|---|
4 2 |
nn and mm are given. | |
? 1 4 |
Ask the position of the minimum in the range p[1,4]p[1,4]. | |
2 |
The judge responds with i=2i=2. | |
! -1 |
Print -1 as the answer since
it is impossible to find the index kk such that pk=mpk=m. |