아직 만들고 있는 페이지입니다.

이 페이지는 아직 만드는 중입니다. 보이는 내용은 바뀔 수 있습니다.

Strange sum

시간 제한3초메모리 제한1024 MB

요약
색이 칠해진 격자가 주어질 때, 같은 색을 가진 모든 세포 쌍의 맨해튼 거리 합을 구한다.
난이도

보통10점 중 5점

유형
정렬, 수학, 해시맵, 배열
정답자
아직 제출이 없습니다

문제

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 between two cells (r_1,c_1)(r\_1, c\_1) and (r_2,c_2)(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 (1≤n≤m1 \leq n \le m, n⋅m≤500,000n \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 c_i1,c_i2,…,c_imc\_{i1}, c\_{i2}, \ldots, c\_{im} (1≤c_ij≤1091 \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.

힌트

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.

예제3

  1. 예제 1

    입력
    2 3
    1 2 3
    3 2 1
    
    예상 출력
    7
    
  2. 예제 2

    입력
    3 4
    1 1 2 2
    2 1 1 2
    2 2 1 1
    
    예상 출력
    76
    
  3. 예제 3

    입력
    4 4
    1 1 2 3
    2 1 1 2
    3 1 2 1
    1 1 2 1
    
    예상 출력
    129