Problem: cf1764_g1
Difficulty: medium
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{30}$ 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{30}$ 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 $30$ 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 = 30; 


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);
    int one_pos = -1;
    for (int i = 0; i < n; ++i) {
        p[i] = inf.readInt(1, n, "p_i");
        if (p[i] == 1) {
            one_pos = i + 1; 
        }
    }
    

    
    log_metrics();

    
    cout << n << endl;

    
    
    vector<bool> seen(n + 1, false);
    vector<int> reset_list;
    reset_list.reserve(n + 1);

    
    
    
    int hard_cap = 300; 

    while (true) {
        string token;
        
        if (!(cin >> token)) {
            finish(_wa, "Contestant output ended without providing an answer");
        }

        if (token == "!") {
            
            int y;
            if (!(cin >> y)) {
                finish(_pe, "Unexpected EOF after '!'");
            }

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

            if (y == one_pos) {
                finish(_ok, format("Correct! Found index %d.", y));
            } else {
                finish(_wa, format("Wrong Answer. Expected %d, found %d.", one_pos, y));
            }
            
        } 
        else {
            
            int l, r, k;

            
            if (token == "?") {
                if (!(cin >> l >> r >> k)) {
                    finish(_pe, "Unexpected EOF reading query parameters");
                }
            } else {
                
                try {
                    l = stoi(token);
                } catch (...) {
                    finish(_pe, format("Expected integer or '?', found '%s'", token.c_str()));
                }
                if (!(cin >> r >> k)) {
                    finish(_pe, "Unexpected EOF reading r, k");
                }
            }

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

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

            
            if (queries > hard_cap) {
                finish(_pe, format("Hard query limit exceeded (%d)", hard_cap));
            }

            
            
            for (int val : reset_list) {
                seen[val] = false;
            }
            reset_list.clear();

            
            int distinct_count = 0;
            for (int i = l - 1; i < r; ++i) {
                int val = p[i] / k;
                if (!seen[val]) {
                    seen[val] = true;
                    reset_list.push_back(val);
                    distinct_count++;
                }
            }

            cout << distinct_count << endl;
        }
    }

    return 0;
}


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

using namespace std;


static int queries = 0;
static int query_limit = 30;


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


template<typename... Args>
void finish(TResult result, const char* fmt, Args... args) {
    log_metrics();
    quitf(result, fmt, args...);
}

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

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

    
    
    vector<int> data;
    while (!inf.seekEof()) {
        data.push_back(inf.readInt());
    }

    vector<int> p(n);
    int target = -1; 

    if (data.size() == 1) {
        
        target = data[0];
        ensuref(target >= 1 && target <= n, "Target index %d out of bounds [1, %d]", target, n);

        
        p[target - 1] = 1;

        vector<int> others;
        others.reserve(n - 1);
        for (int i = 2; i <= n; i++) {
            others.push_back(i);
        }

        
        mt19937 rng(1337);
        shuffle(others.begin(), others.end(), rng);

        int ptr = 0;
        for (int i = 0; i < n; i++) {
            if (i == target - 1) continue;
            p[i] = others[ptr++];
        }

    } else {
        
        if (data.size() != (size_t)n) {
            finish(_fail, "Input format error: expected %d integers for permutation, found %d", n, (int)data.size());
        }

        vector<int> count(n + 1, 0);
        for (int i = 0; i < n; i++) {
            p[i] = data[i];
            ensuref(p[i] >= 1 && p[i] <= n, "Permutation value %d out of bounds", p[i]);
            count[p[i]]++;
        }

        for (int i = 1; i <= n; i++) {
            ensuref(count[i] == 1, "Invalid permutation: value %d appears %d times", i, count[i]);
        }

        
        for (int i = 0; i < n; i++) {
            if (p[i] == 1) {
                target = i + 1;
                break;
            }
        }
    }

    
    cout << n << endl;
    
    
    log_metrics();

    
    
    int hard_cap = 200; 

    while (true) {
        if (queries > hard_cap) {
            finish(_pe, "Too many queries (hard cap hit)");
        }

        string type;
        if (!(cin >> type)) {
            
            finish(_pe, "Unexpected end of output from solution");
        }

        if (type == "?") {
            queries++;
            int l, r, k;
            if (!(cin >> l >> r >> k)) {
                finish(_pe, "Invalid query format");
            }

            
            if (l < 1 || l > n || r < 1 || r > n || k < 1 || k > n) {
                finish(_pe, "Query parameters out of bounds: %d %d %d", l, r, k);
            }
            if (l > r) {
                finish(_pe, "Invalid query range: l (%d) > r (%d)", l, r);
            }

            
            set<int> distinct;
            for (int i = l - 1; i < r; i++) {
                distinct.insert(p[i] / k);
            }

            cout << distinct.size() << endl;

        } else if (type == "!") {
            int y;
            if (!(cin >> y)) {
                finish(_pe, "Invalid answer format");
            }

            if (y == target) {
                finish(_ok, "Correct! Found index %d in %d queries", target, queries);
            } else {
                finish(_wa, "Wrong Answer. Expected index %d, found %d", target, y);
            }

        } else {
            finish(_pe, "Invalid query type '%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");
    
    
    
    
    long long seed_val = -1;
    if (argc > 1) {
        string s = argv[1];
        
        if (!s.empty() && s.find_first_not_of("0123456789") == string::npos) {
            try {
                seed_val = stoll(s);
            } catch (...) {
                seed_val = -1;
            }
        }
    }

    int n;
    vector<int> p;

    
    if (seed_val == 1) {
        
        n = 1024;
        p.resize(n);
        iota(p.begin(), p.end(), 1);
    }
    else if (seed_val == 2) {
        
        n = 1024;
        p.resize(n);
        iota(p.begin(), p.end(), 1);
        reverse(p.begin(), p.end());
    }
    else if (seed_val == 3) {
        
        n = 3;
        p = {2, 3, 1}; 
    }
    else if (seed_val == 4) {
        
        
        n = 1024;
        p.assign(n, 0);
        
        vector<int> smalls = {1, 2, 3, 4, 5};
        int start_pos = n / 2 - 2;
        
        for (int i = 0; i < (int)smalls.size(); ++i) {
            if (start_pos + i < n) p[start_pos + i] = smalls[i];
        }
        
        vector<int> others;
        for (int i = 6; i <= n; ++i) others.push_back(i);
        shuffle(others.begin(), others.end());
        
        int ptr = 0;
        for (int i = 0; i < n; ++i) {
            if (p[i] == 0) p[i] = others[ptr++];
        }
    }
    else if (seed_val == 5) {
        
        n = 512;
        p.resize(n);
        iota(p.begin(), p.end(), 1);
        shuffle(p.begin(), p.end());
    }
    else {
        
        
        if (rnd.next(0, 1)) {
            n = 1024;
        } else {
            n = rnd.next(3, 1024);
        }
        
        p.resize(n);
        iota(p.begin(), p.end(), 1);
        shuffle(p.begin(), p.end());
    }

    
    if (mode == "non") {
        
        cout << n << endl;
        for (int i = 0; i < n; ++i) {
            cout << p[i] << (i == n - 1 ? "" : " ");
        }
        cout << endl;
    } else {
        
        
        int target = -1;
        for (int i = 0; i < n; ++i) {
            if (p[i] == 1) {
                target = i + 1;
                break;
            }
        }
        cout << n << endl;
        cout << target << endl;
    }

    return 0;
}