Problem: cf2178_e
Difficulty: medium
Categories: search
Time limit: 3000 ms, memory limit: 512 MB

This is an interactive problem.

While procrastinating at work, Dilhan the elf stumbled upon two arrays $a$ and $b$. Initially, both of them consist of a single integer $2^k$ (i.e., $a=b=[2^k]$), where $k$ is a non-negative integer.

Dilhan then applied the following two types of operations an arbitrary number of times (possibly zero), in any order:

1. Flatten — Choose either $a$ or $b$, and select any element $x$ that is maximal within that array ($x$ does not need to be maximal in the other array). Then, replace $x$ with two copies of $\frac x2$ in the same position. This operation can only be applied if $x$ is even.
2. Concatenate — Set both $a$ and $b$ to be $a+b$, where $+$ denotes array concatenation.

After performing these operations, Dilhan discards $b$, hides $a$ from you, and challenges you to a game.

Let $n$ be the length of the hidden array $a$. You may make the following query:

* Choose an interval $[l, r]$ ($1\le l\le r\le n$), and Dilhan will tell you the sum $a_l+a_{l+1}+\cdots+a_r$.

Determine the value of the maximum element of $a$ by making at most $300$ queries.

Input

Each test contains multiple test cases. The first line contains the number of test cases $t$ ($1 \le t \le 100$). The description of the test cases follows.

The first line of each test case contains a single integer $n$ ($1 \le n \le 10^5$) — the length of $a$.

It is guaranteed that $1\le a_i\le 2^{30}$, and the array $a$ can be generated by the process described in the statements.

It is guaranteed that the sum of $n$ over all test cases does not exceed $10^5$.

Interaction

For each test case, you are first given an integer $n$ — the length of the hidden array $a$. You may then make up to $300$ queries.

To make a query, print a line in the following format:

* $\texttt{?}\;l\;r$ ($1\le l\le r\le n$) — the indices chosen for the query.

After you print a query, the interactor will respond with a single integer — the sum $a_l+a_{l+1}+\ldots+a_r$.

To report that you have determined the value of the maximum element of $a$, print your answer in the following format:

* $\texttt{!}\; m$ ($1\le m\le 2^{30}$) — the value of the maximum element.

Printing the answer does not count as one of the $300$ queries.

After printing the answer, proceed to the next test case. If the current test case is the last one, terminate the program.

After printing each query do not forget to output the end of line and flush$^{\text{∗}}$ the output. Otherwise, you will get Idleness limit exceeded verdict.

If, at any interaction step, you read $-1$ instead of valid data, your solution must exit immediately. This means that your solution will receive Wrong answer because of an invalid query or any other mistake. Failing to exit can result in an arbitrary verdict because your solution will continue to read from a closed stream.

The interactor in this problem is not adaptive. In other words, the array $a$ is fixed before any queries and will not change during the interaction.

$^{\text{∗}}$To flush, use:

* fflush(stdout) or cout.flush() in C++;
* sys.stdout.flush() in Python;
* see the documentation for other languages.

Example

Input

```
4
11

9

4

12

1

1

8

4

4

4

4

8
```

Output

```
? 3 8

? 1 4

? 5 11

! 2

? 1 1

! 1

? 1 1

? 2 2

? 3 3

? 4 4

! 4

! 1073741824
```

Note

In the first test case, Dilhan's hidden array $a$ is $[1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2]$. It can be generated by the following process:

|  |  |  |  |
| --- | --- | --- | --- |
| # | Type | $a$ after operation | $b$ after operation |
| 0 |  | $[4]$ | $[4]$ |
| 1 | Flatten $a$ | $[\underline 4] \to {[2, 2]}$ | $[4]$ |
| 2 | Flatten $b$ | $[2, 2]$ | $[\underline 4] \to {[2, 2]}$ |
| 3 | Flatten $a$ | $[2, \underline 2] \to {[2, 1, 1]}$ | $[2, 2]$ |
| 4 | Concatenate | $[2, 1, 1, 2, 2]$ | $[2, 1, 1, 2, 2]$ |
| 5 | Flatten $a$ | $[\underline 2, 1, 1, 2, 2]\to {[1, 1, 1, 1, 2, 2]}$ | $[2, 1, 1, 2, 2]$ |
| 6 | Concatenate | $[1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2]$ | $[1, 1, 1, 1, 2, 2, 2, 1, 1, 2, 2]$ |

* For the query $\mathtt{?\;3\;8}$, the jury responded with $1+1+2+2+2+1=9$;
* For the query $\mathtt{?\;1\;4}$, the jury responded with $1+1+1+1=4$;
* For the query $\mathtt{?\;5\;11}$, the jury responded with $2+2+2+1+1+2+2=12$.

And the value of the maximum element of $a$ is $2$.

In the second test case, the array $a$ has only one element. By the query, the value of the only element is $1$, and it must also be the maximum value.

In the third test case, Dilhan's hidden array $a$ is $[4, 4, 4, 4, 4, 4, 4, 4]$. It can be generated by the following process:

|  |  |  |  |
| --- | --- | --- | --- |
| # | Type | $a$ after operation | $b$ after operation |
| 0 |  | $[4]$ | $[4]$ |
| 1 | Concatenate | $[4, 4]$ | $[4, 4]$ |
| 2 | Concatenate | $[4, 4, 4, 4]$ | $[4, 4, 4, 4]$ |
| 3 | Concatenate | $[4, 4, 4, 4, 4, 4, 4, 4]$ | $[4, 4, 4, 4, 4, 4, 4, 4]$ |

And the value of the maximum element of $a$ is $4$.

In the fourth test case, Dilhan's hidden array $a$ is $[2^{29}, 2^{29}, 2^{30}, 2^{29}, 2^{28}, 2^{28}, 2^{29}, 2^{29}]$.

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








static long long queries = 0;
static long long query_limit = 0;
static const long long HARD_CAP = 200000; 



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


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






int read_action(int& l, int& r, long long& ans) {
    string token;
    
    if (!(cin >> token)) return -1;

    if (token == "!") {
        if (!(cin >> ans)) return -1;
        return 1;
    }

    if (token == "?") {
        if (!(cin >> l >> r)) return -1;
        return 0;
    }

    
    
    try {
        size_t idx;
        l = stoi(token, &idx);
        if (idx != token.size()) return -1; 
    } catch (...) {
        return -1;
    }

    if (!(cin >> r)) return -1;
    return 0;
}

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

    
    int t = inf.readInt();
    
    
    query_limit = 300LL * t;
    log_metrics(); 

    
    cout << t << endl;

    while (t--) {
        int n = inf.readInt();
        
        
        vector<long long> a(n);
        long long true_max = -1;
        
        for (int i = 0; i < n; i++) {
            a[i] = inf.readLong();
            if (true_max == -1 || a[i] > true_max) {
                true_max = a[i];
            }
        }

        
        
        vector<long long> p(n + 1, 0);
        for (int i = 0; i < n; i++) {
            p[i+1] = p[i] + a[i];
        }

        
        cout << n << endl;

        bool solved = false;
        while (true) {
            
            if (queries > HARD_CAP) {
                finish(_pe, "Hard query limit exceeded (%lld)", HARD_CAP);
            }

            int l = -1, r = -1;
            long long user_ans = -1;
            int type = read_action(l, r, user_ans);

            if (type == -1) {
                finish(_wa, "Unexpected EOF or malformed input");
            }

            if (type == 1) {
                
                if (user_ans == true_max) {
                    solved = true;
                    break; 
                } else {
                    finish(_wa, "Incorrect answer: expected %lld, got %lld", true_max, user_ans);
                }
            } else {
                
                queries++;
                
                if (queries % 10 == 0) log_metrics();

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

                
                long long range_sum = p[r] - p[l-1];
                cout << range_sum << endl;
            }
        }
        
        if (!solved) {
            finish(_wa, "Loop terminated incorrectly");
        }
    }

    finish(_ok, "All cases passed");
    return 0;
}


Adaptive interactor (C++):

Test generator (C++):








#include "testlib.h"
#include <bits/stdc++.h>
using namespace std;

string mode;



struct ArrayState {
    list<long long> L;
    
    
    map<long long, vector<list<long long>::iterator>> vals;

    
    void add(long long val, list<long long>::iterator it) {
        vals[val].push_back(it);
    }

    
    
    void copy_from(const ArrayState& other) {
        L.clear();
        vals.clear();
        for (auto x : other.L) {
            L.push_back(x);
            vals[x].push_back(prev(L.end()));
        }
    }
    
    
    
    void append(const ArrayState& other) {
        for (auto x : other.L) {
            L.push_back(x);
            vals[x].push_back(prev(L.end()));
        }
    }
    
    
    bool can_flatten() {
        if (vals.empty()) return false;
        long long m = vals.rbegin()->first;
        return (m % 2 == 0);
    }
    
    
    void flatten() {
        if (vals.empty()) return;
        long long m = vals.rbegin()->first;
        auto& vec = vals.rbegin()->second;
        
        if (vec.empty()) { 
            vals.erase(m);
            return;
        }

        
        int idx = rnd.next(vec.size());
        auto it = vec[idx];
        
        
        vec[idx] = vec.back();
        vec.pop_back();
        if (vec.empty()) vals.erase(m);
        
        long long half = m / 2;
        
        
        *it = half;
        auto new_it = L.insert(next(it), half);
        
        
        vals[half].push_back(it);
        vals[half].push_back(new_it);
    }
    
    int size() const {
        return (int)L.size();
    }
};

static vector<long long> generate_flatten_only(int n, int k) {
    ArrayState A;
    long long start = 1LL << k;
    A.L.push_back(start);
    A.add(start, A.L.begin());

    while (A.size() < n) {
        if (!A.can_flatten()) break;
        A.flatten();
    }

    vector<long long> out;
    out.reserve(A.size());
    for (auto x : A.L) out.push_back(x);
    return out;
}

static void print_case(const vector<long long>& a) {
    cout << a.size() << "\n";
    for (int i = 0; i < (int)a.size(); i++) {
        if (i) cout << " ";
        cout << a[i];
    }
    cout << "\n";
}

int main(int argc, char* argv[]) {
    registerGen(argc, argv, 1);
    mode = opt<string>("mode", "non");

    long long seed_val = -1;
    try {
        seed_val = stoll(string(argv[1]));
    } catch (...) {
        seed_val = -1;
    }

    
    if (seed_val == 40) {
        const int t = 2;
        cout << t << "\n";

        
        
        print_case(generate_flatten_only(50000, 16));
        print_case(generate_flatten_only(50000, 30));
        return 0;
    }

    
    cout << 1 << "\n";

    
    
    int target_n = rnd.next(1000, 100000);
    
    if (rnd.next(10) == 0) target_n = rnd.next(1, 100);
    
    
    
    long long k;
    int type = rnd.next(100);
    if (type < 15) k = rnd.next(0, 5);      
    else if (type < 30) k = rnd.next(25, 30); 
    else k = rnd.next(0, 30);                
    
    ArrayState A, B;
    
    
    A.L.push_back(1LL << k);
    A.add(1LL << k, A.L.begin());
    
    B.L.push_back(1LL << k);
    B.add(1LL << k, B.L.begin());
    
    const int MAX_N = 100000;
    int ops = 0;
    
    
    
    while (ops < 300000 && A.size() < target_n) {
        ops++;
        
        vector<int> moves; 
        
        
        
        
        if (A.size() < MAX_N && A.can_flatten()) moves.push_back(0);
        if (B.size() < MAX_N && B.can_flatten()) moves.push_back(1);
        
        
        if ((long long)A.size() + B.size() <= MAX_N) {
            moves.push_back(2);
        }
        
        if (moves.empty()) break;
        
        
        int m = moves[rnd.next(moves.size())];
        
        if (m == 0) {
            A.flatten();
        } else if (m == 1) {
            B.flatten();
        } else {
            
            A.append(B);
            B.copy_from(A);
        }
    }
    
    
    
    cout << A.size() << endl;
    bool first = true;
    for (auto x : A.L) {
        if (!first) cout << " ";
        cout << x;
        first = false;
    }
    cout << endl;
    
    return 0;
}