Score : 600 points
Problem Statement
We have N polygons on the xy-plane.
Every side of these polygons is parallel to the x- or y-axis, and every interior angle is 90 or 270 degrees. All of these polygons are simple.
The i-th polygon has Mi corners, the j-th of which is (xi,j,yi,j).
The sides of this polygon are segments connecting the j-th and (j+1)-th corners. (Assume that (Mi+1)-th corner is the 1-st corner.)
A polygon is simple when...
for any two of its sides that are not adjacent, they do not intersect (cross or touch) each other.
You are given $Q$ queries.
For each $i = 1, 2, \dots, Q$, the $i$-th query is as follows.
- Among the N polygons, how many have the point (Xi+0.5,Yi+0.5) inside them?
Constraints
- 1≤N≤105
- 4≤Mi≤105
- Each Mi is even.
- ∑iMi≤4×105
- 0≤xi,j,yi,j≤105
- (xi,j,yi,j)=(xi,k,yi,k) if j=k.
- xi,j=xi,j+1 for j=1,3,…Mi−1.
- yi,j=yi,j+1 for j=2,4,…Mi. (Assume yi,Mi+1=yi,1.)
- The given polygons are simple.
- 1≤Q≤105
- 0≤Xi,Yi<105
- All values in input are integers.
Input is given from Standard Input in the following format:
N
M1
x1,1 y1,1 x1,2 y1,2 … x1,M1 y1,M1
M2
x2,1 y2,1 x2,2 y2,2 … x2,M2 y2,M2
⋮
MN
xN,1 yN,1 xN,2 yN,2 … xN,MN yN,MN
Q
X1 Y1
X2 Y2
⋮
XQ YQ
Output
Print Q lines.
The i-th line should contain the answer to the i-th query.
3
4
1 2 1 4 3 4 3 2
4
2 5 2 3 5 3 5 5
4
5 6 5 5 3 5 3 6
3
1 4
2 3
4 5
0
2
1
Note that different polygons may cross or touch each other.
2
4
12 3 12 5 0 5 0 3
12
1 1 1 9 10 9 10 0 4 0 4 6 6 6 6 2 8 2 8 7 2 7 2 1
4
2 6
4 4
6 3
1 8
0
2
1
1
Although the polygons are simple, they may not be convex.