ICPC Practice Contest 2021 - A: A Classic Problem
Xem PDFClassic problems in competitive programming are problems frequently appearing in programming contests. This problem is also a very classic one in the field of computational geometry.
It asks to determine which points are covered by a given simple polygon.
To state the problem precisely, we first give the definition of a polygon.
A polygon of \(n\) edges is described by \(n\) straight line segments \(e_1, e_2, \dots, e_n\) connected to form a closed chain.
That is, there exists a sequence of vertices \(v_1 = (x_1, y_1), v_2 = (x_2, y_2), \dots, v_n = (x_n, y_n)\) such that:
- \(e_n = v_n v_1\)
- \(e_i = v_i v_{i+1}\) for \(1 \le i < n\)
So a polygon can be defined by the sequence of vertices \(v_1, v_2, \dots, v_n\).
A simple polygon is a polygon that does not intersect itself. That is, any two line segments may only meet at their endpoints.
Therefore, a simple polygon encloses a region called its interior.
A point \(p\) is covered by a simple polygon \(P\) if and only if:
- \(p\) lies on some edge of \(P\), or
- \(p\) lies inside the interior of \(P\).
Task
You are given:
- A simple polygon with \(n\) edges defined by a sequence of vertices.
- \(m\) points on the \(2D\) plane.
Determine for each point whether it is covered by the given simple polygon.
Input
The first line contains two space-separated positive integers:
- \(n\) — number of edges of the simple polygon
- \(m\) — number of query points
Then \(n + m\) lines follow.
For \(1 \le i \le n\), the \((i+1)\)-th line contains two space-separated integers \(x_i\) and \(y_i\), where \(v_i = (x_i, y_i)\).
For \(1 \le j \le m\), the \((1 + n + j)\)-th line contains two space-separated integers \(X_j\) and \(Y_j\), where \(p_j = (X_j, Y_j)\).
Output
Output \(m\) lines. On the \(j\)-th line:
- Print
YESif \(p_j\) is covered by the polygon. - Otherwise, print
NO.
Constraints
- \(3 \le n \le 10^5\)
- \(1 \le m \le 10^5\)
- The sequence \(v_1, v_2, \dots, v_n\) always defines a simple polygon.
- \(x_i, y_i \in [-10^9, 10^9]\) for \(1 \le i \le n\)
- \(X_j, Y_j \in [-10^9, 10^9]\) for \(1 \le j \le m\)
Example
Test 1
Input
3 3
0 0
5 5
5 0
1 1
1 2
2 1
Output
YES
NO
YES
Test 2
Input
4 4
0 0
1 1
2 0
1 5
1 0
1 1
1 2
1 3
Output
NO
YES
YES
YES
Bình luận