A14926 | Weird Sum
时间限制1s
内存限制256MB
通过 / 提交0/0
题目描述
Egor has a table of size $n \times m$ , with lines numbered from $1$ to $n$ and columns numbered from $1$ to $m$ . Each cell has a color that can be presented as an integer from $1$ to $10^5$ .
Let us denote the cell that lies in the intersection of the $r$ -th row and the $c$ -th column as $(r, c)$ . We define the manhattan distance between two cells $(r_1, c_1)$ and $(r_2, c_2)$ as the length of a shortest path between them where each consecutive cells in the path must have a common side. The path can go through cells of any color. For example, in the table $3 \times 4$ the manhattan distance between $(1, 2)$ and $(3, 3)$ is $3$ , one of the shortest paths is the following: $(1, 2) \to (2, 2) \to (2, 3) \to (3, 3)$ .
Egor decided to calculate the sum of manhattan distances between each pair of cells of the same color. Help him to calculate this sum.
Let us denote the cell that lies in the intersection of the $r$ -th row and the $c$ -th column as $(r, c)$ . We define the manhattan distance between two cells $(r_1, c_1)$ and $(r_2, c_2)$ as the length of a shortest path between them where each consecutive cells in the path must have a common side. The path can go through cells of any color. For example, in the table $3 \times 4$ the manhattan distance between $(1, 2)$ and $(3, 3)$ is $3$ , one of the shortest paths is the following: $(1, 2) \to (2, 2) \to (2, 3) \to (3, 3)$ .
Egor decided to calculate the sum of manhattan distances between each pair of cells of the same color. Help him to calculate this sum.
输入格式
The first line contains two integers $n$ and $m$ ( $1 \leq n \le m$ , $n \cdot m \leq 100\,000$ ) — number of rows and columns in the table.
Each of next $n$ lines describes a row of the table. The $i$ -th line contains $m$ integers $c_{i1}, c_{i2}, \ldots, c_{im}$ ( $1 \le c_{ij} \le 100\,000$ ) — colors of cells in the $i$ -th row.
Each of next $n$ lines describes a row of the table. The $i$ -th line contains $m$ integers $c_{i1}, c_{i2}, \ldots, c_{im}$ ( $1 \le c_{ij} \le 100\,000$ ) — colors of cells in the $i$ -th row.
输出格式
Print one integer — the the sum of manhattan distances between each pair of cells of the same color.
输入输出样例
输入 #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
In the first sample there are three pairs of cells of same color: in cells $(1, 1)$ and $(2, 3)$ , in cells $(1, 2)$ and $(2, 2)$ , in cells $(1, 3)$ and $(2, 1)$ . The manhattan distances between them are $3$ , $1$ and $3$ , the sum is $7$ .
C++ 编辑器
输入
输出
可保存默认模板;新题优先使用已保存模板。
当前快捷键仅展示,暂不支持修改。
- 撤销
Ctrl / ⌘ + Z - 重做
Ctrl / ⌘ + Y - 查找
Ctrl / ⌘ + F - 全选
Ctrl / ⌘ + A - 复制
Ctrl / ⌘ + C - 剪切
Ctrl / ⌘ + X - 粘贴
Ctrl / ⌘ + V - 自动排版
工具栏排版按钮 - 草稿保存
编辑时自动保存到本机
历史
提交记录
状态说明时间源码
AI
作答助手
你好,我是作答助手。可以问思路、复杂度、样例含义或代码报错原因;不会直接给出完整 AC 代码。
确定要清空代码吗?
提交通过
评测结果:Accepted