X Aura( 2024-2025 ICPC Asia Jakarta Regional Contest )

X Aura( 2024-2025 ICPC Asia Jakarta Regional Contest (Unrated, Online Mirror, ICPC Rules, Teams Preferred)

Mount ICPC can be represented as a grid of R R R rows (numbered from 1 1 1 to R R R) and C C C columns (numbered from 1 1 1 to C C C). The cell located at row r r r and column c c c is denoted as ( r , c ) (r, c) (r,c) and has a height of H r , c H_{r, c} Hr,c. Two cells are adjacent to each other if they share a side. Formally, ( r , c ) (r, c) (r,c) is adjacent to ( r − 1 , c ) (r-1, c) (r1,c), ( r + 1 , c ) (r+1, c) (r+1,c), ( r , c − 1 ) (r, c-1) (r,c1), and ( r , c + 1 ) (r, c+1) (r,c+1), if any exists.

You can move only between adjacent cells, and each move comes with a penalty. With an aura of an odd positive integer X X X, moving from a cell with height h 1 h_1 h1 to a cell with height h 2 h_2 h2 gives you a penalty of ( h 1 − h 2 ) X (h_1 - h_2)^X (h1h2)X. Note that the penalty can be negative.

You want to answer Q Q Q independent scenarios. In each scenario, you start at the starting cell ( R s , C s ) (R_s, C_s) (Rs,Cs) and you want to go to the destination cell ( R f , C f ) (R_f, C_f) (Rf,Cf) with minimum total penalty. In some scenarios, the total penalty might become arbitrarily small; such a scenario is called invalid. Find the minimum total penalty to move from the starting cell to the destination cell, or determine if the scenario is invalid.

Input

The first line consists of three integers R R R C C C X X X ( 1 ≤ R , C ≤ 1000 ; 1 ≤ X ≤ 9 ; X 1 \leq R, C \leq 1000; 1 \leq X \leq 9; X 1R,C1000;1X9;X is an odd integer).

Each of the next R R R lines consists of a string H r H_r Hr of length C C C. Each character in H r H_r Hr is a number from 0 to 9. The c c c-th character of H r H_r Hr represents the height of cell ( r , c ) (r, c) (r,c), or H r , c H_{r, c} Hr,c.

The next line consists of an integer Q Q Q ( 1 ≤ Q ≤ 100   000 ) 1 \leq Q \leq 100\,000) 1Q100000).

Each of the next Q Q Q lines consists of four integers R s R_s Rs C s C_s Cs R f R_f Rf C f C_f Cf ( 1 ≤ R s , R f ≤ R ; 1 ≤ C s , C f ≤ C 1 \leq R_s, R_f \leq R; 1 \leq C_s, C_f \leq C 1Rs,RfR;1Cs,CfC).

Output

For each scenario, output the following in a single line. If the scenario is invalid, output INVALID. Otherwise, output a single integer representing the minimum total penalty to move from the starting cell to the destination cell.

Examples

Input

Copy

3 4 1
3359
4294
3681
5
1 1 3 4
3 3 2 1
2 2 1 4
1 3 3 2
1 1 1 1

Output

Copy

2
4
-7
-1
0

Input

Copy

2 4 5
1908
2023
2
1 1 2 4
1 1 1 1

Output

Copy

INVALID
INVALID

Input

Copy

3 3 9
135
357
579
2
3 3 1 1
2 2 2 2

Output

Copy

2048
0

Note

Explanation for the sample input/output #1

For the first scenario, one of the solutions is to move as follows: ( 1 , 1 ) → ( 2 , 1 ) → ( 3 , 1 ) → ( 3 , 2 ) → ( 3 , 3 ) → ( 3 , 4 ) (1, 1) \rightarrow (2, 1) \rightarrow (3, 1) \rightarrow (3, 2) \rightarrow (3, 3) \rightarrow (3, 4) (1,1)(2,1)(3,1)(3,2)(3,3)(3,4). The total penalty of this solution is ( 3 − 4 ) 1 + ( 4 − 3 ) 1 + ( 3 − 6 ) 1 + ( 6 − 8 ) 1 + ( 8 − 1 ) 1 = 2 (3 - 4)^1 + (4 - 3)^1 + (3 - 6)^1 + (6 - 8)^1 + (8 - 1)^1 = 2 (34)1+(43)1+(36)1+(68)1+(81)1=2.

Explanation for the sample input/output #2

For the first scenario, the cycle ( 1 , 1 ) → ( 2 , 1 ) → ( 2 , 2 ) → ( 1 , 2 ) → ( 1 , 1 ) (1, 1) \rightarrow (2, 1) \rightarrow (2, 2) \rightarrow (1, 2) \rightarrow (1, 1) (1,1)(2,1)(2,2)(1,2)(1,1) has a penalty of ( 1 − 2 ) 5 + ( 2 − 0 ) 5 + ( 0 − 9 ) 5 + ( 9 − 1 ) 5 = − 26250 (1 - 2)^5 + (2 - 0)^5 + (0 - 9)^5 + (9 - 1)^5 = -26250 (12)5+(20)5+(09)5+(91)5=26250. You can keep repeating this cycle to make your total penalty arbitrarily small. Similarly, for the second scenario, you can move to ( 1 , 1 ) (1, 1) (1,1) first, then repeat the same cycle.

题目大意

题目描述了一个格子地图,每个格子有一个高度。在这张地图上,你可以在相邻的格子之间移动,每次移动会产生一个惩罚,惩罚是由两个相邻格子的高度差的幂计算得出的。具体来说,若从一个高度为 (h_1) 的格子移动到一个高度为 (h_2) 的格子,移动的惩罚为 ((h_1 - h_2)^X),其中 (X) 是一个给定的奇数。

对于多个查询,你需要计算从一个起点格子到一个终点格子的最小惩罚路径,或者判断是否存在一种情况,导致总惩罚可以任意小(即出现“INVALID”情况)。

解题思路

1. 惩罚的性质

每次移动的惩罚是 ((h_1 - h_2)^X),其中 (X) 是一个奇数。因此,惩罚的计算不仅依赖于高度差,还涉及到 X 的奇数性质。

2. 零周期性(Zero-cycle Property)

若在一个网格上存在一种循环路径,其惩罚可以通过多次循环任意减小,这时我们认为这个网格存在“零周期性”。具体来说,如果网格中任意的 2x2 子网格的惩罚差为零,那么这个网格的惩罚可以任意调整。要判断网格是否具有零周期性,我们需要检查所有的 2x2 子网格,确保它们的惩罚差为零。

3. 单源最短路径

由于每次移动都有一个惩罚,我们可以将这个问题转化为图的最短路径问题。在图中,每个格子是一个节点,相邻的格子之间有边,边的权重即为两格子之间的惩罚。问题可以通过对每个查询计算起点到终点的最短路径来解决。

4. 无效情况(INVALID)

如果网格没有零周期性,那么我们会遇到一些路径可能导致总惩罚变得任意小,因此需要输出 INVALID。检查网格的零周期性可以通过检测每个 2x2 子网格的惩罚差是否为零来完成。

5. 动态规划预处理

通过动态规划方法,我们可以预处理每个格子到起点的最短惩罚值,从而使得每个查询可以在常数时间内回答。具体来说,定义 dp[i][j] 为从起点 (1, 1) 到格子 (i, j) 的惩罚累计值,利用这个预处理,可以快速计算任意两个点之间的惩罚差。

代码分析

代码结构

  1. 预处理部分:

    • 首先,读取输入的高度图,将其存储在 p 数组中。
    • 然后,通过动态规划计算从起点 (1, 1) 到每个格子 (i, j) 的最小惩罚。这个惩罚值保存在 dp 数组中。
    • 使用一个辅助函数 dodo(x, y) 计算两格子之间的惩罚。
  2. 零周期性检查:

    • 通过遍历所有的 2x2 子网格,检查它们的惩罚差是否为零。如果有任何一个 2x2 子网格的惩罚差不为零,说明网格不满足零周期性,直接输出 INVALID
  3. 查询部分:

    • 对每个查询,判断网格是否是零周期的。如果是零周期网格,直接根据预处理的 dp 数组计算起点和终点之间的惩罚差;否则,输出 INVALID

代码解释

#include <iostream>
#include <cmath>
#define int long long
using namespace std;

const int N = 1000 + 10;
int R, C, X;
int p[N][N]; // 存储高度值
int dp[N][N]; // 存储从起点到每个格子的最小惩罚

// 计算两格子之间的惩罚
int dodo(int x, int y) {
    return pow(y - x, X);
}

void solved() {
    cin >> R >> C >> X;

    // 读取高度图
    for (int i = 1; i <= R; ++i) {
        string s;
        cin >> s;
        for (int j = 1; j <= C; ++j) {
            p[i][j] = s[j - 1] - '0';
        }
    }

    // 计算从起点到每个格子的最小惩罚
    for (int j = 2; j <= C; ++j) {
        dp[1][j] = dp[1][j - 1] + dodo(p[1][j - 1], p[1][j]);
    }

    for (int i = 2; i <= R; ++i) {
        for (int j = 1; j <= C; ++j) {
            dp[i][j] = dp[i - 1][j] + dodo(p[i - 1][j], p[i][j]);
        }
    }

    // 检查零周期性
    int flag = 1;
    for (int i = 2; i <= R; ++i) {
        for (int j = 2; j <= C; ++j) {
            if (dp[i][j] - dp[i][j - 1] != dodo(p[i][j - 1], p[i][j])) {
                flag = 0;
            }
        }
    }

    int q;
    cin >> q;
    while (q--) {
        int s1, e1, s2, e2;
        cin >> s1 >> e1 >> s2 >> e2;
        if (flag == 0) {
            cout << "INVALID" << endl;
        } else {
            cout << dp[s1][e1] - dp[s2][e2] << endl;
        }
    }
}

signed main() {
    ios::sync_with_stdio(0);
    cin.tie(0);
    
    solved();
}

代码分析:

  • dodo(x, y):这个函数计算两个高度差的惩罚,即 ((h_1 - h_2)^X)。
  • dp 数组:用来存储从起点 (1, 1) 到其他每个格子的惩罚。
  • 零周期性检查:遍历所有 2x2 子网格,检查它们是否满足零周期性。如果任何子网格不满足,标记为 INVALID
  • 查询回答:对于每个查询,直接计算两点之间的惩罚差。如果网格是零周期的,使用预处理的 dp 数组返回结果,否则返回 INVALID

时间复杂度:

  • 预处理:计算 dp 数组的时间复杂度是 (O(R \times C)),因为每个格子的惩罚仅依赖于其相邻的格子。
  • 查询:每个查询的时间复杂度是 (O(1)),因为我们已经通过动态规划计算出了每个格子到起点的惩罚。
  • 总时间复杂度:预处理的时间复杂度为 (O(R \times C)),对于 (Q) 个查询,时间复杂度为 (O(Q)),所以总时间复杂度为 (O(R \times C + Q))。

总结:

通过动态规划进行预处理和零周期性检查,可以有效地解决多个查询问题。对于每个查询,答案可以在常数时间内给出,确保了高效性。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值