Nails
Time limit1sMemory limit128 MB
Given up to 500000 upward triangles on a triangular grid of N nails per side, count the nails covered by at least one triangle.
- Level
Medium7 of 10
- Topics
- Array, Prefix sum, Implementation, Geometry
- Solved
- No attempts yet
Problem
JOI is playing by driving nails into a board. JOI arranges the nails in the shape of an equilateral triangle with nails along each side. Row from the top () contains nails, and the -th nail from the left in that row () is denoted .
An equilateral triangle whose three vertices are nails is called a good triangle if each of its sides is parallel to a side of the whole triangle and it points in the same direction as the whole triangle. In other words, a good triangle is the triangle whose vertices are the three nails , , and , where , , and .
JOI wraps a rubber band around a good triangle. A rubber band wrapped around a good triangle encloses every nail on the boundary of and inside that triangle.
Given the number of nails along one side, the number of rubber bands , and the good triangle wrapped by each rubber band, write a program that computes how many nails are enclosed by at least one rubber band.
Input
The first line contains two integers and , separated by a space. is the number of nails along one side of the triangle, and is the number of rubber bands.
Each of the following lines describes the good triangle wrapped by one rubber band. The -th line () contains three integers , , (, , ), separated by spaces. This means that the -th rubber band wraps the good triangle whose vertices are the nails , , and .
Output
Print, on a single line, the number of nails enclosed by at least one rubber band.
Constraints
- : the number of nails along one side
- () : the number of rubber bands
Hint
The nails on the boundary of and inside the good triangle , , are exactly the following: for each row with , the nails with .
For instance, if and two rubber bands wrap the good triangles and , then every nail except , , and — that is, nails — is enclosed by at least one rubber band.