Problem: cf1764_g3
Difficulty: hard
Categories: search, math
Time limit: 1000 ms, memory limit: 256 MB

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.

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

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$)

in a separate line. After each query, you should read an integer $x$ — the value of $Q(l,r,k)$ for $p$. In this version of the problem, you can make at most $20$ such queries.

To give the answer, you should output

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

in a separate line, where $p_y=1$.

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.

Example

Input

```
5

2

2

1

3
```

Output

```
? 1 3 4

? 3 5 3

? 3 4 5

? 3 5 2

! 4
```

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.

Non-adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;


static long long queries = 0;
static long long query_limit = 20; 




static void log_metrics() {
    try {
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
        
    }
}



static void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}

int main(int argc, char** argv) {
    
    registerInteraction(argc, argv);
    
    
    atexit(log_metrics);

    
    
    int n = inf.readInt(3, 1024, "n");
    
    
    vector<int> p(n + 1);
    for (int i = 1; i <= n; i++) {
        p[i] = inf.readInt(1, n, format("p[%d]", i));
    }
    
    
    {
        vector<int> temp;
        temp.reserve(n);
        for(int i = 1; i <= n; i++) temp.push_back(p[i]);
        sort(temp.begin(), temp.end());
        for(int i = 0; i < n; i++) {
            if (temp[i] != i + 1) {
                quitf(_fail, "Input array is not a permutation of 1..n");
            }
        }
    }

    
    log_metrics();

    
    cout << n << endl; 

    
    long long hard_cap = 2000;

    
    while (true) {
        string token;
        
        if (!(cin >> token)) {
            finish(_wa, "Unexpected end of stream (expected '?' or '!')");
        }

        if (token == "!") {
            
            int y;
            if (!(cin >> y)) {
                finish(_wa, "Expected integer index after '!'");
            }

            
            if (y < 1 || y > n) {
                finish(_wa, format("Index %d out of bounds [1, %d]", y, n));
            }

            
            if (p[y] == 1) {
                finish(_ok, format("Correct! p[%d] = 1. Queries used: %lld", y, queries));
            } else {
                finish(_wa, format("Wrong answer. p[%d] = %d, expected 1", y, p[y]));
            }

        } else {
            
            int l, r, k;

            
            if (token == "?") {
                if (!(cin >> l >> r >> k)) {
                    finish(_wa, "Expected 3 integers (l r k) after '?'");
                }
            } else {
                
                try {
                    l = stoi(token);
                } catch (...) {
                    finish(_wa, format("Expected '?' or integer, found '%s'", token.c_str()));
                }
                if (!(cin >> r >> k)) {
                    finish(_wa, "Expected r and k");
                }
            }

            
            if (l < 1 || l > r || r > n) {
                finish(_wa, format("Invalid query range: l=%d, r=%d (n=%d)", l, r, n));
            }
            if (k < 1 || k > n) {
                finish(_wa, format("Invalid k: %d (n=%d)", k, n));
            }

            
            queries++;
            
            if (queries % 10 == 0) log_metrics();
            
            
            if (queries > hard_cap) {
                finish(_pe, "Hard query limit exceeded (infinite loop protection)");
            }

            
            
            vector<int> values;
            values.reserve(r - l + 1);
            for (int i = l; i <= r; i++) {
                values.push_back(p[i] / k); 
            }
            
            sort(values.begin(), values.end());
            int ans = (int)(unique(values.begin(), values.end()) - values.begin());

            
            cout << ans << endl; 
        }
    }

    return 0;
}


Adaptive interactor (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;


static long long queries = 0;
static long long query_limit = 20; 
static int n;
static vector<int> p; 


static void log_metrics() {
    try {
        
        tout << "queries=" << queries << endl;
        tout << "query_limit=" << query_limit << endl;
    } catch (...) {
        
    }
}


static void finish(TResult verdict, const string& msg) {
    log_metrics();
    quitf(verdict, "%s", msg.c_str());
}


int solve_query(int l, int r, int k) {
    
    static vector<int> vals;
    vals.clear();
    vals.reserve(r - l + 1);
    
    for (int i = l; i <= r; ++i) {
        vals.push_back(p[i] / k);
    }
    
    
    if (vals.empty()) return 0;
    sort(vals.begin(), vals.end());
    
    int cnt = 1;
    for (size_t i = 1; i < vals.size(); ++i) {
        if (vals[i] != vals[i - 1]) {
            cnt++;
        }
    }
    return cnt;
}

int main(int argc, char** argv) {
    
    registerInteraction(argc, argv);
    
    
    atexit(log_metrics);

    
    n = inf.readInt(3, 1024, "n");

    
    
    
    if (inf.seekEof()) {
        p.resize(n);
        iota(p.begin(), p.end(), 1);
        
        ::shuffle(p.begin(), p.end());
    } else {
        p.resize(n);
        for (int i = 0; i < n; ++i) {
            p[i] = inf.readInt(1, n, format("p[%d]", i + 1));
        }
        
    }

    
    cout << n << endl;
    
    
    query_limit = 20;
    log_metrics();

    int hard_cap = 200; 

    while (true) {
        
        if (queries > hard_cap) {
            finish(_pe, "Hard query limit exceeded (infinite loop?)");
        }

        string type;
        if (!(cin >> type)) {
            finish(_pe, "Unexpected EOF (expected '?' or '!')");
        }

        if (type == "!") {
            int y;
            if (!(cin >> y)) {
                finish(_pe, "Expected integer after '!'");
            }
            
            if (y < 1 || y > n) {
                finish(_wa, format("Index %d out of bounds [1, %d]", y, n));
            }
            
            
            
            if (p[y - 1] == 1) {
                finish(_ok, format("Correct! Found 1 at index %d in %lld queries", y, queries));
            } else {
                int correct = -1;
                for (int i = 0; i < n; ++i) {
                    if (p[i] == 1) correct = i + 1;
                }
                finish(_wa, format("Wrong answer. Guess %d, but p[%d]=%d. Correct index is %d.", y, y, p[y - 1], correct));
            }
        } else if (type == "?") {
            int l, r, k;
            if (!(cin >> l >> r >> k)) {
                finish(_pe, "Invalid query format");
            }

            
            if (l < 1 || l > n || r < 1 || r > n || l > r) {
                finish(_pe, format("Invalid range l=%d, r=%d (n=%d)", l, r, n));
            }
            if (k < 1 || k > n) {
                finish(_pe, format("Invalid k=%d (n=%d)", k, n));
            }

            queries++;
            
            if (queries % 5 == 0) log_metrics();

            
            
            int ans = solve_query(l - 1, r - 1, k);
            cout << ans << endl; 
        } else {
            finish(_pe, format("Unknown token '%s', expected '?' or '!'", type.c_str()));
        }
    }

    return 0;
}


Test generator (C++):
#include "testlib.h"
#include <bits/stdc++.h>

using namespace std;

int main(int argc, char* argv[]) {
    registerGen(argc, argv, 1);

    
    string mode = opt<string>("mode", "non");
    int n = opt<int>("n", 0);

    
    
    long long seedVal = 0;
    if (argc > 1) {
        string s = argv[1];
        if (!s.empty() && all_of(s.begin(), s.end(), ::isdigit)) {
            seedVal = atoll(s.c_str());
        }
    }

    
    if (n == 0) {
        if (seedVal == 1) n = 3;                       
        else if (seedVal >= 2 && seedVal <= 7) n = 1024; 
        else if (seedVal == 8) n = 1023;                 
        else {
            
            if (rnd.next(3) == 0) n = rnd.next(3, 1024);
            else n = 1024;
        }
    }

    
    cout << n << endl;

    
    if (mode == "adp") {
        return 0;
    }

    
    vector<int> p(n);
    iota(p.begin(), p.end(), 1);

    if (seedVal == 6) {
        reverse(p.begin(), p.end());
    } else if (seedVal == 7) {
        
    } else {
        shuffle(p.begin(), p.end());

        
        if (seedVal == 3) {
            
            iter_swap(p.begin(), find(p.begin(), p.end(), 1));
        } else if (seedVal == 4) {
            
            iter_swap(p.end() - 1, find(p.begin(), p.end(), 1));
        } else if (seedVal == 5) {
            
            iter_swap(p.begin() + n / 2, find(p.begin(), p.end(), 1));
        }
    }

    println(p);

    return 0;
}