Robogolf
Time limit3sMemory limit512 MB
Two robots alternately move a chip right or down across an n by m grid with priced traps, and the task is to sum the game value over every starting square.
- Level
Hard9 of 10
- Topics
- Dynamic programming, Sorting, Math
- Solved
- No attempts yet
Statement
The World Robot Olympiad holds robogolf competitions. The playing field is a rectangle of unit squares. Rows are numbered 1 to , and columns are numbered 1 to . Each square is identified by its row and column .
Two robots take turns moving a special chip across the field. Some squares contain traps. When the chip is on square , the robot whose turn it is can move it to or to . If that square does not exist because the chip is in the last row or the last column, the robot can move the chip off the field. The game ends when the chip is off the field or on a square with a trap.
Each trap has an integer price . The game result is the price of the trap where the game ended, or 0 if the chip left the field. The robot that moves first wants to minimize the result, and the robot that moves second wants to maximize it.
Let the chip start on square . The guaranteed result is the smallest result the first robot can force, no matter how the opponent plays. Since the starting square is unknown in advance, the developers want the sum of over all squares. Given the positions and prices of the traps, compute this sum.
Input
The first line contains three integers , , and (; ), the number of rows, columns, and traps. Each of the next lines contains three integers , , and (, , ), the row, column, and price of the -th trap. No two traps share a square.
Output
Print one integer, the sum of over all squares modulo . The remainder for a possibly negative is the value in such that for some integer . For example, , , , and .
Hint
In the first example, the guaranteed results for all squares are shown in the figure below, with trap squares highlighted.

The sum of the results is . The answer is .
In the second example, the guaranteed results for all squares are shown in the figure below, with trap squares highlighted.

The sum of the results is . The answer is .