CF371 D Searching Rectangles

基本思路就是二分 每条边分别二分求
c++11 用fflush(sdtout) 不行 囧啊

#include<bits/stdc++.h>
using namespace std;

int ans[2][4];
int inp;
int check(int x1,int y1,int x2,int y2) {
    printf("? %d %d %d %d\n",x1,y1,x2,y2);
    fflush(stdout);
    scanf("%d",&inp);
    return inp;
}
int has(int x1,int y1,int x2,int y2) {
    if(x1 <= ans[0][0] && y1 <= ans[0][1] && x2 >= ans[0][2] && y2 >= ans[0][3]) return 1;
    return 0;
}
void solve(int x1,int y1,int x2,int y2,int flag){
    int l=x1, r=x2;  
    while(l<=r){
        int mid = (l+r)>>1;
        int num = check(mid,y1,x2,y2);
        if(flag &&  has(mid,y1,x2,y2) ) num--;
        if(num==1 || num==2)
            ans[flag][0] = mid, l = mid+1;
        else
            r = mid-1;
    }
    x1 = ans[flag][0];
//  printf("x1:%d\n",x1);

    l=x1,r=x2;
    while(l<=r){
        int mid = (l+r)>>1;
        int num = check(x1,y1,mid,y2);
        if( flag && has(x1,y1,mid,y2) ) num--;
        if(num==1 || num==2)
            ans[flag][2]=mid, r = mid-1;
        else
            l = mid+1;
    }
    x2 = ans[flag][2];
//  printf("x2;%d\n",x2);

    l=y1,r=y2;
    while(l<=r){
        int mid = (l+r)>>1;
        int num = check(x1,mid,x2,y2);
        if(flag &&  has(x1,mid,x2,y2) ) num--;
        if(num==1 || num==2)
            ans[flag][1]=mid, l = mid+1;
        else
            r=mid-1;
    }
    y1 = ans[flag][1];
//  printf("y1:%d\n",y1);

    l=y1,r=y2;
    while(l<=r){
        int mid = (l+r)>>1;
        int num = check(x1,y1,x2,mid);
        if(flag &&  has(x1,y1,x2,mid) ) num--;
        if(num==1 || num==2)
            ans[flag][3]=mid, r=mid-1;
        else
            l=mid+1;
    }
    y2 = ans[flag][3];
//  printf("y2:%d\n",y2);
}

int main(){
    int n;
    while(~scanf("%d",&n)) {
        solve(1,1,n,n,0);
        solve(1,1,n,n,1);

        printf("!");
        for(int i = 0; i < 2; ++i) {
            for(int j = 0; j < 4; ++j) {
                printf(" %d",ans[i][j]);
            } 
        }printf("\n"); fflush(stdout);
    }
    return 0;
}
posted @ 2016-09-16 17:29  basasuya  阅读(181)  评论(0编辑  收藏  举报