Donut Drone
Time limit8sMemory limit512 MB
Simulate a drone on a toroidal grid where each step moves to the highest of three rightward neighbors, handling up to 1e9 steps per move query and elevation updates.
- Level
Hard8 of 10
- Topics
- Simulation, Binary search, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
You are building a simulation of a drone that explores a donut shaped planet. The drone moves over a toroidal grid, a rectangular grid that wraps around in both directions. The grid has rows numbered to from top to bottom and columns numbered to from left to right. Every cell has an elevation, and an elevation is a positive integer.

The drone starts in the cell in row and column . In each step the drone looks at three cells: the cell directly to the right, the cell diagonally right and down, and the cell diagonally right and up. Row and column numbers wrap around, so row is below row and column is to the right of column . The drone flies to the cell with the largest elevation among the three.
Two kinds of events happen during the simulation.
move k: the drone takes steps.change a b e: the elevation of the cell in row and column becomes .
Find the position of the drone right after each move event.
At every point in time, any three circularly consecutive cells of one column have pairwise different elevations, so the target of each step is always a single cell.

Input
The first line has two integers and (), the number of rows and the number of columns of the grid. The -th of the next lines has integers (), the initial elevations of the cells in row .
The next line has an integer (), the number of events. The -th of the next lines has the -th event. An event is either move k with , or change a b e with , and .
Output
Print one line for each move event of the input, in the order the events appear. The line for the -th move event has the row number and the column number of the cell where the drone stands right after that event, separated by one space.