QOJ.ac

QOJ

시간 제한: 3.0 s 메모리 제한: 1024 MB 총점: 100 해킹 가능 ✓

#16269. 奇怪的和

통계

Egor 有一个大小为 $n \times m$ 的表格,其中所有行从 $1$ 到 $n$ 编号,所有列从 $1$ 到 $m$ 编号。每个单元格都涂有某种颜色,颜色可以用 $1$ 到 $10^9$ 之间的整数表示。

我们把位于第 $r$ 行第 $c$ 列的单元格记作 $(r, c)$。我们定义两个单元格 $(r_1, c_1)$ 和 $(r_2, c_2)$ 之间的曼哈顿距离为它们之间最短路径的长度,其中路径上每两个相邻的单元格都有公共边。路径可以经过任何颜色的单元格。例如,$(1, 2)$ 和 $(3, 3)$ 之间的曼哈顿距离为 $3$,其中一条最短路径为:$(1, 2) \to (2, 2) \to (2, 3) \to (3, 3)$。

Egor 决定计算每对相同颜色单元格之间的曼哈顿距离之和。请帮助他计算这个总和。

输入格式

第一行包含两个整数 $n$ 和 $m$($1 \le n \le m$,$n \cdot m \le 500\,000$)—— 表格的行数和列数。

接下来的 $n$ 行,每行描述表格的一行。第 $i$ 行包含 $m$ 个整数 $c_{i1}, c_{i2}, \dots, c_{im}$($1 \le c_{ij} \le 10^9$)—— 第 $i$ 行单元格的颜色。

输出格式

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

子任务

本题的测试点分为 5 个子任务。对于每个子任务,只有当你的程序通过该子任务中的所有测试点以及某些先前子任务中的所有测试点时,你才能获得该子任务的分数。请注意,你的程序可能无法通过样例测试,但仍会被接受进行评测。

我们定义 $C$ 为表格中不同颜色的最大数量。

子任务 分值 附加限制 $n$ 附加限制 $n \cdot m$ 附加限制 $C$ 依赖子任务 备注
0 0 - - - 样例测试
1 23 - $n \cdot m \le 1000$ - 0
2 17 $n = 1$ - -
3 15 $n = 2$ - -
4 20 - - $C \le 2$
5 25 - - - 0 – 4

样例

输入样例 1

2 3
1 2 3
3 2 1

输出样例 1

7

输入样例 2

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

输出样例 2

76

输入样例 3

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

输出样例 3

129

说明

在第一个样例中,有三对相同颜色的单元格:坐标为 $(1, 1)$ 和 $(2, 3)$ 的单元格,坐标为 $(1, 2)$ 和 $(2, 2)$ 的单元格,以及坐标为 $(1, 3)$ 和 $(2, 1)$ 的单元格。它们之间的曼哈顿距离分别为 $3$、$1$ 和 $3$,总和为 $7$。

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

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.