CodeForces 714D

题面

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 rand 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 x21y21 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

1
2
3
4
5
6
7
8
9
5
2
1
0
1
1
1
0
1

Sample Output

1
2
3
4
5
6
7
8
9
? 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

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

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

代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
#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;
}