D. Searching Rectangles
题目连接:
http://codeforces.com/contest/714/problem/D
Description
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 “? x1 y1 x2 y2” (without quotes) (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ n), where (x1, y1) stands for the position of the bottom left cell of the query and (x2, y2) 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 “! x11 y11 x12 y12 x21 y21 x22 y22” (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 x1, y1, x2, y2 (1 ≤ x1 ≤ x2 ≤ n, 1 ≤ y1 ≤ y2 ≤ 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
Hint
题意
交互题,平面上有俩矩形,不会相交。
你每次可以问一个区域里面能够完全包含多少个矩形。
然后让你找到这俩矩形。
题解:
给你一个矩形区域,然后找到里面的一个矩形,这个二分就好了。
由于俩矩形不会相交,那我首先二分到那个分界线,然后每个区域里面再进行二分就好了。
代码
#include<bits/stdc++.h>
using namespace std;
struct abc
{
int x1,y1,x2,y2;
void print(){
cout<<x1<<" "<<y1<<" "<<x2<<" "<<y2<<" ";
}
};
int query(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;
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();
}