考虑依次选取以下 77 个「关键营地」(需要指出的是,这 77 个「关键营地」不一定都存在):

  • 00 号「关键营地」为输入的第一个营地。
  • 11 号「关键营地」为输入的第一个与 00 号「关键营地」xx 坐标相同,yy 坐标不同的营地。
  • 22 号「关键营地」为输入的第一个与 00 号「关键营地」xx 坐标不同,yy 坐标相同的营地。
  • 33 号「关键营地」为输入的第一个与 00 号「关键营地」xx 坐标不同,yy 坐标不同的营地。
  • 44 号「关键营地」为输入的第一个与 00 号「关键营地」xx 坐标不同,yy 坐标不同,与 33 号「关键营地」xx 坐标相同,yy 坐标不同的营地。
  • 55 号「关键营地」为输入的第一个与 00 号「关键营地」xx 坐标不同,yy 坐标不同,与 33 号「关键营地」xx 坐标不同,yy 坐标相同的营地。
  • 66 号「关键营地」为输入的第一个与 00 号「关键营地」xx 坐标不同,yy 坐标不同,与 33 号「关键营地」xx 坐标不同,yy 坐标不同的营地。

可以证明,如果某个询问有解,则这 77 个「关键营地」中必有一个是该询问的合法解,证明此处从略。

最终的时间复杂度为 O(n+q)O(n + q),空间复杂度为 O(1)O(1)

#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;
}