#5395. Problem 3. Photoshoot

0

Problem 3. Photoshoot

Problem 3. Photoshoot

USACO 2026 First Contest, Bronze

Farmer John is looking at his cows in a magical field and wants to take pictures of subsets of his cows.

The field can be seen as a N×NN \times N grid (1N500)(1 \leq N \leq 500), with a single stationary cow at each location. Farmer John's camera is capable of taking a picture of any K×KK \times K square that is part of the field (1Kmin(N,25))(1 \leq K \leq \min(N, 25)).

At all times, each cow has a beauty value between 00 and 10610^6. The attractiveness index of a picture is the sum of the beauty values of the cows contained in the picture.

The beauty value for every cow starts out as 00, so the attractiveness index of any picture in the beginning is 00.

At QQ times (1Q3104)(1 \leq Q \leq 3\cdot 10^{4}), the beauty of a single cow will increase by a positive integer due to eating the magical grass that is planted on Farmer John's field.

Farmer John wants to know the maximum attractiveness index of a picture he can take after each of the QQ updates.

INPUT FORMAT (input arrives from the terminal / stdin):

The first line contains integers NN and KK.

The following line contains an integer QQ.

Each of the following QQ lines contains three integers: rr, cc, and vv, which are the row, column, and new beauty value, respectively (1r,cN,1v1061 \leq r, c \leq N, 1 \leq v \leq 10^6). It is guaranteed that the new beauty value is greater than the beauty value at that location before.

OUTPUT FORMAT (print output to the terminal / stdout):

Output QQ lines, corresponding to the maximum attractiveness index of a picture after each update.

SAMPLE INPUT:


4 2
3
2 2 11
3 4 3
3 1 100

SAMPLE OUTPUT:


11
11
111

After the first update, a picture with the maximum attractiveness index is the picture with upper left corner (2,2)(2, 2) and lower right corner (3,3)(3, 3), which has an attractiveness index of 11+0+0+0=1111 + 0 + 0 + 0 = 11.

The second update does not affect the maximum attractiveness index.

After the third update, the picture with the maximum attractiveness index changes to the picture with upper left corner (2,1)(2, 1) and lower right corner (3,2)(3, 2), which has an attractiveness index of 0+11+100+0=1110 + 11 + 100 + 0 = 111.

SAMPLE INPUT:


3 1
3
2 2 3
2 2 5
2 2 7

SAMPLE OUTPUT:


3
5
7

There is only one cow with a positive beauty value, so the maximum attractiveness index will always include that cow.

SCORING: Inputs 3-6: N50,Q100N \leq 50, Q \leq 100Inputs 7-10: N50N \leq 50Inputs 11-18: No additional constraints.

Problem credits: Brian Law and Cici Liu