Cf1153e service and snake [construction]

Keywords: PHP

Title Link: Luo Gu

NTF told me this question a long time ago. Now I want to do it...

We find that if both ends of the snake are inside or outside the rectangle, the query is even, otherwise it is odd.

So if we ask each row and column, we can know the horizontal and vertical coordinates of the two ends of the snake.

But there is a situation that can't be solved, that is, two ends are on the same row or column (only the same row is considered below), but they must not be in the same column, so you can find the column where they are, and then find the row where they are through dichotomy.

The specific implementation can be seen in the code.

 1 #include<bits/stdc++.h>
 2 #define Rint register int
 3 using namespace std;
 4 inline int read(){
 5     int ch = getchar(), res = 0;
 6     while(ch < '0' || ch > '9') ch = getchar();
 7     while(ch >= '0' && ch <= '9'){
 8         res = res * 10 + ch - '0';
 9         ch = getchar();
10     }
11     return res;
12 }
13 int n, cnt;
14 pair<int, int> res[4];
15 inline int query(int a1, int b1, int a2, int b2){
16     printf("? %d %d %d %d\n", a1, b1, a2, b2);
17     fflush(stdout);
18     return read() & 1;
19 }
20 inline int solve1(int x){
21     int l = 1, r = n, mid;
22     while(l < r){
23         mid = l + r >> 1;
24         if(query(x, l, x, mid)) r = mid;
25         else l = mid + 1;
26     }
27     return l;
28 }
29 inline int solve2(int x){
30     int l = 1, r = n, mid;
31     while(l < r){
32         mid = l + r >> 1;
33         if(query(l, x, mid, x)) r = mid;
34         else l = mid + 1;
35     }
36     return l;
37 }
38 int main(){
39     n = read();
40     for(Rint i = 1;i <= n;i ++)
41         if(query(i, 1, i, n)) res[cnt ++] = make_pair(i, solve1(i));
42     if(!cnt){
43         for(Rint i = 1;i <= n;i ++)
44             if(query(1, i, n, i)){
45                 if(!cnt) res[cnt ++] = make_pair(solve2(i), i);
46                 else res[cnt ++] = make_pair(res[0].first, i);
47             }
48     }
49     printf("! %d %d %d %d\n", res[0].first, res[0].second, res[1].first, res[1].second);
50     fflush(stdout);
51 }
CF1153E

Posted by IceRegent on Fri, 01 Nov 2019 11:21:07 -0700