#abc224e. E - Integers on Grid

E - Integers on Grid

Score : 500500 points

问题描述

我们有一个包含 HH 行(水平方向)和 WW 列(垂直方向)的网格。令 (i,j)(i, j) 表示从上到下的第 ii 行以及从左到右的第 jj 列的方格。

每个方格内都有一个整数。对于 i=1,2,,Ni = 1, 2, \ldots, N,方格 (ri,ci)(r_i, c_i) 内含一个正整数 aia_i。其余方格内则包含一个 00

初始时,棋子位于方格 (R,C)(R, C) 上。Takahashi 可以将棋子移动到当前位置以外的任何方格上,任意多次。但是,在移动棋子时,必须同时满足以下两个条件:

  • 移动至的新方格上的整数严格大于移动自的原方格上的整数。
  • 移动过程中涉及的两个方格必须在同一行或同一列上。

对于 i=1,2,,Ni = 1, 2, \ldots, N,请打印当 (R,C)=(ri,ci)(R, C) = (r_i, c_i) 时,Takahashi 最多可以移动棋子多少次。

以上为通义千问 qwen-max 翻译,仅供参考。

Problem Statement

We have a grid with HH horizontal rows and WW vertical columns. Let (i,j)(i, j) denote the square at the ii-th row from the top and jj-th column from the left.

Each square contains an integer. For each i=1,2,,Ni = 1, 2, \ldots, N, the square (ri,ci)(r_i, c_i) contains a positive integer aia_i. The other square contains a 00.

Initially, there is a piece on the square (R,C)(R, C). Takahashi can move the piece to a square other than the square it occupies now, any number of times. However, when moving the piece, both of the following conditions must be satisfied.

  • The integer written on the square to which the piece is moved is strictly greater than the integer written on the square from which the piece is moved.
  • The squares to and from which the piece is moved are in the same row or the same column.

For each i=1,2,,Ni = 1, 2, \ldots, N, print the maximum number of times Takahashi can move the piece when (R,C)=(ri,ci)(R, C) = (r_i, c_i).

Constraints

  • 2H,W2×1052 \leq H, W \leq 2 \times 10^5
  • 1Nmin(2×105,HW)1 \leq N \leq \min(2 \times 10^5, HW)
  • 1riH1 \leq r_i \leq H
  • 1ciW1 \leq c_i \leq W
  • 1ai1091 \leq a_i \leq 10^9
  • ij(ri,ci)(rj,cj)i \neq j \Rightarrow (r_i, c_i) \neq (r_j, c_j)
  • All values in input are integers.

Input

Input is given from Standard Input in the following format:

HH WW NN

r1r_1 c1c_1 a1a_1

r2r_2 c2c_2 a2a_2

\vdots

rNr_N cNc_N aNa_N

Output

Print NN lines. For each i=1,2,,Ni = 1, 2, \ldots, N, the ii-th line should contain the maximum number of times Takahashi can move the piece when (R,C)=(ri,ci)(R, C) = (r_i, c_i).

Sample Input 1

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

Sample Output 1

1
0
2
0
3
1
0

The grid contains the following integers.

4 7 0
3 0 5
2 5 5
  • When (R,C)=(r1,c1)=(1,1)(R, C) = (r_1, c_1) = (1, 1), you can move the piece once by moving it as (1,1)(1,2)(1, 1) \rightarrow (1, 2).
  • When (R,C)=(r2,c2)=(1,2)(R, C) = (r_2, c_2) = (1, 2), you cannot move the piece at all.
  • When (R,C)=(r3,c3)=(2,1)(R, C) = (r_3, c_3) = (2, 1), you can move the piece twice by moving it as (2,1)(1,1)(1,2)(2, 1) \rightarrow (1, 1) \rightarrow (1, 2).
  • When (R,C)=(r4,c4)=(2,3)(R, C) = (r_4, c_4) = (2, 3), you cannot move the piece at all.
  • When (R,C)=(r5,c5)=(3,1)(R, C) = (r_5, c_5) = (3, 1), you can move the piece three times by moving it as $(3, 1) \rightarrow (2, 1) \rightarrow (1, 1) \rightarrow (1, 2)$.
  • When (R,C)=(r6,c6)=(3,2)(R, C) = (r_6, c_6) = (3, 2), you can move the piece once by moving it as (3,2)(1,2)(3, 2) \rightarrow (1, 2).
  • When (R,C)=(r7,c7)=(3,3)(R, C) = (r_7, c_7) = (3, 3), you cannot move the piece at all.

Sample Input 2

5 7 20
2 7 8
2 6 4
4 1 9
1 5 4
2 2 7
5 5 2
1 7 2
4 6 6
1 4 1
2 1 10
5 6 9
5 3 3
3 7 9
3 6 3
4 3 4
3 3 10
4 2 1
3 5 4
1 2 6
4 7 9

Sample Output 2

2
4
1
5
3
6
6
2
7
0
0
4
1
5
3
0
5
2
4
0

update @ 2024/3/10 09:51:41