Problem: cf1552_h
Difficulty: hard
Categories: search, math
Time limit: 1000 ms, memory limit: 256 MB

Let us call a point of the plane admissible if its coordinates are positive integers less than or equal to $200$.

There is an invisible rectangle such that:

* its vertices are all admissible;
* its sides are parallel to the coordinate axes;
* its area is strictly positive.

Your task is to guess the perimeter of this rectangle.

In order to guess it, you may ask at most $4$ queries.

In each query, you choose a nonempty subset of the admissible points and you are told how many of the chosen points are inside or on the boundary of the invisible rectangle.

Interaction

To ask a query (of the kind described in the statement), you shall print two lines:

* In the first line print "? $k$" (without the quotes) where $k$ ($k\ge 1$) is the number of chosen points.
* In the second line print $2k$ integers $x_1,\, y_1,\, x_2,\, y_2,\, \dots,\, x_k,\, y_k$ ($1\le x_i,y_i\le 200$ for $i=1,2,\dots,k$) where $(x_1, y_1),\,(x_2, y_2),\,(x_3, y_3),\, \dots,\,(x_k, y_k)$ are the $k$ distinct admissible chosen points (the order of the points is not important).

After this, you should read an integer — the number of chosen points that are inside or on the boundary of the invisible rectangle.

When you have identified the perimeter $p$ of the invisible rectangle, you must print "! $p$" (without quotes) and terminate your program.

If you ask more than $4$ queries or if one of the queries is malformed, the interactor terminates immediately and your program receives verdict Wrong Answer.

The interactor may be adaptive (i.e., the hidden rectangle may not be chosen before the beginning of the interaction).

After printing a query do not forget to output 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.

Examples

Input

```
13 5 123 80
```

Output

Input

```
2 2 4 4
```

Output

Input

```
1 1 200 200
```

Output

Note

The following is an example of interaction for the first sample intended to show the format of the queries.

 \begin{array}{l|l|l} \text{Query (contestant program)} & \text{Answer (interactor)} & \text{Explanation} \\ \hline \mathtt{?\ 4} & & \text{We choose the $4$ vertices of} \\ \mathtt{13\ 5\ 13\ 80\ 123\ 5\ 123\ 80} & \mathtt{4} &\text{the hidden rectangle.}\\ \hline \mathtt{?\ 5} & & \text{We choose $4$ points just outside the hidden}\\ \mathtt{100\ 4\ 100\ 81\ 12\ 40\ 124\ 40\ 50\ 50} & \mathtt{1}& \text{rectangle and also the point $(50,50)$.}\\ \hline \mathtt{?\ 2} & & \text{We choose the points $(1, 1)$} \\ \mathtt{200\ 200\ 1\ 1} & \mathtt{0} & \text{and $(200,200)$.}\\ \hline \mathtt{!\ 370} & & \text{This is the correct perimeter.} \end{array} 

For the second sample, a possible interaction is the following.

 \begin{array}{l|l|l} \text{Query (contestant program)} & \text{Answer (interactor)} & \text{Explanation} \\ \hline \mathtt{?\ 4} & & \text{We choose the points $(3, 2)$, $(4, 1)$,} \\ \mathtt{3\ 2\ 4\ 1\ 5\ 2\ 4\ 3} & 2 & \text{$(5, 2)$ and $(4, 3)$.} \\ \hline \mathtt{?\ 7} & & \text{We choose the points $(1, 4)$, $(2, 4)$,} \\ \mathtt{1\ 4\ 2\ 4\ 1\ 5\ 2\ 5\ 5\ 5\ 5\ 6\ 6\ 5} & 1 & \text{$(1, 5)$, $(2, 5)$, $(5, 5)$, $(5, 6)$ and $(6, 5)$.} \\ \hline \mathtt{!\ 8} & & \text{This is the correct perimeter.} \end{array}  The situation is shown in the following picture: ![](https://espresso.codeforces.com/48058721d325b8816be56f22859d6ad002db127f.png) 

The green points are the ones belonging to the first query, while the orange points are the ones belonging to the second query. One can see that there are exactly two rectangles consistent with the interactor's answers:

* the rectangle of vertices $(2, 2)$ and $(4, 4)$, shown in red;
* the rectangle of vertices $(4, 2)$ and $(5, 5)$, shown in blue.

Since both of these rectangles have perimeter $8$, this is the final answer.

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

using namespace std;


static long long queries = 0;
static const long long query_limit = 4; 


static int hx1, hy1, hx2, hy2;



static const int HARD_CAP = 50;






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

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

    
    
    
    hx1 = inf.readInt(1, 200, "hx1");
    hy1 = inf.readInt(1, 200, "hy1");
    hx2 = inf.readInt(1, 200, "hx2");
    hy2 = inf.readInt(1, 200, "hy2");

    
    
    if (hx1 > hx2) swap(hx1, hx2);
    if (hy1 > hy2) swap(hy1, hy2);

    
    while (true) {
        
        string token;
        if (!(cin >> token)) {
            
            finish(_wa, "Unexpected EOF: contestant terminated without providing an answer");
        }

        if (token == "!") {
            
            long long p;
            if (!(cin >> p)) {
                finish(_wa, "Expected integer perimeter after '!'");
            }

            
            
            
            
            
            
            long long width = hx2 - hx1;
            long long height = hy2 - hy1;
            long long true_p = 2 * (width + height);

            if (p == true_p) {
                finish(_ok, "Correct perimeter " + to_string(p));
            } else {
                finish(_wa, "Wrong perimeter. Expected " + to_string(true_p) + ", got " + to_string(p));
            }
        } else {
            
            queries++;
            
            
            if (queries % 10 == 0) log_metrics();

            
            if (queries > HARD_CAP) {
                finish(_pe, "Hard query limit exceeded (" + to_string(HARD_CAP) + ")");
            }

            
            int k;
            if (token == "?") {
                
                if (!(cin >> k)) {
                    finish(_wa, "Expected integer k after '?'");
                }
            } else {
                
                try {
                    k = stoi(token);
                } catch (...) {
                    finish(_wa, "Invalid query start token: '" + token + "' (expected '?' or integer k)");
                }
            }

            if (k < 1) {
                finish(_wa, "Invalid number of points k=" + to_string(k) + " (must be >= 1)");
            }

            
            int count = 0;
            for (int i = 0; i < k; ++i) {
                int x, y;
                if (!(cin >> x >> y)) {
                    finish(_wa, "Expected " + to_string(k) + " points, but input ended early");
                }

                
                if (x < 1 || x > 200 || y < 1 || y > 200) {
                    finish(_wa, "Point (" + to_string(x) + "," + to_string(y) + ") is out of admissible range [1, 200]");
                }

                
                if (x >= hx1 && x <= hx2 && y >= hy1 && y <= hy2) {
                    count++;
                }
            }

            
            cout << count << endl;
        }
    }

    return 0;
}


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

using namespace std;


const int MAX_COORD = 200;
const int QUERY_LIMIT = 4;


static long long queries = 0;
static long long query_limit = QUERY_LIMIT;
static bool adaptive = false;

struct Rect {
    int x1, y1, x2, y2; 

    
    
    
    
    int perimeter() const {
        return 2 * ((x2 - x1) + (y2 - y1));
    }

    
    
    
    
    int query_count(const vector<vector<int>>& pref) const {
        return pref[x2][y2] - pref[x1 - 1][y2] - pref[x2][y1 - 1] + pref[x1 - 1][y1 - 1];
    }
};

vector<Rect> pool;

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

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

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

    
    
    int hx1 = inf.readInt();
    int hy1 = inf.readInt();
    int hx2 = inf.readInt();
    int hy2 = inf.readInt();

    
    if (hx1 == 0 && hy1 == 0 && hx2 == 0 && hy2 == 0) {
        adaptive = true;
    } else {
        adaptive = false;
        
        if (hx1 > hx2) swap(hx1, hx2);
        if (hy1 > hy2) swap(hy1, hy2);
        pool.push_back({hx1, hy1, hx2, hy2});
    }

    if (adaptive) {
        
        
        
        int target_size = 300000;
        pool.reserve(target_size + 10);
        
        for(int i = 0; i < target_size; ++i) {
            int rx1 = rnd.next(1, MAX_COORD);
            int rx2 = rnd.next(1, MAX_COORD);
            
            if(rx1 == rx2) {
                if(rx2 < MAX_COORD) rx2++; else rx1--;
            }
            if(rx1 > rx2) swap(rx1, rx2);

            int ry1 = rnd.next(1, MAX_COORD);
            int ry2 = rnd.next(1, MAX_COORD);
            
            if(ry1 == ry2) {
                if(ry2 < MAX_COORD) ry2++; else ry1--;
            }
            if(ry1 > ry2) swap(ry1, ry2);

            pool.push_back({rx1, ry1, rx2, ry2});
        }
        
        
        
        pool.push_back({1, 1, 200, 200});
        
        pool.push_back({1, 1, 2, 2});
        
        pool.push_back({1, 1, 2, 200});
        pool.push_back({1, 1, 200, 2});
    }

    
    while (true) {
        string type;
        
        if (!(cin >> type)) {
            
            finish(_wa, "Unexpected EOF");
        }

        if (type == "!") {
            long long ans;
            if (!(cin >> ans)) finish(_wa, "Expected perimeter after '!'");
            
            if (pool.empty()) {
                finish(_wa, "No consistent rectangles left (contestant forced invalid state)");
            }
            
            
            for (const auto& r : pool) {
                if (r.perimeter() != ans) {
                    string s = "Wrong answer. Found consistent rectangle with perimeter " + to_string(r.perimeter()) 
                             + ", but contestant guessed " + to_string(ans);
                    finish(_wa, s);
                }
            }
            finish(_ok, "Correct perimeter " + to_string(ans));
            
        } else if (type == "?") {
            queries++;
            
            int k;
            if (!(cin >> k)) finish(_wa, "Expected k after '?'");
            if (k < 1) finish(_wa, "k must be >= 1");
            
            
            
            static int grid[MAX_COORD + 2][MAX_COORD + 2];
            
            for(int i=0; i<=MAX_COORD; ++i) 
                memset(grid[i], 0, sizeof(grid[i]));
                
            for(int i=0; i<k; ++i) {
                int x, y;
                if (!(cin >> x >> y)) finish(_wa, "Expected coordinates");
                if (x < 1 || x > MAX_COORD || y < 1 || y > MAX_COORD) {
                    finish(_wa, "Coordinate out of range");
                }
                grid[x][y] = 1; 
            }

            
            if (queries > query_limit) {
                finish(_wa, "Query limit exceeded");
            }
            
            
            vector<vector<int>> pref(MAX_COORD + 2, vector<int>(MAX_COORD + 2, 0));
            for(int i=1; i<=MAX_COORD; ++i) {
                for(int j=1; j<=MAX_COORD; ++j) {
                    pref[i][j] = grid[i][j] + pref[i-1][j] + pref[i][j-1] - pref[i-1][j-1];
                }
            }
            
            int chosen_ans = -1;
            
            if (!adaptive) {
                chosen_ans = pool[0].query_count(pref);
            } else {
                
                
                
                
                
                map<int, int> counts;
                for(const auto& r : pool) {
                    counts[r.query_count(pref)]++;
                }
                
                int max_rem = -1;
                for(auto const& [ans, cnt] : counts) {
                    if (cnt > max_rem) {
                        max_rem = cnt;
                        chosen_ans = ans;
                    } else if (cnt == max_rem) {
                        
                        if (chosen_ans == -1 || ans < chosen_ans) {
                            chosen_ans = ans;
                        }
                    }
                }
                
                
                int new_size = 0;
                for(int i=0; i<(int)pool.size(); ++i) {
                    if (pool[i].query_count(pref) == chosen_ans) {
                        pool[new_size++] = pool[i];
                    }
                }
                pool.resize(new_size);
            }
            
            
            cout << chosen_ans << endl; 
            log_metrics();
            
        } else {
            finish(_wa, "Invalid query type");
        }
    }
    
    return 0;
}


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");

    
    
    
    if (mode == "adp") {
        cout << "0 0 0 0" << endl;
        return 0;
    }

    
    
    
    const int N = 200;

    auto print_rect = [&](int x1, int y1, int x2, int y2) {
        if (x1 > x2) swap(x1, x2);
        if (y1 > y2) swap(y1, y2);
        cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl;
    };

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

    if (seedVal >= 1 && seedVal <= 10) {
        
        if (seedVal == 1) {
            
            print_rect(1, 1, N, N);
        } else if (seedVal == 2) {
            
            print_rect(100, 100, 101, 101);
        } else if (seedVal == 3) {
            
            print_rect(1, 1, N, 2);
        } else if (seedVal == 4) {
            
            print_rect(1, 1, 2, N);
        } else if (seedVal == 5) {
            
            print_rect(10, 10, 138, 12);
        } else if (seedVal == 6) {
            
            print_rect(10, 10, 137, 12);
        } else if (seedVal == 7) {
            
            print_rect(1, 1, 129, 129);
        } else if (seedVal == 8) {
            
            print_rect(1, 1, 200, 200);
        } else if (seedVal == 9) {
            
            print_rect(N-2, N-2, N, N);
        } else {
            
            if (rnd.next(2) == 0) {
                int y = rnd.next(1, N - 1);
                vector<int> x = rnd.distinct(2, 1, N);
                print_rect(x[0], y, x[1], y + 1);
            } else {
                int x = rnd.next(1, N - 1);
                vector<int> y = rnd.distinct(2, 1, N);
                print_rect(x, y[0], x + 1, y[1]);
            }
        }
    } else {
        
        int type = rnd.next(1, 100);
        if (type <= 60) {
            
            vector<int> x = rnd.distinct(2, 1, N);
            vector<int> y = rnd.distinct(2, 1, N);
            print_rect(x[0], y[0], x[1], y[1]);
        } else if (type <= 80) {
            
            int w = rnd.next(N / 2, N - 1);
            int h = rnd.next(N / 2, N - 1);
            int x1 = rnd.next(1, N - w);
            int y1 = rnd.next(1, N - h);
            print_rect(x1, y1, x1 + w, y1 + h);
        } else {
            
            int w = rnd.next(1, 20);
            int h = rnd.next(1, 20);
            int x1 = rnd.next(1, N - w);
            int y1 = rnd.next(1, N - h);
            print_rect(x1, y1, x1 + w, y1 + h);
        }
    }

    return 0;
}