Codeforces Round #551 (Div. 2) E. Serval and Snake

5 篇文章 0 订阅
2 篇文章 0 订阅

E. Serval and Snake

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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×nn×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 44.

Today Serval got up too late and only have time to make 20192019 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 00, 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 nn (2≤n≤10002≤n≤1000) — the size of the grid.

Output

When you are ready to answer, you should print ! x1 y1 x2 y2, where (x1,y1)(x1,y1) represents the position of the head and (x2,y2)(x2,y2)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≤x1≤x2≤n1≤x1≤x2≤n, 1≤y1≤y2≤n1≤y1≤y2≤n), representing a rectangle consisting of all cells (x,y)(x,y) such that x1≤x≤x2x1≤x≤x2 and y1≤y≤y2y1≤y≤y2. 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−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−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.

Hacks

To make a hack, print a single integer nn (2≤n≤10002≤n≤1000) in the first line, indicating the size of the grid.

Then print an integer kk (2≤k≤n22≤k≤n2) in the second line, indicating the length of the snake.

In the next kk lines, print kk pairs of integers xi,yixi,yi (1≤xi,yi≤n1≤xi,yi≤n), each pair in a single line, indicating the ii-th cell of snake, such that the adjacent pairs are adjacent, and all kk pairs are distinct.

Examples

input

Copy

2

1

0

0

output

Copy

? 1 1 1 1

? 1 2 1 2

? 2 2 2 2

! 1 1 2 1

input

Copy

3

2

0

output

Copy

? 2 2 2 2

? 2 1 2 3

! 2 1 2 3

Note

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

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

 

题意:

告诉你在方格中有一条蛇,你可以通过2019次查询任意大小的矩形,然后可以得到矩形穿过蛇身的次数,

让你找到蛇的头尾分别是什么。 

思路:

首先我们可以知道,查询任意矩形,如果矩形里面有蛇的头或者有尾,那么超过矩形的次数一定是奇数,

如果是偶数,可以证明蛇身不在矩形里,或者头和尾都在矩形里。

然后就开始查询每一列先,其实查询每一列的奇偶性,不用 1000次, 999就够了,因为最后一列的奇偶性可以由前面的和推出来,因为整体所有的奇偶性肯定是偶数,所以自己脑补吧。

在同一列:这样就还需要另外查询一下所有的行,然后在同一列,肯定不再相同的行,接着再二分找到的两个奇数行,可以得出答案,最坏查询就是 999 * 2 + 10 + 10 = 2018次 

不在同一列:证明找到两列奇数的列,头和尾肯定分别在这两列里,接着你可以二分这两列里头和尾在的位置,因为前n项肯定

是有奇偶性的 ,稍微想一下就知道了。

 

代码:

#include <bits/stdc++.h>
#include <time.h>
#define fi first
#define se second
#define endll "\n"
#define MS0(X) memset((X), 0, sizeof((X)))
#define MS1(X) memset((X), -1, sizeof((X)))
#define LEN(X) strlen(X)
///vector(len,val);
using namespace std;

typedef long long ll;
typedef double db;
int xx[4] = {1,-1,0,0};
int yy[4] = {0,0,1,-1};
const double eps = 1e-9;
typedef pair<int,int>  P;
const int maxn = 2e6 + 5000;
const ll mod = 1e9 + 7;
inline int sign(db a) { return a < -eps ? -1 : a > eps;}
//inline int cmp(db a,db b){ return sign(a - b);}
void debug(int x){  cout << x << endl; }
ll mul(ll a,ll b,ll c) { ll res = 1; while(b) {  if(b & 1) res *= a,res %= c;  a *= a,a %= c,b >>= 1;  }  return res;}
ll phi(ll x) {  ll res = x;  for(ll i = 2; i * i <= x; i++) { if(x % i == 0) res = res / i * (i - 1);   while(x % i == 0) x /= i;   }  if(x > 1) res = res / x  * (x - 1);    return res;}
template <typename A, typename B> inline bool chmin(A &a, B b){if(a > b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline bool chmax(A &a, B b){if(a < b) {a = b; return 1;} return 0;}
template <typename A, typename B> inline ll add(A x, B y) {if(x + y < 0) return x + y + mod; return x + y >= mod ? x + y - mod : x + y;}
template <typename A, typename B> inline void add2(A &x, B y) {if(x + y < 0) x = x + y + mod; else x = (x + y >= mod ? x + y - mod : x + y);}
template <typename A, typename B> inline ll mul1(A x, B y) {return 1ll * x * y % mod;} /// x * y % mod;
template <typename A, typename B> inline void mul2(A &x, B y) {x = (1ll * x * y % mod + mod) % mod;} /// return x * y
template <typename A> inline void debug(A a){cout << a << '\n';}
template <typename A> inline ll sqr(A x){return 1ll * x * x;}
template <typename A> A inv(A x) {return mul(x, mod - 2);}
inline ll read() { char c = getchar(); ll x = 0, f = 1; while(c < '0' || c > '9') {if(c == '-') f = -1; c = getchar();}while(c >= '0' && c <= '9') x = 1ll * x * 10 + c - '0', c = getchar();return x * f;}
int fa[maxn];
int Find(int x) {   if(x != fa[x]) return fa[x] = Find(fa[x]);  return fa[x];}
int n,k;
bool check(int x,int y,int op){
    if(op)///查询行
        cout << '?' << " " << x << " " << 1 << " " << x << " " << y << endl;
    else ///查询列
        cout << '?' << " " << 1 << " " << x << " " << y << " " << x << endl;
    cin >> x;
    if(x == -1) return false;
    if(x & 1) return true;
    return false;
}
vector<P>anss;
void solve(int x,int op){
    int l = 1,r = n,id = 0;
    while(l <= r){

        int mid = (l + r) / 2;
        if(mid <= 0) break;
        if(check(x,mid,op)){
            r = mid - 1;
            id = mid;
        }else{
            l = mid + 1;
        }
    }
    if(op)
        anss.push_back(P(x,id));
    else
        anss.push_back(P(id,x));
    return ;
}
int ans[maxn];
int main() {
//    ios::sync_with_stdio(false);
    while(cin >> n){
            int s = 0;
        for(int i = 1;i < n;i++){
            cout << '?' << " " << 1 << " " << i << " " << n << " " << i << endl;///查询列
            cin >> ans[i];
            s += ans[i];
        }
        ans[n] = s;
        vector<int>v;
        for(int i = 1;i <= n;i++){
            if(ans[i] & 1) v.push_back(i);
        }
        if(v.size() == 2){///不在同一列
            for(auto d:v)
                solve(d,0);
        }else{
            s = 0;
            for(int i = 1;i < n;i++){
                cout << '?' << " " << i << " " << 1 << " " << i << " " << n << endl;
                cin >> ans[i];
                s += ans[i];
            }
            ans[n] = s;
            v.clear();
            for(int i = 1;i <= n;i++){
                if(ans[i] & 1) v.push_back(i);
            }
            for(auto d:v)
                solve(d,1);
        }
        cout <<"! ";
        for(auto d:anss){
            cout << d.fi << " " << d.se <<" ";
        }
    }
    cerr << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
    return 0;
}

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值