Pixel Triangles
Time limit2sMemory limit512 MB
Given up to four million right isosceles triangles on a 2000x2000 grid, count the total number of grid cells covered by at least one triangle.
- Level
Hard8 of 10
- Topics
- Prefix sum, Matrix, Implementation, Math
- Solved
- No attempts yet
Problem
A 2000×2000 grid is given, and each pixel (cell) is identified by its row and column. The leftmost column is column 1, and the topmost row is row 1.
A pixel triangle is defined as follows.
- The pixel in row x, column y is written as (x, y), and a pixel triangle consists of several pixels.
- A pixel triangle is written as for three positive integers A, B, and C.
- Every pixel making up a pixel triangle on the grid is guaranteed to lie inside the grid.
Given n pixel triangles made of grid pixels, print the number of pixels the triangles cover.
Different pixel triangles may share the same pixel.
For example, when n = 3 and the three given pixel triangles are , the state of the grid is depicted below.

Figure: A portion of the grid
The number of pixels covered in the end is 9.
Input
The first line gives the number of pixel triangles n. Each of the following n lines gives three integers A, B, C describing one pixel triangle.
Output
Print the number of covered pixels.
Constraints
- 1 ≤ n ≤ 4,000,000
- 1 ≤ A, B, C ≤ 2,000
- 1 ≤ A + C - 1, B + C - 1 ≤ 2,000