Nails

Time limit1sMemory limit128 MB

Summary
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 NN nails along each side. Row aa from the top (1≤a≤N1 \le a \le N) contains aa nails, and the bb-th nail from the left in that row (1≤b≤a1 \le b \le a) is denoted (a,b)(a, b).

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 (a,b)(a, b), (a+x,b)(a + x, b), and (a+x,b+x)(a + x, b + x), where 1≤a<N1 \le a < N, 1≤b≤a1 \le b \le a, and 1≤x≤N−a1 \le x \le N - a.

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 NN along one side, the number of rubber bands MM, 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 NN and MM, separated by a space. NN is the number of nails along one side of the triangle, and MM is the number of rubber bands.

Each of the following MM lines describes the good triangle wrapped by one rubber band. The ii-th line (1≤i≤M1 \le i \le M) contains three integers AiA_i, BiB_i, XiX_i (1≤Ai<N1 \le A_i < N, 1≤Bi≤Ai1 \le B_i \le A_i, 1≤Xi≤N−Ai1 \le X_i \le N - A_i), separated by spaces. This means that the ii-th rubber band wraps the good triangle whose vertices are the nails (Ai,Bi)(A_i, B_i), (Ai+Xi,Bi)(A_i + X_i, B_i), and (Ai+Xi,Bi+Xi)(A_i + X_i, B_i + X_i).

Output

Print, on a single line, the number of nails enclosed by at least one rubber band.

Constraints

  • 2≤N≤50002 \le N \le 5000 : the number of nails along one side
  • 1≤M≤5000001 \le M \le 500000 (=5×105= 5 \times 10^5) : the number of rubber bands

Hint

The nails on the boundary of and inside the good triangle (a,b)(a, b), (a+x,b)(a + x, b), (a+x,b+x)(a + x, b + x) are exactly the following: for each row rr with a≤r≤a+xa \le r \le a + x, the nails (r,c)(r, c) with b≤c≤b+(r−a)b \le c \le b + (r - a).

For instance, if N=5N = 5 and two rubber bands wrap the good triangles (2,2,1)(2, 2, 1) and (2,1,3)(2, 1, 3), then every nail except (1,1)(1, 1), (4,4)(4, 4), and (5,5)(5, 5) — that is, 1212 nails — is enclosed by at least one rubber band.

Examples1

  1. Example 1

    Input
    5 2
    2 2 1
    2 1 3
    
    Expected output
    12