#P13543. [OOI 2022] Strange sum
[OOI 2022] Strange sum
题目描述
Egor has table of size , where all lines are numbered from to and all columns are numbered from to . Each cell is painted in some color that can be presented as integer from to .
Let us call the cell that lies in -th row and -th column as . We define the between two cells and 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 and is 3, one of the shortest paths is the following: .
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 and (, ) --- number of rows and columns in the table.
Each of next lines describes the rows of the table. -th line contains integers () --- colors of cells in -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 and , in coordinates and , in coordinates and . The manhattan distances between them are , and , the sum is .
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 as the maximum number of color in the table.
Group | Points | Additional constraints | < | Required groups | Comment | |
---|---|---|---|---|---|---|
0 | -- | -- | -- | Sample tests | ||
1 | ||||||
2 | -- | |||||
3 | ||||||
4 | -- | |||||
5 | -- | -- |