CodeForces 714D Searching Rectangles

题面

D. Searching Rectangles

time limit per test: 1 second

memory limit per test: 256 megabytes

input: standard input

output: standard output

Filya just learned new geometry object — rectangle. He is given a field consisting of n × n unit cells. Rows are numbered from bottom to top with integer from 1 to n. Columns are numbered from left to right with integers from 1 to n. Cell, located at the intersection of the row r*and column *c is denoted as (r, c). Filya has painted two rectangles, such that their sides are parallel to coordinate axes and each cell lies fully inside or fully outside each of them. Moreover, no cell lies in both rectangles.

Later, hedgehog Filya became interested in the location of his rectangles but was unable to find the sheet of paper they were painted on. They were taken by Sonya and now she wants to play a little game with Filya. He tells her a query rectangle and she replies with the number of initial rectangles that lie fully inside the given query rectangle. The query rectangle should match the same conditions as initial rectangles. Rectangle lies fully inside the query if each o its cells lies inside the query.

Filya knows Sonya really well, so is sure that if he asks more than 200 questions she will stop to reply.

Input

The first line of the input contains an integer n (2 ≤ n ≤ 216) — size of the field.

For each query an integer between 0 and 2 is returned — the number of initial rectangles that lie fully inside the query rectangle.

Output

To make a query you have to print “? x*1 *y*1 *x*2 *y*2” (without quotes) (1 ≤ *x*1 ≤ *x*2 ≤ *n, 1 ≤ y*1 ≤ *y*2 ≤ *n), where (*x*1, *y*1) stands for the position of the bottom left cell of the query and (*x*2, *y*2) stands for the up right cell of the query. You are allowed to ask no more than 200 queries. After each query you should perform “flush” operation and read the answer.

In case you suppose you’ve already determined the location of two rectangles (or run out of queries) you should print “! *x*11 *y*11 *x*12 *y*12 *x*21*y*21 *x*22 *y*22” (without quotes), where first four integers describe the bottom left and up right cells of the first rectangle, and following four describe the corresponding cells of the second rectangle. You can print the rectangles in an arbitrary order. After you have printed the answer, print the end of the line and perform “flush”. Your program should terminate immediately after it print the answer.

Interaction

To flush you can use (just after printing an integer and end-of-line):

  • fflush(stdout) in C++;
  • System.out.flush() in Java;
  • stdout.flush() in Python;
  • flush(output) in Pascal;
  • See the documentation for other languages.

You will get the Wrong Answer verdict if you ask more than 200 queries, or if you print an incorrect coordinates.

You will get the Idleness Limit Exceeded verdict if you don’t print anything (but you should) or if you forget about flushing the output (more info below).

Hacking.

The first line should contain an integer n (2 ≤ n ≤ 216).

The second line should contain four integers x*1, *y*1, *x*2, *y*2 (1 ≤ *x*1 ≤ *x*2 ≤ *n, 1 ≤ y*1 ≤ *y*2 ≤ *n) — the description of the first rectangle.

The third line contains the description of the second rectangle in the similar way.

Sample Input

5
2
1
0
1
1
1
0
1

Sample Output

? 1 1 5 5
? 1 1 3 3
? 1 1 3 1
? 2 2 2 2
? 3 3 5 5
? 3 3 3 5
? 3 3 3 4
? 3 4 3 5
! 2 2 2 2 3 4 3 5

题目大致是给你两个不相交的矩形,你可以作若干次询问,每次询问一个大矩形,返回被这个大矩形完全覆盖的原矩形个数(0,1,2),最后你需要输出这两个矩形的坐标,询问次数不超过200

(注意需要加fflush,题目要求)

思路

这是一道二分练手好题(我二分写的比较丑。。。看了一下最多询问了180次才得出结果)

先考虑只有一个矩形的情况,不要太简单对不对?分别二分这个矩形的上下左右边界即可

那么相比于一个矩形的情况,两个矩形难在哪里呢?(注意它们并不相交)

仔细想想其实区别并不是很大,只要我们能将两个矩形分开,那么分别当成一个矩形的情况求解就好了~

现在考虑不相交的两个矩形,我们有两把刀分别平行于两个坐标轴,刀每次选取不同的位置将平面切成两块。

显然,至少有一把刀存在一种切法使得平面被切成两块后每块刚好有一个矩形。

(黄色区域表示平面,红色区域表示目标矩形,紫色线条表示切法)

情况1:situation 1

情况2:situation 2

情况3:situation 3

最最最开心的是,这条被切开的线是可以二分的哦~

整道题就是二分二分再二分!!!

代码

#include <iostream>
#include <cstring>
#include <stack>
#include <vector>
#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <cstdio>
#include <cstdlib>
#include <climits>
#include <deque>
#include <bitset>
#include <algorithm>

using namespace std;

const double eps=1e-10;
const double pi=3.1415926535897932384626433832795;
const double eln=2.718281828459045235360287471352;

#define LL long long
#define IN freopen("in.txt", "r", stdin)
#define OUT freopen("out.txt", "w", stdout)
#define scan(x) scanf("%d", &x)
#define scan2(x, y) scanf("%d%d", &x, &y)
#define scan3(x, y, z) scanf("%d%d%d", &x, &y, &z)
#define sqr(x) (x) * (x)
#define pr(x) printf("Case %d: ",x)
#define prn(x) printf("Case %d:\n",x)
#define prr(x) printf("Case #%d: ",x)
#define prrn(x) printf("Case #%d:\n",x)
#define lowbit(x) (x&(-x))

int QA(int x1,int y1,int x2,int y2)
{
    int t;
    printf("? %d %d %d %d\n",x1,y1,x2,y2);
    fflush(stdout);
    scanf("%d",&t);
    return t;
}

int pdud(int n)
{
    int l=1,r=n;
    int mid,ans1,ans2;
    while(r-l>1)
    {
        mid=(l+r)>>1;
        ans1=QA(1,1,mid,n);
        ans2=QA(mid+1,1,n,n);
        if(ans1==1 && ans2==1)return mid;
        if(ans1==2 || ans2==0)r=mid;else l=mid;
    }
    ans1=QA(1,1,l,n);
    ans2=QA(r,1,n,n);
    if(ans1*ans2==1)return l;else return -1;
}

int pdlr(int n)
{
    int l=1,r=n;
    int mid,ans1,ans2;
    while(r-l>1)
    {
        mid=(l+r)>>1;
        ans1=QA(1,1,n,mid);
        ans2=QA(1,mid+1,n,n);
        if(ans1==1 && ans2==1)return mid;
        if(ans1==2 || ans2==0)r=mid;else l=mid;
    }
    ans1=QA(1,1,n,l);
    ans2=QA(1,r,n,n);
    if(ans1*ans2==1)return l;else return -1;
}

int row[2][2],col[2][2];

void find(int r1,int c1,int r2,int c2,int t)
{
    //锁定下边界
    int l=r1,r=r2;
    int mid,ans;
    while(r-l>1)
    {
        mid=(l+r)>>1;
        ans=QA(mid,c1,r2,c2);
        if(ans>0)l=mid;else r=mid;
    }
    ans=QA(r,c1,r2,c2);
    if(ans==1)row[t][0]=r;else row[t][0]=l;

    //锁定上边界
    l=row[t][0],r=r2;
    while(r-l>1)
    {
        mid=(l+r)>>1;
        ans=QA(row[t][0],c1,mid,c2);
        if(ans>0)r=mid;else l=mid;
    }
    ans=QA(row[t][0],c1,l,c2);
    if(ans==1)row[t][1]=l;else row[t][1]=r;

    //锁定左边界
    l=c1,r=c2;
    while(r-l>1)
    {
        mid=(l+r)>>1;
        ans=QA(row[t][0],mid,row[t][1],c2);
        if(ans>0)l=mid;else r=mid;
    }
    ans=QA(row[t][0],r,row[t][1],c2);
    if(ans==1)col[t][0]=r;else col[t][0]=l;

    //锁定右边界
    l=col[t][0],r=c2;
    while(r-l>1)
    {
        mid=(l+r)>>1;
        ans=QA(row[t][0],col[t][0],row[t][1],mid);
        if(ans>0)r=mid;else l=mid;
    }
    ans=QA(row[t][0],col[t][0],row[t][1],l);
    if(ans==1)col[t][1]=l;else col[t][1]=r;

}

int main()
{
    int n;
    scanf("%d",&n);
    int r1=pdud(n);
    if(r1<0)
    {
        int u1=pdlr(n);
        find(1,1,n,u1,0);
        find(1,u1+1,n,n,1);
    }else
    {
        find(1,1,r1,n,0);
        find(r1+1,1,n,n,1);
    }
    printf("! %d %d %d %d",row[0][0],col[0][0],row[0][1],col[0][1]);
    printf(" %d %d %d %d\n",row[1][0],col[1][0],row[1][1],col[1][1]);
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值