Universal Cup Judging System

Universal Cup

时间限制: 2.0 s 内存限制: 256 MB 总分: 100 可 Hack ✓
统计

Mr. Ham 拥有一个大小为 $n \times m$ 的矩阵,其中每个单元格都填充了一个颜色值 $c_{i,j}$。Mr. Ham 对相同颜色单元格之间的关系很感兴趣,并希望计算所有颜色相同的单元格对之间的曼哈顿距离之和。

两个单元格 $(x_1, y_1)$ 和 $(x_2, y_2)$ 之间的曼哈顿距离由 $d_M = |x_1 - x_2| + |y_1 - y_2|$ 给出。

形式化地,请计算:

$$\text{Total Sum} = \sum_{C} \sum_{(x_i, y_i) \in S_C} \sum_{(x_j, y_j) \in S_C} |x_i - x_j| + |y_i - y_j|$$

这里,$C$ 表示所有不同颜色的集合。$S_C$ 表示颜色值等于指定颜色 $C$ 的坐标 $(x, y)$ 的集合。

输入格式

第一行包含两个整数 $n$ 和 $m$ ($1 \le n, m \le 1000$),表示矩阵的行数和列数。

接下来包含 $n$ 行。每行 $i$ 包含 $m$ 个空格分隔的整数 $c_{i,j}$ ($1 \le c_{i,j} \le 10^9$),表示矩阵中的颜色值。

输出格式

输出一个整数,即所有颜色相同的单元格对之间的曼哈顿距离之和。

样例

样例输入 1

2 2
1 1
2 2

样例输出 1

4

样例输入 2

4 4
1 3 2 4
2 1 2 3
1 3 3 2
3 2 1 4

样例输出 2

152

说明

在第一个样例中,不同的颜色值为 1 和 2。

  1. 对于颜色 1:

    • 颜色为 1 的坐标是 $(1, 1)$ 和 $(1, 2)$。
    • $(1, 1)$ 和 $(1, 1)$ 之间的曼哈顿距离为 $|1 - 1| + |1 - 1| = 0$。
    • $(1, 1)$ 和 $(1, 2)$ 之间的曼哈顿距离为 $|1 - 1| + |1 - 2| = 1$。
    • $(1, 2)$ 和 $(1, 1)$ 之间的曼哈顿距离为 $|1 - 1| + |2 - 1| = 1$。
    • $(1, 2)$ 和 $(1, 2)$ 之间的曼哈顿距离为 $|1 - 1| + |2 - 2| = 0$。
  2. 对于颜色 2:

    • 颜色为 2 的坐标是 $(2, 1)$ 和 $(2, 2)$。
    • $(2, 1)$ 和 $(2, 1)$ 之间的曼哈顿距离为 $|2 - 2| + |1 - 1| = 0$。
    • $(2, 1)$ 和 $(2, 2)$ 之间的曼哈顿距离为 $|2 - 2| + |1 - 2| = 1$。
    • $(2, 2)$ 和 $(2, 1)$ 之间的曼哈顿距离为 $|2 - 2| + |2 - 1| = 1$。
    • $(2, 2)$ 和 $(2, 2)$ 之间的曼哈顿距离为 $|2 - 2| + |2 - 2| = 0$。

因此,所有颜色相同的单元格对的曼哈顿距离总和为 $1 + 1 + 1 + 1 = 4$。

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.