Doremy's Perfect DS Class (Hard Version)
Contest: Codeforces Global Round 24
Difficulty rating: 3300
Tags: binary search, interactive

The only difference between this problem and the other two versions is the maximum number of queries. In this version, you are allowed to ask at most $$$\mathbf{20}$$$ queries. You can make hacks only if all versions of the problem are solved.

This is an interactive problem.

"Everybody! Doremy's Perfect Data Structure Class is about to start! Come and do your best if you want to have as much IQ as me!" In today's Data Structure class, Doremy is teaching everyone a powerful data structure — Doremy tree! Now she gives you a quiz to prove that you are paying attention in class.

Given an array $$$a$$$ of length $$$m$$$, Doremy tree supports the query $$$Q(l,r,k)$$$, where $$$1 \leq l \leq r \leq m$$$ and $$$1 \leq k \leq m$$$, which returns the number of distinct integers in the array $$$\left[\lfloor\frac{a_l}{k} \rfloor, \lfloor\frac{a_{l+1}}{k} \rfloor, \ldots, \lfloor\frac{a_r}{k} \rfloor\right]$$$.

Doremy has a secret permutation $$$p$$$ of integers from $$$1$$$ to $$$n$$$. You can make queries, in one query, you give $$$3$$$ integers $$$l,r,k$$$ ($$$1 \leq l \leq r \leq n$$$, $$$1 \leq k \leq n$$$) and receive the value of $$$Q(l,r,k)$$$ for the array $$$p$$$. Can you find the index $$$y$$$ ($$$1 \leq y \leq n$$$) such that $$$p_y=1$$$ in at most $$$\mathbf{20}$$$ queries?

Note that the permutation $$$p$$$ is fixed before any queries are made.

Interaction format:
You begin the interaction by reading an integer $$$n$$$ ($$$3 \le n \le 1024$$$) in the first line — the length of the permutation.

To make a query, you should output

- "? $$$l\ r\ k$$$" ($$$1 \leq l \leq r \leq n$$$, $$$1 \leq k \leq n$$$)

To give the answer, you should output

- "! $$$y$$$" ($$$1 \leq y \leq n$$$)

After printing a query or the answer, do not forget to output the end of line and flush the output. Otherwise, you will get Idleness limit exceeded. To do this, use:

- fflush(stdout) or cout.flush() in C++;
- System.out.flush() in Java;
- flush(output) in Pascal;
- stdout.flush() in Python;
- see documentation for other languages.

Hacks Format

The first line of the hack contains an integer $$$n$$$ ($$$3 \le n \le 1024$$$) — the length of the permutation.

The second line of the hack contains $$$n$$$ distinct integers $$$p_1,p_2,\ldots,p_n$$$ ($$$1 \le p_i\le n$$$) — the permutation.

Constraints:
Time limit: 1.0 s
Memory limit: 256.0 MB

Note:
The permutation in the example is $$$[3,5,2,1,4]$$$.

The input and output for example illustrate possible interaction on that test (empty lines are inserted only for clarity).

In this interaction process:

- For the first query, $$$\lfloor\frac{3}{4}\rfloor=0,\lfloor\frac{5}{4}\rfloor=1,\lfloor\frac{2}{4}\rfloor=0$$$, so the answer is $$$2$$$.
- For the second query, $$$\lfloor\frac{2}{3}\rfloor=0,\lfloor\frac{1}{3}\rfloor=0,\lfloor\frac{4}{3}\rfloor=1$$$, so the answer is still $$$2$$$.
- For the third query, $$$\lfloor\frac{2}{5}\rfloor=0,\lfloor\frac{1}{5}\rfloor=0$$$, so the answer is $$$1$$$.
- For the fourth query, $$$\lfloor\frac{2}{2}\rfloor=1,\lfloor\frac{1}{2}\rfloor=0,\lfloor\frac{4}{2}\rfloor=2$$$, so the answer is $$$3$$$.

The correct answer is got after $$$4$$$ queries, so this process will be judged correct.

Example input:
5

2

2

1

3

Example output:
? 1 3 4

? 3 5 3

? 3 4 5

? 3 5 2

! 4

Editorial:
If we add an edge between numbers $$$a,b$$$ when $$$\lfloor\frac{a}{2}\rfloor=\lfloor\frac{b}{2}\rfloor$$$, then when $$$n$$$ is an odd number, $$$1$$$ is the only one which has no neighbor, so let's consider the odd case at first.
Let the number of edges $$$(u,v)$$$ satisfying $$$u,v\in [l,r]$$$ be $$$C(l,r)$$$, then $$$C(l,r)+Q(l,r,2)=r-l+1$$$. That is to say, we can get any $$$C(l,r)$$$ with a query.
Try to use binary search to solve this problem. The key problem is for a certain $$$m$$$, determining whether $$$1$$$ is in $$$[1,m]$$$ or $$$[m+1,n]$$$. Let $$$2C(1,m)+x=m,2C(m+1,n)+y=n-m$$$. The edges between $$$[1,m]$$$ and $$$[m+1,n]$$$ must connect the remaining $$$x$$$ numbers in $$$[1,m]$$$ and the remaining $$$y$$$ numbers in $$$[m+1,n]$$$. There is exactly one number with no neighbor, so if $$$x=y+1$$$, $$$1$$$ is in $$$[1,m]$$$, otherwise $$$y=x+1$$$ and $$$1$$$ is in $$$[m+1,n]$$$. So we can do this case in $$$20$$$ queries.
If $$$n$$$ is an even number, then $$$1,n$$$ will be the only two number which has no neighbor. With the similar analysis as above, we can know if $$$x>y$$$, $$$1,n$$$ are in $$$[1,m]$$$, if $$$y>x$$$, $$$1,n$$$ are in $$$[m+1,n]$$$, otherwise $$$1,n$$$ are in the different sides. When $$$l<r$$$, we can use $$$Q(l,r,n)$$$ to check whether $$$n$$$ is in $$$[l,r]$$$. Because $$$n\ge 3$$$, we can use this method to determine $$$1$$$ is in which side. So we can do this case in $$$30$$$ queries.
Finding that we only need one extra query $$$Q(l,r,n)$$$, we can do the even case in $$$21$$$ queries.
When we decrease the range of $$$1$$$ to $$$[l,r]$$$, we have already got $$$C(1,l-1),C(1,r),C(l,n),C(r+1,n)$$$. When $$$l+1=r$$$, if $$$C(1,l-1)+1=C(1,r)$$$, we only need to know $$$C(1,l)$$$ to get the answer, if $$$C(l,n)=C(r+1,n)+1$$$, we only need to know $$$C(r,n)$$$ to get the answer, otherwise it means that $$$p_l=1,p_r=n$$$ or $$$p_l=n,p_r=1$$$, this case we also only need one more query. So we decrease the number of queries by $$$1$$$, solving this case in $$$20$$$ queries.