Problem: cf1153_e
Difficulty: hard
Categories: bit, search
Time limit: 1000 ms, memory limit: 256 MB

This is an interactive problem.

Now Serval is a senior high school student in Japari Middle School. However, on the way to the school, he must go across a pond, in which there is a dangerous snake. The pond can be represented as a $n \times n$ grid. The snake has a head and a tail in different cells, and its body is a series of adjacent cells connecting the head and the tail without self-intersecting. If Serval hits its head or tail, the snake will bite him and he will die.

Luckily, he has a special device which can answer the following question: you can pick a rectangle, it will tell you the number of times one needs to cross the border of the rectangle walking cell by cell along the snake from the head to the tail. The pictures below show a possible snake and a possible query to it, which will get an answer of $4$.

 ![](https://espresso.codeforces.com/95652ffad46f36bf8dda20ad0e718e2278b1096b.png)  ![](https://espresso.codeforces.com/fc4f7627830cbe45738e7543a046cae6f1f858da.png) 

Today Serval got up too late and only have time to make $2019$ queries. As his best friend, can you help him find the positions of the head and the tail?

Note that two cells are adjacent if and only if they have a common edge in the grid, and a snake can have a body of length $0$, that means it only has adjacent head and tail.

Also note that the snake is sleeping, so it won't move while Serval using his device. And what's obvious is that the snake position does not depend on your queries.

Input

The first line contains a single integer $n$ ($2\leq n \leq 1000$) — the size of the grid.

Output

When you are ready to answer, you should print ! x1 y1 x2 y2, where $(x_1, y_1)$ represents the position of the head and $(x_2,y_2)$ represents the position of the tail. You can print head and tail in any order.

Interaction

To make a query, you should print ? x1 y1 x2 y2 ($1 \leq x_1 \leq x_2 \leq n$, $1\leq y_1 \leq y_2 \leq n$), representing a rectangle consisting of all cells $(x,y)$ such that $x_1 \leq x \leq x_2$ and $y_1 \leq y \leq y_2$. You will get a single integer as the answer.

After printing a query, 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.

Answer $-1$ instead of a valid answer means that you made an invalid query or exceeded the maximum number of queries. Exit immediately after receiving $-1$ and you will see Wrong answer verdict. Otherwise you can get an arbitrary verdict because your solution will continue to read from a closed stream.

If your program cannot find out the head and tail of the snake correctly, you will also get a Wrong Answer verdict.

Examples

Input

```
2

1

0

0
```

Output

```
? 1 1 1 1

? 1 2 1 2

? 2 2 2 2

! 1 1 2 1
```

Input

```
3

2

0
```

Output

```
? 2 2 2 2

? 2 1 2 3

! 2 1 2 3
```

Note

 ![](https://espresso.codeforces.com/e99f68d2bd9d49b9944f0e1f2441a371b15c9231.png)  ![](https://espresso.codeforces.com/c2d364b093a2d4e05b4e085cd1690a6d69954840.png)  ![](https://espresso.codeforces.com/4ddccf6f38769989691d1f0ea9ad6066929a41fe.png)  ![](https://espresso.codeforces.com/3d1ed786478ab08f799467e9cc458aa2de75696b.png) 

The pictures above show our queries and the answers in the first example. We first made a query for $(1,1)$ and got an answer $1$, then found that it must be connected to exactly one other cell. Then we made a query for $(1,2)$ and got an answer of $0$, then knew that the snake never entered it. So the cell connected to $(1,1)$ must be $(2,1)$. Then we made a query for $(2,2)$ and got an answer $0$, then knew that it never entered $(2,2)$ as well. So the snake cannot leave $(2,1)$, which implies that the answer is $(1,1)$ and $(2,1)$.

 ![](https://espresso.codeforces.com/0576a29dc4d6548b5fd63f165ecfad387b220334.png)  ![](https://espresso.codeforces.com/8fb34e3007b66c3443c47b37b5c8e14f0ed1b33b.png)  ![](https://espresso.codeforces.com/e73a4b234db4f1566ef5d032a8f83bab23fb2273.png) 

The pictures above show our queries and the answers in the second example. By making query to $(2,2)$ and receiving $2$, we found that the snake occupies $(2,2)$. And by making query to rectangle from $(2,1)$ to $(2,3)$ and receiving answer $0$, we knew that it never goes out of the rectangle from $(2,1)$ to $(2,3)$. Since the first answer is $2$, both $(2,1)$ and $(2,3)$ must be occupied but none of others, so the answer is $(2,1)$ and $(2,3)$.

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 void log_metrics() {
    try {
        tout << "queries=" << queries << "\n";
        tout << "query_limit=" << query_limit << "\n";
        tout.flush();
    } catch (...) {
        
    }
}

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

static bool parseLongLongStrict(const string& s, long long& out) {
    if (s.empty()) return false;
    int i = 0;
    bool neg = false;
    if (s[i] == '+' || s[i] == '-') {
        neg = (s[i] == '-');
        i++;
        if (i == (int)s.size()) return false;
    }
    long long val = 0;
    for (; i < (int)s.size(); i++) {
        char c = s[i];
        if (c < '0' || c > '9') return false;
        int d = c - '0';
        if (val > (LLONG_MAX - d) / 10) return false;
        val = val * 10 + d;
    }
    out = neg ? -val : val;
    return true;
}

static bool readToken(string& tok) {
    tok.clear();
    if (!(cin >> tok)) return false;
    return true;
}

static bool readIntToken(long long& x) {
    string s;
    if (!readToken(s)) return false;
    return parseLongLongStrict(s, x);
}

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

    int n = 0;
    int m = 0;

    try {
        n = inf.readInt(2, 1000, "n");
        m = inf.readInt(2, n * n, "m");
    } catch (...) {
        query_limit = 2019;
        log_metrics();
        finish(_pe, "invalid hidden input header");
    }

    query_limit = 2019;
    log_metrics(); 

    vector<pair<int,int>> snake;
    snake.reserve(m);
    try {
        for (int i = 0; i < m; i++) {
            int x = inf.readInt(1, n, "x");
            int y = inf.readInt(1, n, "y");
            snake.push_back({x, y});
        }
    } catch (...) {
        finish(_pe, "invalid hidden input snake");
    }

    
    for (int i = 0; i + 1 < m; i++) {
        int x1 = snake[i].first, y1 = snake[i].second;
        int x2 = snake[i + 1].first, y2 = snake[i + 1].second;
        int dist = abs(x1 - x2) + abs(y1 - y2);
        if (dist != 1) finish(_pe, "hidden snake is not a simple adjacent path");
    }

    
    
    
    const int VCols = max(0, n - 1);
    const int HRows = max(0, n - 1);

    vector<int> prefV((size_t)VCols * (n + 1), 0); 
    vector<int> prefH((size_t)HRows * (n + 1), 0); 

    auto idxV = [&](int c, int x) -> size_t { 
        return (size_t)(c - 1) * (n + 1) + (size_t)x;
    };
    auto idxH = [&](int r, int y) -> size_t { 
        return (size_t)(r - 1) * (n + 1) + (size_t)y;
    };

    for (int i = 0; i + 1 < m; i++) {
        int x1 = snake[i].first, y1 = snake[i].second;
        int x2 = snake[i + 1].first, y2 = snake[i + 1].second;
        if (x1 == x2) {
            int c = min(y1, y2);
            if (1 <= c && c <= n - 1) prefV[idxV(c, x1)] += 1;
        } else if (y1 == y2) {
            int r = min(x1, x2);
            if (1 <= r && r <= n - 1) prefH[idxH(r, y1)] += 1;
        } else {
            finish(_pe, "hidden snake has non-grid edge");
        }
    }

    
    for (int c = 1; c <= n - 1; c++) {
        for (int x = 1; x <= n; x++) {
            prefV[idxV(c, x)] += prefV[idxV(c, x - 1)];
        }
    }
    for (int r = 1; r <= n - 1; r++) {
        for (int y = 1; y <= n; y++) {
            prefH[idxH(r, y)] += prefH[idxH(r, y - 1)];
        }
    }

    auto sumV = [&](int c, int x1, int x2) -> long long { 
        if (c < 1 || c > n - 1) return 0;
        if (x1 > x2) return 0;
        return (long long)prefV[idxV(c, x2)] - (long long)prefV[idxV(c, x1 - 1)];
    };
    auto sumH = [&](int r, int y1, int y2) -> long long { 
        if (r < 1 || r > n - 1) return 0;
        if (y1 > y2) return 0;
        return (long long)prefH[idxH(r, y2)] - (long long)prefH[idxH(r, y1 - 1)];
    };

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

    long long hard_cap = max(200000LL, 10LL * query_limit);

    while (true) {
        string op;
        if (!readToken(op)) finish(_pe, "unexpected EOF (no final answer)");

        if (op == "?") {
            long long X1, Y1, X2, Y2;
            if (!readIntToken(X1) || !readIntToken(Y1) || !readIntToken(X2) || !readIntToken(Y2)) {
                cout << -1 << "\n";
                cout.flush();
                finish(_pe, "malformed query arguments");
            }

            queries++;
            if (queries > hard_cap) {
                cout << -1 << "\n";
                cout.flush();
                finish(_pe, "hard cap exceeded");
            }

            if (!(1 <= X1 && X1 <= X2 && X2 <= n && 1 <= Y1 && Y1 <= Y2 && Y2 <= n)) {
                cout << -1 << "\n";
                cout.flush();
                finish(_pe, "invalid query rectangle");
            }

            int x1 = (int)X1, y1 = (int)Y1, x2 = (int)X2, y2 = (int)Y2;

            long long ans = 0;
            if (y1 > 1) ans += sumV(y1 - 1, x1, x2);
            if (y2 < n) ans += sumV(y2, x1, x2);
            if (x1 > 1) ans += sumH(x1 - 1, y1, y2);
            if (x2 < n) ans += sumH(x2, y1, y2);

            cout << ans << "\n";
            cout.flush();
        } else if (op == "!") {
            long long X1, Y1, X2, Y2;
            if (!readIntToken(X1) || !readIntToken(Y1) || !readIntToken(X2) || !readIntToken(Y2)) {
                finish(_pe, "malformed answer arguments");
            }
            if (!(1 <= X1 && X1 <= n && 1 <= Y1 && Y1 <= n && 1 <= X2 && X2 <= n && 1 <= Y2 && Y2 <= n)) {
                finish(_wa, "answer coordinates out of bounds");
            }

            pair<int,int> a = {(int)X1, (int)Y1};
            pair<int,int> b = {(int)X2, (int)Y2};
            pair<int,int> head = snake.front();
            pair<int,int> tail = snake.back();

            if ((a == head && b == tail) || (a == tail && b == head)) {
                finish(_ok, "correct");
            } else {
                finish(_wa, "incorrect head/tail");
            }
        } else {
            finish(_pe, "unknown operation token");
        }
    }
}


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 void log_metrics() {
    try {
        tout << "queries=" << queries << '\n';
        tout << "query_limit=" << query_limit << '\n';
        tout.flush();
    } catch (...) {
        
    }
}

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

static inline bool inRect(int x, int y, int x1, int y1, int x2, int y2) {
    return x1 <= x && x <= x2 && y1 <= y && y <= y2;
}

static inline int serpentineX(int n, int idx0) { return idx0 / n + 1; }
static inline int serpentineY(int n, int idx0) {
    int x = serpentineX(n, idx0);
    int pos = idx0 % n; 
    if (x & 1) return pos + 1;
    return n - pos;
}

static inline int serpentineIdx(int n, int x, int y) {
    
    if (x & 1) return (x - 1) * n + (y - 1);
    return (x - 1) * n + (n - y);
}

static inline long long popcount_vec(const vector<uint64_t>& a) {
    long long s = 0;
    for (uint64_t w : a) s += __builtin_popcountll(w);
    return s;
}

static inline long long popcount_and(const vector<uint64_t>& a, const vector<uint64_t>& b) {
    long long s = 0;
    size_t W = a.size();
    for (size_t i = 0; i < W; i++) s += __builtin_popcountll(a[i] & b[i]);
    return s;
}

static inline void set_bit(vector<uint64_t>& bs, int idx) {
    bs[(unsigned)idx >> 6] |= (1ULL << (idx & 63));
}

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

    int n = inf.readInt(2, 1000, "n");
    int m = inf.readInt(2, n * n, "m");
    vector<pair<int,int>> cells;
    cells.reserve(m);
    for (int i = 0; i < m; i++) {
        int x = inf.readInt(1, n, "x");
        int y = inf.readInt(1, n, "y");
        cells.push_back({x, y});
    }

    
    
    int edges = m - 1;
    if (edges <= 0) finish(_pe, "bad hidden input: m < 2");

    query_limit = 2019;
    log_metrics(); 

    long long hard_cap = max(200000LL, 10LL * query_limit);

    
    bool isSerpentineFull = (m == n * n);
    if (isSerpentineFull) {
        for (int i = 0; i < m; i++) {
            int ex = serpentineX(n, i);
            int ey = serpentineY(n, i);
            if (cells[i].first != ex || cells[i].second != ey) {
                isSerpentineFull = false;
                break;
            }
        }
    }

    
    size_t W = (edges + 63) / 64;
    uint64_t lastMask = (edges % 64 == 0) ? ~0ULL : ((1ULL << (edges % 64)) - 1ULL);

    vector<uint64_t> cand(W, ~0ULL);
    cand.back() &= lastMask;
    long long cand_count = edges;

    cout << n << endl; 
    

    string cmd;
    while (true) {
        if (!(cin >> cmd)) {
            finish(_wa, "unexpected EOF from contestant");
        }

        if (cmd == "?") {
            long long x1ll, y1ll, x2ll, y2ll;
            if (!(cin >> x1ll >> y1ll >> x2ll >> y2ll)) finish(_pe, "malformed query arguments");

            if (x1ll < 1 || y1ll < 1 || x2ll < 1 || y2ll < 1 ||
                x1ll > x2ll || y1ll > y2ll || x2ll > n || y2ll > n) {
                finish(_pe, "query rectangle out of range");
            }
            int x1 = (int)x1ll, y1 = (int)y1ll, x2 = (int)x2ll, y2 = (int)y2ll;

            queries++;
            if (queries > hard_cap) finish(_pe, "too many queries (hard cap)");

            vector<uint64_t> trans(W, 0ULL); 

            if (isSerpentineFull) {
                
                for (int x = x1; x <= x2; x++) {
                    if (x & 1) {
                        if (y1 > 1) {
                            int i = (x - 1) * n + (y1 - 2);
                            if (0 <= i && i < edges) set_bit(trans, i);
                        }
                        if (y2 < n) {
                            int i = (x - 1) * n + (y2 - 1);
                            if (0 <= i && i < edges) set_bit(trans, i);
                        }
                    } else {
                        if (y2 < n) {
                            int i = (x - 1) * n + (n - (y2 + 1));
                            if (0 <= i && i < edges) set_bit(trans, i);
                        }
                        if (y1 > 1) {
                            int i = (x - 1) * n + (n - y1);
                            if (0 <= i && i < edges) set_bit(trans, i);
                        }
                    }
                }

                
                for (int x = 1; x <= n - 1; x++) {
                    int c = (x & 1) ? n : 1;
                    bool inside1 = inRect(x, c, x1, y1, x2, y2);
                    bool inside2 = inRect(x + 1, c, x1, y1, x2, y2);
                    if (inside1 != inside2) {
                        int i = x * n - 1;
                        if (0 <= i && i < edges) set_bit(trans, i);
                    }
                }
            } else {
                
                bool prev = inRect(cells[0].first, cells[0].second, x1, y1, x2, y2);
                for (int i = 0; i < edges; i++) {
                    bool cur = inRect(cells[i + 1].first, cells[i + 1].second, x1, y1, x2, y2);
                    if (prev != cur) set_bit(trans, i);
                    prev = cur;
                }
            }

            trans.back() &= lastMask;

            long long count1 = popcount_and(cand, trans);
            long long count0 = cand_count - count1;

            int ans = 0;
            if (count1 > count0) ans = 1; 

            if (ans == 1) {
                for (size_t i = 0; i < W; i++) cand[i] &= trans[i];
            } else {
                for (size_t i = 0; i < W; i++) cand[i] &= ~trans[i];
                cand.back() &= lastMask;
            }

            cand_count = popcount_vec(cand);
            if (cand_count <= 0) finish(_wa, "no feasible hidden instances remain (internal)");

            cout << ans << endl;
        } else if (cmd == "!") {
            long long axll, ayll, bxll, byll;
            if (!(cin >> axll >> ayll >> bxll >> byll)) finish(_pe, "malformed final answer arguments");
            if (axll < 1 || axll > n || ayll < 1 || ayll > n || bxll < 1 || bxll > n || byll < 1 || byll > n) {
                finish(_pe, "final answer out of range");
            }
            int ax = (int)axll, ay = (int)ayll, bx = (int)bxll, by = (int)byll;
            if (ax == bx && ay == by) finish(_pe, "final answer uses identical cells");

            
            bool any = false;
            for (size_t wi = 0; wi < W; wi++) {
                uint64_t w = cand[wi];
                while (w) {
                    int b = __builtin_ctzll(w);
                    int i = (int)(wi * 64 + b);
                    w &= (w - 1);
                    if (i < 0 || i >= edges) continue;

                    any = true;
                    auto p = cells[i];
                    auto q = cells[i + 1];

                    bool ok =
                        (p.first == ax && p.second == ay && q.first == bx && q.second == by) ||
                        (p.first == bx && p.second == by && q.first == ax && q.second == ay);

                    if (!ok) finish(_wa, "wrong head/tail for at least one feasible instance");
                }
            }
            if (!any) finish(_wa, "no feasible instances at final check");

            finish(_ok, "OK");
        } else {
            finish(_pe, "invalid command token");
        }
    }
}


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

static vector<pair<int,int>> buildSerpentine(int n) {
    vector<pair<int,int>> p;
    p.reserve(1LL * n * n);
    for (int i = 1; i <= n; i++) {
        if (i & 1) {
            for (int j = 1; j <= n; j++) p.push_back({i, j});
        } else {
            for (int j = n; j >= 1; j--) p.push_back({i, j});
        }
    }
    return p;
}

static vector<pair<int,int>> takeSegment(const vector<pair<int,int>>& p, int l, int r, bool rev) {
    vector<pair<int,int>> s;
    s.reserve(r - l + 1);
    if (!rev) {
        for (int i = l; i <= r; i++) s.push_back(p[i]);
    } else {
        for (int i = r; i >= l; i--) s.push_back(p[i]);
    }
    return s;
}

int main(int argc, char** argv) {
    registerGen(argc, argv, 1);

    string mode = opt<string>("mode", "non");
    int seed = 1;
    if (argc >= 2) seed = atoi(argv[1]);

    int nOpt = opt<int>("n", 1000);
    int n = max(2, min(1000, nOpt));

    if (mode == "adp") {
        
        
        vector<pair<int,int>> base = buildSerpentine(n);
        cout << n << "\n";
        cout << (int)base.size() << "\n";
        for (auto &c : base) cout << c.first << " " << c.second << "\n";
        return 0;
    }

    
    vector<pair<int,int>> snake;

    if (seed == 1) {
        n = 2;
        snake = {{1, 1}, {2, 1}}; 
    } else if (seed == 2) {
        n = 3;
        
        snake = {
            {2, 2}, {2, 1}, {1, 1}, {1, 2}, {1, 3}, {2, 3}, {3, 3}, {3, 2}, {3, 1}
        };
    } else if (seed == 3) {
        n = 1000;
        snake = buildSerpentine(n); 
    } else {
        vector<pair<int,int>> base = buildSerpentine(n);
        int N = (int)base.size();

        int style = rnd.next(0, 99);
        bool rev = (rnd.next(0, 1) == 1);

        if (N == 2) {
            snake = base;
        } else if (style < 60) {
            
            int maxTrim = min(5000, N - 2);
            int leftTrim = rnd.next(0, maxTrim);
            int rightTrim = rnd.next(0, maxTrim);

            int l = leftTrim;
            int r = N - 1 - rightTrim;
            if (r - l + 1 < 2) {
                l = 0;
                r = N - 1;
            }
            snake = takeSegment(base, l, r, rev);
        } else if (style < 80) {
            
            int maxLen = min(50, N);
            int len = rnd.next(2, maxLen);
            int l = rnd.next(0, N - len);
            int r = l + len - 1;
            snake = takeSegment(base, l, r, rev);
        } else {
            
            int centerL = N / 3;
            int centerR = (2 * N) / 3;

            int wiggle = min(20000, N / 6);
            int l = max(0, centerL - rnd.next(0, wiggle));
            int r = min(N - 1, centerR + rnd.next(0, wiggle));
            if (r - l + 1 < 2) {
                l = 0;
                r = N - 1;
            }
            snake = takeSegment(base, l, r, rev);
        }
    }

    cout << n << "\n";
    cout << (int)snake.size() << "\n";
    for (auto &c : snake) cout << c.first << " " << c.second << "\n";
    return 0;
}