Time limit
2s
Memory limit
128 MB
Farmer Gilsan manages the fence around a square field. One corner of the field is (0, 0), the opposite corner is (N, N), and every side is parallel to either the x-axis or the y-axis. 2 <= N <= 500000.
Fence posts are placed at the four corners and at every 1-meter interval along the four sides. Corners are counted only once, so there are exactly 4N posts. A post is considered a thin line segment with no thickness.
Inside the field there are R large rocks. 1 <= R <= 30000. Each rock is a column whose bottom and top faces are the same convex polygon, and it is tall enough that Gilsan cannot see any post behind it. Rock regions do not overlap, and they do not touch at vertices or edges. No rock lies inside or on another rock.
Given the field size, Gilsan's position, and the shape of each rock, compute how many fence posts Gilsan can see by only turning his head in place. A post on the same line as Gilsan and any vertex of a rock is considered not visible.
The first line contains N and R.
The second line contains Gilsan's position x y.
Then the descriptions of the R rocks follow. For each rock, the first line contains the number of vertices p, where 3 <= p <= 20. The next p lines contain the vertex coordinates x y in counterclockwise order.
All vertex coordinates are distinct. Several vertices of one rock may be collinear.
Print one line containing the number of fence posts visible from Gilsan's current position.