This page is still under construction.

Parts of this page are still being built. What you see may change.

Shower

Time limit2sMemory limit1024 MB

Summary
Rain lowers cell heights over Q days; after each shower, report the lowest cell in the water-connected region containing the shower cell, breaking ties by the earliest rain time.
Level

Hard8 of 10

Topics
Union-find, Simulation, Implementation, Sorting
Solved
No attempts yet

Problem

Sinchon has a plain of size N×MN \times M, called Sinchon Plain. Showers fall on this plain for QQ days. The top-left cell of Sinchon Plain has coordinates (1,1)(1, 1), and the bottom-right cell has coordinates (N,M)(N, M).

A shower falls on cell (a,b)(a, b). After it falls, the ground height decreases by cc and water remains in that cell. Ground heights can be negative.

If adjacent cells contain water, the water in those cells becomes connected. Two cells are adjacent when they share an edge.

After each shower, the Sinchon Plain Management Headquarters installs a water quality inspection robot at that point. The robot moves freely through the connected water and finishes its inspection at the cell with the lowest height. If several cells have the lowest height, it finishes at the cell that received rain the longest ago.

Given the locations where showers fall over QQ days, write a program that prints, for each day, the point where the robot finishes its inspection.

Input

The first line gives NN (1≤N≤1 0001 \le N \le 1\,000), MM (1≤M≤1 0001 \le M \le 1\,000), and QQ (1≤Q≤100 0001 \le Q \le 100\,000), separated by spaces.

Lines 2 to N+1N+1 give the height HH of each cell of Sinchon Plain (0≤Ha,b≤1 0000 \le H_{a, b} \le 1\,000) as integers.

Lines N+2N+2 to N+Q+1N+Q+1 give, in order, the coordinates aa (1≤a≤N1 \le a \le N) and bb (1≤b≤M1 \le b \le M) of the cell where rain falls, and the amount cc (1≤c≤1001 \le c \le 100) by which the ground height decreases, as integers.

Output

For each day, print the coordinates of the point where the robot finishes its inspection, one per line, in order.

Hint

The following is the figure for Sample 1.

Examples2

  1. Example 1

    Input
    2 3 5
    9 9 9
    9 9 9
    1 1 1
    1 2 2
    2 2 2
    1 1 5
    1 2 1
    
    Expected output
    1 1
    1 2
    1 2
    1 1
    1 1
    
  2. Example 2

    Input
    4 5 14
    0 9 9 9 9
    9 9 0 0 0
    0 9 0 0 0
    0 9 9 9 0
    1 4 1
    1 3 1
    1 2 1
    1 5 2
    4 3 1
    4 2 1
    3 2 1
    4 4 2
    2 1 2
    2 2 1
    2 2 1
    1 2 1
    1 2 1
    1 5 1
    
    Expected output
    1 4
    1 4
    1 4
    1 5
    4 3
    4 3
    4 3
    4 4
    2 1
    1 5
    1 5
    1 5
    1 2
    1 2