codeforces 713B B. Searching Rectangles(二分)

题目链接:

B. 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 rowr 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 "? xyxy2" (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 200queries. 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 y12x21 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.

Example
input
5
2
1
0
1
1
1
0
1
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 题意: 给两个矩形区域,然后你进行不超过200次的询问,然后找出这两个区域; 思路: 先找出这两个矩形的分界线,然后再在两个区域内找两个矩形; AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack>
#include <map> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const int mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=(1<<20)+10;
const int maxn=1e6+110;
const double eps=1e-12; int n,ans[20],cnt=0;
inline int out(int x,int y,int fx,int fy)
{
if(x>fx||y>fy)return 0;
printf("? %d %d %d %d\n",x,y,fx,fy);
fflush(stdout);
int num;
cin>>num;
return num;
}
inline void solve(int x,int y,int fx,int fy)
{
int l=x,r=fx,Ans=x;
while(l<=r)
{
int mid=(l+r)>>1;
int f=out(mid,y,fx,fy);
if(f==0)r=mid-1;
else l=mid+1,Ans=mid;
}
ans[++cnt]=Ans;
l=y,r=fy,Ans=y;
while(l<=r)
{
int mid=(l+r)>>1;
int f=out(x,mid,fx,fy);
if(f==0)r=mid-1;
else l=mid+1,Ans=mid;
}
ans[++cnt]=Ans;
l=x,r=fx,Ans=fx;
while(l<=r)
{
int mid=(l+r)>>1;
int f=out(x,y,mid,fy);
if(f==0)l=mid+1;
else r=mid-1,Ans=mid;
}
ans[++cnt]=Ans;
l=y,r=fy,Ans=fy;
while(l<=r)
{
int mid=(l+r)>>1;
int f=out(x,y,fx,mid);
if(f==0)l=mid+1;
else r=mid-1,Ans=mid;
}
ans[++cnt]=Ans;
}
int main()
{
read(n);
int l=1,r=n,flag=0;
while(l<=r)
{
int mid=(l+r)>>1;
int f=out(1,1,n,mid),g=out(1,mid+1,n,n);
if(f&&g)flag=1;
if(f==0)l=mid+1;
else r=mid-1;
}
if(flag)
{
solve(1,1,n,l);
solve(1,l+1,n,n);
}
else
{
l=1,r=n;
while(l<=r)
{
int mid=(l+r)>>1;
int f=out(1,1,mid,n);
if(f==0)l=mid+1;
else r=mid-1;
}
solve(1,1,l,n);
solve(l+1,1,n,n);
}
printf("! ");
for(int i=1;i<=8;i++)printf("%d ",ans[i]);
printf("\n");
return 0;
}

  

上一篇:Spring MVC中处理静态资源的多种方法


下一篇:小甲鱼第51讲:《__name__="__main__"、搜索路径和包》课后练习题