Problem: cf1584_d
Difficulty: easy
Categories: search, math
Time limit: 1000 ms, memory limit: 256 MB

This is an interactive problem.

Jury initially had a sequence $a$ of length $n$, such that $a_i = i$.

The jury chose three integers $i$, $j$, $k$, such that $1 \leq i < j < k \leq n$, $j - i > 1$. After that, Jury reversed subsegments $[i, j - 1]$ and $[j, k]$ of the sequence $a$.

Reversing a subsegment $[l, r]$ of the sequence $a$ means reversing the order of elements $a_l, a_{l+1}, \ldots, a_r$ in the sequence, i. e. $a_l$ is swapped with $a_r$, $a_{l+1}$ is swapped with $a_{r-1}$, etc.

You are given the number $n$ and you should find $i$, $j$, $k$ after asking some questions.

In one question you can choose two integers $l$ and $r$ ($1 \leq l \leq r \leq n$) and ask the number of inversions on the subsegment $[l, r]$ of the sequence $a$. You will be given the number of pairs $(i, j)$ such that $l \leq i < j \leq r$, and $a_i > a_j$.

Find the chosen numbers $i$, $j$, $k$ after at most $40$ questions.

The numbers $i$, $j$, and $k$ are fixed before the start of your program and do not depend on your queries.

Input

Each test consists of multiple test cases. The first line contains a single integer $t$ ($1 \leq t \leq 100$) — the number of test cases. Description of the test cases follows.

The single line of each test case contains a single integer $n$ ($4 \leq n \leq 10^9$). After reading it you should start an interaction process by asking questions for that test case. After giving an answer you should:

* Terminate your program if that is the last test case.
* Proceed to the next test case otherwise.

Interaction

To ask number of inversions on a subsegment $[l, r]$, print "? l r", where ($1 \leq l \leq r \leq n$). You can ask at most $40$ questions in each test case. As a result you should read a single integer $x$.

* If $x = -1$, your program made an invalid question or you exceeded the number of questions for that test case. Your program should terminate immediately (otherwise it can get any verdict instead of "Wrong Answer").
* Otherwise $x$ is equal to the number of inversions on the subsegment $[l, r]$ of the sequence $a$.

To give the answer, print "! i j k", where $i$, $j$, $k$ are the numbers you found. You should continue solving the next test cases or terminate the program after that.

After printing a question or an answer do not forget to print the end of line and flush the output. Otherwise, you will get an "Idleness limit exceeded" verdict. 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 the documentation for other languages.

Example

Input

```
2 
5 

4 

3 

3 

5 

2 

2 

1
```

Output

```
? 1 5

? 2 5

? 3 5

! 1 3 5

? 1 5

? 2 5

? 3 5

! 2 4 5
```

Note

In the first test case, $i = 1$, $j = 3$, $k = 5$, so the sequence $a$ is $[2, 1, 5, 4, 3]$.

In the second test case, $i = 2$, $j = 4$, $k = 5$, so the sequence $a$ is $[1, 3, 2, 5, 4]$.

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

struct Triple {
    long long i;
    long long j;
    long long k;
};

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

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

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

static long long choose2(long long x) {
    if (x <= 1) return 0;
    return x * (x - 1) / 2;
}

static long long isect_len(long long l, long long r, long long a, long long b) {
    long long L = max(l, a);
    long long R = min(r, b);
    if (L > R) return 0;
    return R - L + 1;
}

static long long inversions_on_segment(long long l, long long r, const Triple& t) {
    long long len1 = isect_len(l, r, t.i, t.j - 1);
    long long len2 = isect_len(l, r, t.j, t.k);
    return choose2(len1) + choose2(len2);
}

static void validate_hidden(long long n, const Triple& t) {
    if (!(1 <= t.i && t.i < t.j && t.j < t.k && t.k <= n)) {
        finish(_fail, "Invalid hidden triple bounds");
    }
    if (t.j - t.i <= 1) {
        finish(_fail, "Invalid hidden triple: j-i must be > 1");
    }
}

static void run_one_test(long long n, const Triple& t) {
    int used = 0;
    string cmd;
    while (cin >> cmd) {
        if (cmd == "?") {
            long long l, r;
            if (!(cin >> l >> r)) finish(_pe, "Invalid query format");
            if (!(1 <= l && l <= r && r <= n)) {
                cout << -1 << "\n";
                cout.flush();
                finish(_wa, "Query out of range");
            }

            used++;
            queries++;
            if (used > 40) {
                cout << -1 << "\n";
                cout.flush();
                finish(_wa, "Query limit exceeded");
            }

            cout << inversions_on_segment(l, r, t) << "\n";
            cout.flush();
            continue;
        }

        if (cmd == "!") {
            long long i, j, k;
            if (!(cin >> i >> j >> k)) finish(_pe, "Invalid answer format");
            if (i == t.i && j == t.j && k == t.k) {
                return;
            }
            finish(_wa, "Wrong answer");
        }

        finish(_pe, "Unknown command: " + cmd);
    }

    finish(_pe, "Unexpected EOF");
}

int main(int argc, char* argv[]) {
    registerInteraction(argc, argv);
    atexit(log_metrics);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    const int tc = inf.readInt(1, 100, "t");
    query_limit = 40LL * tc;

    cout << tc << "\n";
    cout.flush();

    for (int ti = 0; ti < tc; ti++) {
        const long long n = inf.readLong(4, 1000000000LL, "n");
        Triple t;
        t.i = inf.readLong(1, n, "i");
        t.j = inf.readLong(1, n, "j");
        t.k = inf.readLong(1, n, "k");
        validate_hidden(n, t);

        cout << n << "\n";
        cout.flush();
        run_one_test(n, t);
    }

    finish(_ok, "OK");
}


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

struct Triple {
    long long i;
    long long j;
    long long k;
};

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

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

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

static long long choose2(long long x) {
    if (x <= 1) return 0;
    return x * (x - 1) / 2;
}

static long long isect_len(long long l, long long r, long long a, long long b) {
    long long L = max(l, a);
    long long R = min(r, b);
    if (L > R) return 0;
    return R - L + 1;
}

static long long inversions_on_segment(long long l, long long r, const Triple& t) {
    long long len1 = isect_len(l, r, t.i, t.j - 1);
    long long len2 = isect_len(l, r, t.j, t.k);
    return choose2(len1) + choose2(len2);
}

static long long parse_case_id(const char* case_path) {
    string s(case_path ? case_path : "");
    size_t slash = s.find_last_of("/\\");
    if (slash != string::npos) s = s.substr(slash + 1);
    size_t dot = s.find_last_of('.');
    if (dot != string::npos) s = s.substr(0, dot);
    long long id = 0;
    bool any = false;
    for (unsigned char ch : s) {
        if (isdigit(ch)) {
            any = true;
            id = id * 10 + (ch - '0');
        }
    }
    return any ? id : 0;
}

static Triple gen_triple(long long n) {
    
    long long total = rnd.next(4LL, n);
    long long L1 = rnd.next(2LL, total - 2);
    long long L2 = total - L1;
    long long i = rnd.next(1LL, n - total + 1);
    Triple t;
    t.i = i;
    t.j = i + L1;
    t.k = t.j + L2 - 1;
    return t;
}

static void run_one_test(long long n, const Triple& t) {
    int used = 0;
    string cmd;
    while (cin >> cmd) {
        if (cmd == "?") {
            long long l, r;
            if (!(cin >> l >> r)) finish(_pe, "Invalid query format");
            if (!(1 <= l && l <= r && r <= n)) {
                cout << -1 << "\n";
                cout.flush();
                finish(_wa, "Query out of range");
            }

            used++;
            queries++;
            if (used > 40) {
                cout << -1 << "\n";
                cout.flush();
                finish(_wa, "Query limit exceeded");
            }

            cout << inversions_on_segment(l, r, t) << "\n";
            cout.flush();
            continue;
        }

        if (cmd == "!") {
            long long i, j, k;
            if (!(cin >> i >> j >> k)) finish(_pe, "Invalid answer format");
            if (i == t.i && j == t.j && k == t.k) return;
            finish(_wa, "Wrong answer");
        }

        finish(_pe, "Unknown command: " + cmd);
    }

    finish(_pe, "Unexpected EOF");
}

int main(int argc, char* argv[]) {
    registerInteraction(argc, argv);
    atexit(log_metrics);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    
    long long case_id = (argc >= 2) ? parse_case_id(argv[1]) : 0;

    const int tc = inf.readInt(1, 100, "t");
    query_limit = 40LL * tc;

    cout << tc << "\n";
    cout.flush();

    for (int ti = 0; ti < tc; ti++) {
        const long long n = inf.readLong(4, 1000000000LL, "n");
        rnd.setSeed(1000003LL * (case_id + 1) + 97LL * (ti + 1) + n);
        const Triple t = gen_triple(n);
        if (!(1 <= t.i && t.i < t.j && t.j < t.k && t.k <= n && t.j - t.i > 1)) {
            finish(_fail, "Internal error: generated invalid triple");
        }

        cout << n << "\n";
        cout.flush();
        run_one_test(n, t);
    }

    finish(_ok, "OK");
}


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

static long long clampLL(long long x, long long lo, long long hi) {
    if (x < lo) return lo;
    if (x > hi) return hi;
    return x;
}

struct Triple {
    long long i, j, k;
};

static Triple makeFromLengths(long long i, long long L1, long long L2) {
    Triple t;
    t.i = i;
    t.j = i + L1;
    t.k = t.j + L2 - 1;
    return t;
}

static void ensureValid(long long n, const Triple& t) {
    ensuref(1 <= t.i && t.i < t.j && t.j < t.k && t.k <= n, "Invalid i<j<k or bounds");
    ensuref(t.j - t.i > 1, "Invalid: j-i must be > 1");
}

static long long pickTotalLenCloseToN(long long n) {
    long long slack = min(2000LL, n - 4);
    if (slack < 0) slack = 0;
    long long totalLen = n - rnd.next(0LL, slack);
    if (totalLen < 4) totalLen = 4;
    if (totalLen > n) totalLen = n;
    return totalLen;
}

static Triple genNonHandcrafted(long long seedVal) {
    if (seedVal == 1) {
        long long n = 4;
        return {1, 3, 4};
    }
    if (seedVal == 2) {
        
        return {1, 4, 5};
    }
    if (seedVal == 3) {
        
        
        return {333333333LL, 666666666LL, 1000000000LL};
    }
    if (seedVal == 4) {
        
        
        return {1, 6, 9}; 
    }
    return {-1, -1, -1};
}

static pair<long long, Triple> genNonRandom(long long nmin, long long nmax, long long nFixed, int patternForced, long long seedVal) {
    long long n;
    if (nFixed != -1) {
        n = nFixed;
    } else if (seedVal == 1) {
        n = 4;
    } else if (seedVal == 2) {
        n = 5;
    } else if (seedVal == 3) {
        n = 1000000000LL;
    } else if (seedVal == 4) {
        n = 9;
    } else {
        
        int roll = rnd.next(0, 99);
        if (roll < 75) {
            long long lo = max(nmin, nmax - 1000000LL);
            n = rnd.next(lo, nmax);
        } else if (roll < 95) {
            long long lo = max(nmin, 1000000LL);
            n = rnd.next(lo, nmax);
        } else {
            long long hi = min(nmax, 1000000LL);
            if (hi < nmin) hi = nmin;
            n = rnd.next(nmin, hi);
        }
        n = clampLL(n, nmin, nmax);
        if (n < 4) n = 4;
    }

    
    Triple hand = genNonHandcrafted(seedVal);
    if (hand.i != -1) {
        ensureValid(n, hand);
        return {n, hand};
    }

    int pattern = patternForced;
    if (pattern == -1) pattern = rnd.next(0, 7);

    long long L1 = 2, L2 = 2;
    long long i = 1;

    auto pickIForTotalLen = [&](long long totalLen) -> long long {
        long long maxStart = n - totalLen + 1;
        if (maxStart <= 1) return 1;
        if (maxStart <= 3) return rnd.next(1LL, maxStart);
        
        long long center = (maxStart + 1) / 2;
        long long delta = min(1000000LL, center - 1);
        long long lo = center - delta;
        long long hi = min(maxStart, center + delta);
        if (lo > hi) lo = 1, hi = maxStart;
        return rnd.next(lo, hi);
    };

    if (pattern == 0) {
        
        long long totalLen = pickTotalLenCloseToN(n);
        L1 = rnd.next(2LL, totalLen - 2);
        L2 = totalLen - L1;
        i = 1;
    } else if (pattern == 1) {
        
        long long totalLen = pickTotalLenCloseToN(n);
        L2 = rnd.next(2LL, totalLen - 2);
        L1 = totalLen - L2;
        i = n - totalLen + 1;
    } else if (pattern == 2) {
        
        long long totalLenLo = max(4LL, n / 2);
        long long totalLenHi = n;
        long long totalLen = rnd.next(totalLenLo, totalLenHi);
        if (totalLen < 4) totalLen = 4;
        if (totalLen > n) totalLen = n;

        long long loL1 = max(2LL, totalLen / 3);
        long long hiL1 = totalLen - 2;
        if (loL1 > hiL1) loL1 = 2, hiL1 = totalLen - 2;
        L1 = rnd.next(loL1, hiL1);
        L2 = totalLen - L1;
        i = pickIForTotalLen(totalLen);
    } else if (pattern == 3) {
        
        L1 = 2;
        long long maxL2 = n - L1;
        if (maxL2 < 2) { L1 = 2; L2 = 2; i = 1; }
        else {
            long long L2lo = max(2LL, maxL2 - 2000LL);
            long long L2hi = maxL2;
            L2 = rnd.next(L2lo, L2hi);
            long long totalLen = L1 + L2;
            i = pickIForTotalLen(totalLen);
        }
    } else if (pattern == 4) {
        
        L2 = 2;
        long long maxL1 = n - L2;
        if (maxL1 < 2) { L1 = 2; L2 = 2; i = 1; }
        else {
            long long L1lo = max(2LL, maxL1 - 2000LL);
            long long L1hi = maxL1;
            L1 = rnd.next(L1lo, L1hi);
            long long totalLen = L1 + L2;
            i = pickIForTotalLen(totalLen);
        }
    } else if (pattern == 5) {
        
        
        L1 = 5; L2 = 4;
        long long totalLen = L1 + L2;
        if (n < totalLen) {
            n = totalLen;
        }
        i = pickIForTotalLen(totalLen);
    } else if (pattern == 6) {
        
        L1 = 1000000LL + rnd.next(0LL, 12345LL);
        L2 = 1000000LL + rnd.next(0LL, 12345LL);
        long long totalLen = L1 + L2;
        if (totalLen > n) {
            
            totalLen = min(n, max(4LL, n - rnd.next(0LL, min(1000LL, n - 4))));
            L1 = max(2LL, totalLen / 2);
            L2 = totalLen - L1;
            if (L2 < 2) { L2 = 2; L1 = totalLen - 2; }
        }
        i = pickIForTotalLen(L1 + L2);
    } else {
        
        long long totalLen = rnd.next(4LL, n);
        if (totalLen < 4) totalLen = 4;
        if (totalLen > n) totalLen = n;
        L1 = rnd.next(2LL, totalLen - 2);
        L2 = totalLen - L1;
        i = rnd.next(1LL, n - totalLen + 1);
    }

    if (L1 < 2) L1 = 2;
    if (L2 < 2) L2 = 2;
    if (L1 + L2 > n) {
        long long totalLen = n;
        L1 = max(2LL, totalLen / 2);
        L2 = totalLen - L1;
        if (L2 < 2) { L2 = 2; L1 = totalLen - 2; }
        i = 1;
    }

    Triple t = makeFromLengths(i, L1, L2);
    ensureValid(n, t);
    return {n, t};
}

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

    long long nmin = opt<long long>("nmin", 4LL);
    long long nmax = opt<long long>("nmax", 1000000000LL);
    long long nFixed = opt<long long>("n", -1LL);
    int patternForced = opt<int>("pattern", -1);

    if (nmin < 4) nmin = 4;
    if (nmax < nmin) nmax = nmin;

    long long seedVal = 0;
    if (argc >= 2) {
        seedVal = atoll(argv[1]);
        if (seedVal < 0) seedVal = -seedVal;
    }

    if (mode != "non" && mode != "adp") {
        quitf(_fail, "Unknown --mode: %s (expected non/adp)", mode.c_str());
    }

    
    cout << 1 << "\n";

    if (mode == "adp") {
        long long n;
        if (nFixed != -1) n = nFixed;
        else if (seedVal == 1) n = 4;
        else if (seedVal == 2) n = 5;
        else if (seedVal == 3) n = 1000000000LL;
        else {
            int roll = rnd.next(0, 99);
            if (roll < 85) {
                long long lo = max(nmin, nmax - 1000000LL);
                n = rnd.next(lo, nmax);
            } else {
                n = rnd.next(nmin, nmax);
            }
        }
        n = clampLL(n, nmin, nmax);
        if (n < 4) n = 4;
        cout << n << "\n";
        return 0;
    }

    auto [n, t] = genNonRandom(nmin, nmax, nFixed, patternForced, seedVal);
    cout << n << " " << t.i << " " << t.j << " " << t.k << "\n";
    return 0;
}