#P13543. [OOI 2022] Strange sum

[OOI 2022] Strange sum

题目描述

Egor has table of size n×mn \times m, where all lines are numbered from 11 to nn and all columns are numbered from 11 to mm. Each cell is painted in some color that can be presented as integer from 11 to 10910^9.

Let us call the cell that lies in rr-th row and cc-th column as (r,c)(r, c). We define the manhattan distance\emph{manhattan distance} between two cells (r1,c1)(r_1, c_1) and (r2,c2)(r_2, c_2) as the length of shortest path between them where each consecutive cells have common side. The path can go through cells of any color. For example, the manhattan distance between (1,2)(1, 2) and (3,3)(3, 3) is 3, one of the shortest paths is the following: (1,2)(2,2)(2,3)(3,3)(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 same color. Help him to calculate this sum.

输入格式

The first line contains two integers nn and mm (1nm1 \leq n \le m, nm500000n \cdot m \leq 500\,000) --- number of rows and columns in the table.

Each of next nn lines describes the rows of the table. ii-th line contains mm integers ci1,ci2,,cimc_{i1}, c_{i2}, \ldots, c_{im} (1cij1091 \le c_{ij} \le 10^9) --- colors of cells in ii-th row.

输出格式

Print one integer --- the the sum of manhattan distances between each pair of cells of same color.

2 3
1 2 3
3 2 1
7
3 4
1 1 2 2
2 1 1 2
2 2 1 1
76
4 4
1 1 2 3
2 1 1 2
3 1 2 1
1 1 2 1
129

提示

Note

In the first sample there are three pairs of cells of same color: in coordinates (1,1)(1, 1) and (2,3)(2, 3), in coordinates (1,2)(1, 2) and (2,2)(2, 2), in coordinates (1,3)(1, 3) and (2,1)(2, 1). The manhattan distances between them are 33, 11 and 33, the sum is 77.

Scoring

Tests for this problem are divided into 5 groups. For each of the groups you earn points only if your solution passes all tests in this group and all tests in some of the previous groups. Note that your solution may not pass the sample tests, but it will still be accepted for evaluation

We define CC as the maximum number of color in the table.

Group Points Additional constraints < Required groups Comment
nn nmn \cdot m CC
0 00 -- -- -- Sample tests
1 2323 nm1000n \cdot m \le 1000 00
2 1717 n=1n=1 --
3 1515 n=2n=2
4 2020 -- C2C \le 2
5 2525 -- 00--44