- P209's solution
-
P209's Solution
- @ 2025-12-10 21:51:03
考虑依次选取以下 个「关键营地」(需要指出的是,这 个「关键营地」不一定都存在):
- 号「关键营地」为输入的第一个营地。
- 号「关键营地」为输入的第一个与 号「关键营地」 坐标相同, 坐标不同的营地。
- 号「关键营地」为输入的第一个与 号「关键营地」 坐标不同, 坐标相同的营地。
- 号「关键营地」为输入的第一个与 号「关键营地」 坐标不同, 坐标不同的营地。
- 号「关键营地」为输入的第一个与 号「关键营地」 坐标不同, 坐标不同,与 号「关键营地」 坐标相同, 坐标不同的营地。
- 号「关键营地」为输入的第一个与 号「关键营地」 坐标不同, 坐标不同,与 号「关键营地」 坐标不同, 坐标相同的营地。
- 号「关键营地」为输入的第一个与 号「关键营地」 坐标不同, 坐标不同,与 号「关键营地」 坐标不同, 坐标不同的营地。
可以证明,如果某个询问有解,则这 个「关键营地」中必有一个是该询问的合法解,证明此处从略。
最终的时间复杂度为 ,空间复杂度为 。
#include <bits/stdc++.h>
using namespace std;
inline int read()
{
int x = 0;
char c = getchar_unlocked();
while (c < '0') c = getchar_unlocked();
while (c >= '0')
{
x = x * 10 + c - '0';
c = getchar_unlocked();
}
return x;
}
void write(int x)
{
if (x <= 9)
{
putchar_unlocked(x + '0');
return;
}
write(x / 10);
putchar_unlocked(x % 10 + '0');
}
inline void write_solution(int x)
{
write(x);
putchar_unlocked('\n');
}
inline void write_no_solution()
{
putchar_unlocked('-');
putchar_unlocked('1');
putchar_unlocked('\n');
}
struct Point
{
int x, y, i;
};
Point p[7];
int main()
{
freopen("laser.in", "r", stdin);
freopen("laser.out", "w", stdout);
int n = read(), q = read();
p[0] = (Point) {read(), read(), 1};
for (int i = 2; i <= n; i++)
{
int x = read(), y = read();
if (x == p[0].x && y != p[0].y) p[1] = (Point) {x, y, i};
if (x != p[0].x && y == p[0].y) p[2] = (Point) {x, y, i};
if (x != p[0].x && y != p[0].y)
{
if (!p[3].i)
{
p[3] = (Point) {x, y, i};
continue;
}
if (x == p[3].x && y != p[3].y) p[4] = (Point) {x, y, i};
if (x != p[3].x && y == p[3].y) p[5] = (Point) {x, y, i};
if (x != p[3].x && y != p[3].y) p[6] = (Point) {x, y, i};
}
}
while (q--)
{
int x = read(), y = read();
for (int i = 0; i <= 7; i++)
{
if (i == 7)
{
write_no_solution();
break;
}
if (x != p[i].x && y != p[i].y && p[i].i)
{
write_solution(p[i].i);
break;
}
}
}
return 0;
}