Problem: cf1520_f2
Difficulty: medium
Categories: data structures, search
Time limit: 4000 ms, memory limit: 256 MB

This is an interactive problem.

This is a hard version of the problem. The difference from the easy version is that in the hard version $1 \le t \le \min(n, 10^4)$ and the total number of queries is limited to $6 \cdot 10^4$.

Polycarp is playing a computer game. In this game, an array consisting of zeros and ones is hidden. Polycarp wins if he guesses the position of the $k$-th zero from the left $t$ times.

Polycarp can make no more than $6 \cdot 10^4$ requests totally of the following type:

* ? $l$ $r$ — find out the sum of all elements in positions from $l$ to $r$ ($1 \le l \le r \le n$) inclusive.

To make the game more interesting, each guessed zero turns into one and the game continues on the changed array. More formally, if the position of the $k$-th zero was $x$, then after Polycarp guesses this position, the $x$-th element of the array will be replaced from $0$ to $1$.

Help Polycarp win the game.

Interaction

First, your program must read two integers $n$ and $t$ ($1 \le n \le 2 \cdot 10^5$, $1 \le t \le \min(n, 10^4)$).

Then $t$ lines follow, each of which contains one integer $k$ ($1 \le k \le n$). It is guaranteed that at the moment of the request the array contains at least $k$ zeros. In order to get the next value of $k$, you must output the answer for the previous value of $k$.

After that, you can make no more than $6 \cdot 10^4$ requests in total.

Use the following format to output the answer (it is not a request, it doesn't count in $6 \cdot 10^4$):

* ! $x$ — position of the $k$-th zero.

Positions in the array are numbered from left to right from $1$ to $n$ inclusive.

After printing $t$ answers, your program should exit immediately.

In this task, the interactor is not adaptive. This means that within the same test, the hidden array and the queries do not change.

In case of an incorrect query, -1 will be displayed. When this value is received, your program must immediately exit normally (for example, by calling exit(0)), otherwise, the testing system may issue an arbitrary verdict.

If the number of requests is exceeded, the verdict wrong answer will be displayed.

Your solution may get the verdict Idleness limit exceeded if you don't print anything or forget to flush the output buffer.

To flush the output buffer, you need to do the following immediately after the query output and the end-of-line character:

* 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

```
6 2

2

2

1

1

0

1

0
```

Output

```
? 4 6

? 1 1

? 1 2

? 5 5

! 5

? 2 2

! 2
```

Note

In the first test, the array $[1, 0, 1, 1, 0, 1]$ is hidden. After answering the query $k=2$, the array changed to $[1, 0, 1, 1, 1, 1]$.

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

using namespace std;



static long long queries = 0;
static const long long query_limit_val = 60000;
static long long query_limit = query_limit_val;


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



struct Fenwick {
    int n;
    vector<int> tree;
    Fenwick(int n) : n(n), tree(n + 1, 0) {}
    
    
    void add(int i, int delta) {
        for (; i <= n; i += i & -i) tree[i] += delta;
    }
    
    
    int query(int i) {
        int s = 0;
        for (; i > 0; i -= i & -i) s += tree[i];
        return s;
    }
    
    
    int query(int l, int r) {
        if (l > r) return 0;
        return query(r) - query(l - 1);
    }
};

int main(int argc, char* argv[]) {
    
    signal(SIGPIPE, SIG_IGN);

    registerInteraction(argc, argv);
    atexit(log_metrics);

    
    int n = inf.readInt(1, 200000, "n");
    int t = inf.readInt(1, min(n, 10000), "t");

    
    
    vector<int> a(n + 1);
    Fenwick ft(n);
    for (int i = 1; i <= n; ++i) {
        a[i] = inf.readInt(0, 1, format("a[%d]", i));
        if (a[i] == 1) {
            ft.add(i, 1);
        }
    }

    
    vector<int> ks(t);
    for (int i = 0; i < t; ++i) {
        ks[i] = inf.readInt(1, n, format("k[%d]", i));
    }

    
    log_metrics();

    
    cout << n << " " << t << endl;

    
    for (int round = 0; round < t; ++round) {
        int target_k = ks[round];
        cout << target_k << endl; 

        bool round_solved = false;
        while (!round_solved) {
            string token;
            
            if (!(cin >> token)) {
                finish(_wa, "Unexpected EOF (solution exited prematurely)");
            }

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

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

                
                
                if (a[x] != 0) {
                    finish(_wa, format("Position %d contains 1, expected 0", x));
                }
                
                
                
                int ones_count = ft.query(x);
                int zeros_count = x - ones_count;

                if (zeros_count != target_k) {
                    finish(_wa, format("Position %d is the %d-th zero, expected %d-th", x, zeros_count, target_k));
                }

                
                
                a[x] = 1;
                ft.add(x, 1);
                round_solved = true; 
            } else {
                
                int l, r;
                bool has_qm = (token == "?");
                
                if (has_qm) {
                    if (!(cin >> l)) finish(_wa, "Expected l after '?'");
                } else {
                    
                    try {
                        size_t pos;
                        l = stoi(token, &pos);
                        if (pos != token.size()) finish(_wa, "Invalid token: " + token);
                    } catch (...) {
                        finish(_wa, "Invalid token: " + token);
                    }
                }

                if (!(cin >> r)) finish(_wa, "Expected r after l");

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

                queries++;
                
                if (queries % 50 == 0) log_metrics();
                
                if (queries > 200000) {
                    finish(_pe, "Too many queries (hard cap hit)");
                }

                
                int ans = ft.query(l, r);
                cout << ans << endl;
            }
        }
    }

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


Adaptive interactor (C++):

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


struct Fenwick {
    int n;
    vector<int> tree;
    Fenwick(int n) : n(n), tree(n + 1, 0) {}
    
    
    void add(int i, int delta) {
        for (; i <= n; i += i & -i) tree[i] += delta;
    }
    
    
    int query(int i) {
        int s = 0;
        for (; i > 0; i -= i & -i) s += tree[i];
        return s;
    }
    
    
    
    int find_kth_zero(int k) {
        int l = 1, r = n, ans = -1;
        while (l <= r) {
            int mid = l + (r - l) / 2;
            if (mid - query(mid) >= k) {
                ans = mid;
                r = mid - 1;
            } else {
                l = mid + 1;
            }
        }
        return ans;
    }
};

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

    int n, t;
    
    
    if (seed_val == 1) {
        
        n = 200000; 
        t = 10000;
    } else if (seed_val == 2) {
        
        n = 200000; 
        t = 10000;
    } else if (seed_val == 3) {
        
        n = 200000; 
        t = 10000;
    } else if (seed_val == 4) {
        
        n = 200000; 
        t = 10000;
    } else {
        
        n = rnd.next(1, 200000);
        t = rnd.next(1, min(n, 10000));
        
        if (rnd.next(3) > 0) {
            n = rnd.next(190000, 200000);
            t = rnd.next(9000, 10000);
        }
    }

    
    
    int min_zeros = t;
    int max_ones = n - min_zeros;
    int num_ones;
    
    if (seed_val == 1) {
        num_ones = 0;
    } else if (seed_val == 2) {
        num_ones = max_ones;
    } else {
        num_ones = rnd.next(0, max_ones);
    }

    
    vector<int> a(n, 0);
    if (num_ones > 0) {
        vector<int> p(n);
        iota(p.begin(), p.end(), 0);
        shuffle(p.begin(), p.end());
        for (int i = 0; i < num_ones; ++i) {
            a[p[i]] = 1;
        }
    }

    
    Fenwick ft(n);
    for (int i = 0; i < n; ++i) {
        if (a[i] == 1) ft.add(i + 1, 1);
    }

    vector<int> ks;
    int current_zeros = n - num_ones;

    for (int i = 0; i < t; ++i) {
        int k;
        if (seed_val == 3) {
            k = 1;
        } else if (seed_val == 4) {
            k = current_zeros;
        } else {
            
            int r = rnd.next(100);
            if (r < 10) k = 1; 
            else if (r < 20) k = current_zeros; 
            else if (r < 30) k = current_zeros / 2 + 1; 
            else k = rnd.next(1, current_zeros); 
        }
        
        ks.push_back(k);
        
        
        int idx = ft.find_kth_zero(k);
        ft.add(idx, 1);
        current_zeros--;
    }

    
    cout << n << " " << t << "\n";
    
    
    
    
    
    
    if (mode == "non") {
        for (int i = 0; i < n; ++i) {
            cout << a[i] << (i == n - 1 ? "" : " ");
        }
        cout << "\n";
    }
    
    
    for (int i = 0; i < t; ++i) {
        cout << ks[i] << (i == t - 1 ? "" : " ");
    }
    cout << "\n";

    return 0;
}