Codeforces Round #371 (Div. 2)D. Searching Rectangles交互题

题目大意:Filya在边长为n的正方形里面, 画了两个矩形。这两个矩形的边与正方形的边平行,且两个长方形没有不能相交(边可以重合)。但是Filya忘记他把两个矩形画在正方形的哪边了,所以他问Sonya在以(x1, y1)为左下顶点和以(x2, y2)为右上顶点的矩形里面,完整包含了几个他之前画的矩形, 问的次数不能超过200次。让你来模拟这个过程。你的输入首先是n(正方形的边长),然后由计算机来问你,你的输入就是回答的答案。

方法二分即可,但是搞不懂为什么用cin, cout就不用清空缓冲区,这个题目清空缓冲区的意义何在?

#include
using namespace std;
struct abc
{
    int x1,y1,x2,y2;
    void print(){
        cout<" "<" "<" "<" ";
    }
};
int query(int x1,int y1,int x2,int y2)
{
    if(x1>x2)swap(x1,x2);
    if(y1>y2)swap(y1,y2);
    cout<<"? "<" "<" "<" "<int ans;cin>>ans;
    return ans;
}
abc fi(int xx1,int yy1,int xx2,int yy2)
{
    abc Ans;
    int l=yy1,r=yy2,ans=yy1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(query(xx1,yy1,xx2,mid)<1)
            l=mid+1;
        else
            r=mid-1,ans=mid;
    }
    Ans.y2=ans;
    l=yy1,r=yy2,ans=yy1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(query(xx1,mid,xx2,yy2)<1)
            r=mid-1;
        else
            l=mid+1,ans=mid;
    }
    Ans.y1=ans;

    l=xx1,r=xx2,ans=xx1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(query(xx1,yy1,mid,yy2)<1)
            l=mid+1;
        else
            r=mid-1,ans=mid;
    }
    Ans.x2=ans;
    l=xx1,r=xx2,ans=xx1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(query(mid,yy1,xx2,yy2)<1)
            r=mid-1;
        else
            l=mid+1,ans=mid;
    }
    Ans.x1=ans;
    return Ans;
}
int main()
{
    int n;
    scanf("%d",&n);
    int l=1,r=n,ans=1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(query(1,1,mid,n)<1)
            l=mid+1;
        else
            r=mid-1,ans=mid;
    }
    if(query(1,1,ans,n)==1&&query(ans+1,1,n,n)==1)
    {
        abc r1=fi(1,1,ans,n);
        abc r2=fi(ans+1,1,n,n);
        cout<<"! ";
        r1.print();
        r2.print();
        return 0;
    }
    l=1,r=n,ans=1;
    while(l<=r)
    {
        int mid=(l+r)/2;
        if(query(1,1,n,mid)<1)
            l=mid+1;
        else
            r=mid-1,ans=mid;
    }
    abc r1 = fi(1,1,n,ans);
    abc r2 = fi(1,ans+1,n,n);
    cout<<"! ";
    r1.print();
    r2.print();
}

你可能感兴趣的:(CodeForces)