NOI Contest Archive

Archive / 2026 / Final Round — Day 2

DVD Screensaver (Again)

Medium · noi-2026-day-2

During the qualifier round, you spent hours watching a classic DVD screensaver bounce around a television screen. Watching a single logo bounce forever got boring, so you decided to make things more interesting. Now, you can launch multiple DVDs, each with its own assigned color (not necessarily different).

DVD Screensaver

The screen is a rectangle with integer coordinates from \((0,0)\) to \((N,M)\), inclusive. Every point with integer coordinates on this grid initially has color \(0\).

A DVD logo is treated as a single point. When a DVD is launched, it starts from an integer coordinate \((x,y)\) that lies exactly on at least one boundary of the screen (\(x = 0\), \(x = N\), \(y = 0\), or \(y = M\)) and moves in one of the four diagonal directions:

The logo moves perfectly along its diagonal path. As it arrives at any integer coordinate point (including its starting point), it paints that point with its assigned color. If a point is traversed by multiple logos, its color is overwritten by the most recent traversal.

When the logo reaches a boundary of the screen, it instantly bounces. A bounce simply means the logo shifts to the perpendicular diagonal trajectory (for example, changing from a North-East to a South-East trajectory if hitting the top boundary \(y = M\)), ensuring its path always remains at a 45-degree angle to the edges. If the logo hits exactly in one of the four corners, it reverses its trajectory completely and travels back along the exact same diagonal line it arrived on.

We track the logo by the number of boundary bounces it completes. A bounce is counted the moment the logo touches any boundary line. Note that the logo’s initial starting position on a boundary does not count as a bounce. (Touching a corner during its travel counts as exactly one bounce).

You are given \(Q\) queries and you must process them in the given order. There are two types of queries:

Input Format

The first line contains three space-separated integers \(N\), \(M\), and \(Q\), representing the screen dimensions and the number of queries.

The next \(Q\) lines each describe a query in the format specified above.

Output Format

For each Type 2 query, print a single integer on a new line representing the current color of the queried point.

Constraints

Subtasks

Subtask 1 (11 points)

Subtask 2 (14 points)

Subtask 3 (20 points)

Subtask 4 (22 points)

Subtask 5 (33 points)

Sample Input 0

5 5 4
1 0 2 1 2 7
2 2 4
1 5 3 4 1 9
2 4 2

Sample Output 0

7
9

Sample Input 1

4 4 3
1 0 2 1 1 7
2 1 3
2 0 0

Sample Output 1

7
0