Problem: cf2157_g
Difficulty: medium
Categories: bit, search
Time limit: 4000 ms, memory limit: 256 MB

wavetearz - contemporary chemical bonds

⠀

You have reached the final level of the popular roguelike game "Isaac's Keybindings". Instead of a boss, you encounter a shopkeeper who holds an hidden array of integers $a_1, a_2, \ldots, a_n$, where $0 \leq a_i < 2^{30}$ for each $i$ in $[1, n]$.

It is guaranteed that the array is generated randomly, i.e., each $a_i$ ($1 \leq i \leq n$) is an integer independently generated uniformly at random in $[0, 2^{30})$, in all the tests excluding the example.

Let $f(u, v) = a_u \oplus a_{u+1} \oplus \ldots \oplus a_v$, where $\oplus$ is the bitwise $\texttt{XOR}$.

You can ask queries of the following form: $\texttt{? u v}$, with $1 \leq u \leq v \leq n$.

The answer to the query is:

* $-1$, if $f(u, v) = 0$;
* $\lfloor \log_2(f(u, v)) \rfloor$ otherwise.

Each query has a cost of $\frac{1}{v-u+1}$ robocoins. On each test, you are given $300$ robocoins in total to pass at most $30$ test cases (that is, you can spend $10$ robocoins on average for a single test case). If your balance ever becomes negative you lose. Note that your robocoin balance does not need to be an integer at any moment.

Find the answer to all possible $\frac{n(n+1)}{2}$ queries without losing.

Input

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

The first line of each test case contains one integer $n$ ($n = 3$ or $n = 100$) — the length of the array $a_1, a_2, \ldots, a_n$. It is guaranteed that the array is generated randomly in all the tests excluding the example.

There are exactly $50$ tests in this problem (including the example). The example has $t = 1$ and $n = 3$, and all the other tests have $t = 30$ and $n = 100$.

Interaction

For each test case, first read a single integer $n$. If the integer you read is $-2$, it means that the answer to the previous test case was wrong, and you should exit immediately.

To ask a query, print a line in the format $\texttt{? u v}$ with $1 \leq u \leq v \leq n$.

As a response to the query, you will receive $-2$ if you made an invalid query (i.e., the format is invalid, or you would reach a negative amount of robocoins). In that case, you should exit immediately. Otherwise, you will receive the answer to the query.

When you determine the answers to all the $\frac{n(n+1)}{2}$ queries, print them in the following format.

Print $n+1$ lines. The first line must contain a single character $\texttt{!}$. The $i$-th of the next $n$ lines must contain $n-i+1$ integers: the answers to queries $\texttt{? i i}, \, \texttt{? i i+1}, \,\ldots, \, \texttt{? i n}$, respectively.

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

* $\texttt{fflush(stdout)}$ or $\texttt{cout.flush()}$ in C++;
* $\texttt{System.out.flush()}$ in Java;
* $\texttt{stdout.flush()}$ in Python;
* see the documentation for other languages.

Example

Input

```
1
3

2

-1

1
```

Output

```? 1 2? 1 3? 2 3!
1 2 -1 
2 1 
2
```

Note

In the example, the hidden array is $[a_1, a_2, a_3] = [2, 4, 6]$.

* First, you ask $\texttt{? 1 2}$. Since $f(1, 2) = a_1 \oplus a_2 = 6$, the answer is $\lfloor \log_2(6) \rfloor = 2$.
* Then, you ask $\texttt{? 1 3}$. Since $f(1, 3) = a_1 \oplus a_2 \oplus a_3 = 0$, the answer is $-1$.
* Then, you ask $\texttt{? 2 3}$. Since $f(2, 3) = a_2 \oplus a_3 = 2$, the answer is $\lfloor \log_2(2) \rfloor = 1$.

Now, even without knowing the hidden array, you claim the answer to all the possible $6$ queries. For example, you claim that the answer to $\texttt{? 1 1}$ is $1$.

You have spent $1/2 + 1/3 + 1/2 = 4/3$ robocoins, which is less than the allowed $300$ robocoins.

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


static long long queries = 0;           
static long long query_limit = 50000;   
static double remaining_budget = 300.0; 
static const double EPS = 1e-9;


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());
}


long long get_val(int u, int v, const vector<int>& pref) {
    
    
    long long x = pref[v] ^ pref[u-1];
    if (x == 0) return -1;
    
    
    return 31 - __builtin_clz((unsigned int)x);
}

void solve_case(int test_idx) {
    
    int n = inf.readInt();
    vector<int> a(n);
    for (int i = 0; i < n; i++) {
        a[i] = inf.readInt();
    }

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

    
    cout << n << endl;

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

        string token;
        
        if (!(cin >> token)) {
            finish(_wa, "Unexpected EOF: expected query or '!'");
        }

        if (token == "!") {
            
            
            
            for (int i = 1; i <= n; i++) {
                for (int j = i; j <= n; j++) {
                    long long user_ans;
                    if (!(cin >> user_ans)) {
                        finish(_wa, "Unexpected EOF while reading answer matrix");
                    }
                    long long correct = get_val(i, j, pref);
                    if (user_ans != correct) {
                        
                        
                        
                        cout << -2 << endl;
                        finish(_wa, format("Wrong answer for range [%d, %d]: expected %lld, got %lld", i, j, correct, user_ans));
                    }
                }
            }
            
            return;
        } else {
            
            int u, v;
            if (token == "?") {
                if (!(cin >> u >> v)) finish(_wa, "Invalid query format");
            } else {
                
                try {
                    u = stoi(token);
                } catch (...) {
                    finish(_wa, "Invalid query token: expected '?' or integer");
                }
                if (!(cin >> v)) finish(_wa, "Invalid query format: missing v");
            }

            
            if (u < 1 || u > n || v < 1 || v > n || u > v) {
                cout << -2 << endl;
                finish(_wa, format("Query range out of bounds: %d %d", u, v));
            }

            
            double cost = 1.0 / (v - u + 1);
            remaining_budget -= cost;
            queries++;
            
            
            if (queries % 10 == 0) log_metrics();

            
            if (remaining_budget < -EPS) {
                cout << -2 << endl;
                finish(_wa, "Budget limit exceeded (300 robocoins)");
            }

            
            cout << get_val(u, v, pref) << endl;
        }
    }
}

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

    
    int t = inf.readInt();
    cout << t << endl;

    for (int i = 0; i < t; i++) {
        solve_case(i);
    }

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


Adaptive interactor (C++):

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 def_t = 30;
    int def_n = 100;
    if (string(argv[1]) == "1") {
        def_t = 1;
        def_n = 3;
    }

    
    int t = opt<int>("t", def_t);
    int n = opt<int>("n", def_n);

    
    
    
    
    
    
    
    
    
    if (mode == "non" || mode == "adp") {
        cout << t << endl;
        for (int i = 0; i < t; i++) {
            cout << n << endl;
            vector<int> a(n);

            
            if (string(argv[1]) == "1" && i == 0 && n == 3) {
                a = {2, 4, 6};
            } else {
                
                
                for (int j = 0; j < n; j++) {
                    a[j] = rnd.next(0, (1 << 30) - 1);
                }
            }

            for (int j = 0; j < n; j++) {
                cout << a[j] << (j == n - 1 ? "" : " ");
            }
            cout << endl;
        }
    } else {
        quitf(_fail, "Unknown mode: %s", mode.c_str());
    }

    return 0;
}