WKT Geofence Lookup
A fleet-tracking system stores its geofences in a tiny subset of
Well-Known Text (WKT).
Each fence is either a POLYGON (one outer ring plus zero or more holes) or a MULTIPOLYGON
(several such polygons grouped under one name). You must parse the fences and then, for each query
point, report which fences contain it — and whether the point lands exactly on a fence's boundary.
Input
The first line contains two integers f and q (1 ≤ f ≤ 30, 1 ≤ q ≤ 1000): the number of
fences and the number of query points.
Each of the next f lines defines one fence, matching this grammar (WS is zero or more spaces
and/or tabs, WS+ is one or more):
line := WS name WS "=" WS geometry WS
name := letter (letter | digit | "_")* (1 to 20 characters)
geometry := "POLYGON" WS ringList
| "MULTIPOLYGON" WS multiBody
ringList := "(" WS ring (WS "," WS ring)* WS ")"
multiBody := "(" WS ringList (WS "," WS ringList)* WS ")"
ring := "(" WS point (WS "," WS point)* WS ")"
point := integer WS+ integer
integer := "-"? digit+ (no leading zeros, except "0" itself)
letterisa–zorA–Z;digitis0–9.- Whitespace (spaces and tabs) may appear before or after
=, the keyword, any(,)or,, and at the start or end of the line — except that thexandyof a single point are always separated by at least one space or tab (that separator is mandatory, since otherwise the two numbers couldn't be told apart). POLYGONandMULTIPOLYGONare written exactly like that, in upper case.- Every ring has at least 4 points, and its first and last points are identical (rings are always closed explicitly in the input — you don't need to verify or add this).
- In a
POLYGON, the first ring is the outer boundary and every later ring is a hole cut out of it. In aMULTIPOLYGON, eachringListis one such polygon (outer ring plus its own holes, possibly none), and the fence is the union of all of them. - Coordinates are integers with absolute value at most 10^6.
- Fence names consist only of letters, digits and underscores, start with a letter, are at most 20 characters long, and are distinct across the whole input.
- Every ring is simple (its edges don't cross or touch except at shared endpoints of consecutive
edges). A polygon's holes lie inside its outer ring and don't overlap each other or the outer
boundary (though a hole may touch the outer ring or another hole, sharing single points or whole
edges). The polygons making up one
MULTIPOLYGONnever overlap each other. - The total number of points over every ring of every fence (each ring's repeated closing point counts once per occurrence) is at most 2000.
Each of the next q lines contains two integers x and y (|x|, |y| ≤ 10^6), separated by a
single space: one query point.
Output
For each query point, in input order, print one line. A fence contains the point if the point is
inside the fenced area or lies exactly on any of its ring boundaries (outer ring or a hole's edge);
being on any boundary is reported as BOUNDARY even when the point would otherwise be strictly
inside, and a point strictly inside a hole (not on the hole's edge) counts as outside that fence,
since holes remove area.
Print the number of fences that contain the point, followed by name:STATUS for each of those
fences — in the order the fences were defined in the input — where STATUS is INSIDE or
BOUNDARY. Separate all tokens on the line with single spaces. If no fence contains the point,
print just 0.
Notes
In the first example, Pen is a 10×10 square. The point (5, 5) is strictly inside it, (0, 5)
lies on its left edge, (10, 10) is a corner (still BOUNDARY), and (15, 5) is outside.
In the second example, Ring is a 10×10 square with a 3×3 hole square inside it, and Split is a
MULTIPOLYGON made of two disjoint squares far from Ring. (5, 5) sits strictly inside Ring's
hole and outside both of Split's squares, so no fence contains it. (3, 5) lies exactly on the
hole's left edge, a BOUNDARY hit on Ring. (8, 8) is inside Ring (outside the hole) but
misses both of Split's squares. (16, 16) is inside the second square of Split, even though
Ring and the first square of Split both miss it.