Under construction

Still being built. Things may change

Pixel Triangles

Time limit2sMemory limit512 MB

Summary
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 P(A,B,C)P(A, B, C) for three positive integers A, B, and C.
  • P(A,B,C)={(x,y)∣A≤x, B≤y, 0≤(x−A)+(y−B)≤C−1}P(A, B, C) = \{ (x, y) \mid A \le x,\ B \le y,\ 0 \le (x-A) + (y-B) \le C-1 \}
  • 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 P(1,2,3),P(3,1,2),P(5,5,1)P(1, 2, 3), P(3, 1, 2), P(5, 5, 1), 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

Examples1

  1. Example 1

    Input
    3
    1 2 3
    3 1 2
    5 5 1
    
    Expected output
    9