Codeforces Round 775(Div.2) C. Weird Sum

C. Weird Sum

time limit per test

2 seconds

memory limit per test

256 megabytes

Egor has a table of size $ n \times m$, with lines numbered from 1 1 1 to n n n and columns numbered from $1 $to m m m. Each cell has a color that can be presented as an integer from$ 1 $to 1 0 5 . 10^5. 105.

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 ) (r_1, c_1) (r1,c1) and ( r 2 , c 2 ) (r_2, c_2) (r2,c2)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 × 4 3 \times 4 3×4 the manhattan distance between ( 1 , 2 ) (1, 2) (1,2) and ( 3 , 3 ) (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) (1,2)(2,2)(2,3)(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.

Input

The first line contains two integers n and m ( 1 ≤ n ≤ m , n ⋅ m ≤ 100   000 ) (1 \leq n \le m, n \cdot m \leq 100\,000) (1nm,nm100000) — 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 i 1 , c i 2 , … , c i m ( 1 ≤ c i j ≤ 100   000 ) c_{i1}, c_{i2}, \ldots, c_{im} (1 \le c_{ij} \le 100\,000) ci1,ci2,,cim(1cij100000)— colors of cells in the i-th row.

Output

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

Examples

input

2 3
1 2 3
3 2 1

output

7

input

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

output

Copy

76

input

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

output

129

Note

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

We note that the manhattan distance between cells ( r 1 , c 1 ) (r_1, c_1) (r1,c1)and ( r 2 , c 2 ) (r_2, c_2) (r2,c2) is equal to ∣ r 1 − r 2 ∣ + ∣ c 1 − c 2 ∣ |r_1-r_2|+|c_1-c_2| r1r2+c1c2 For each color we will compose a list of all cells ( r 0 , c 0 ) (r_0, c_0) (r0,c0), \ldots, ( r k − 1 , c k − 1 ) (r_{k-1}, c_{k-1}) (rk1,ck1)of this color, compute the target sum for this color, and sum up the answers for all colors. The sum is equal:

∑ i = 0 k − 1 ∑ j = i + 1 k − 1 ∣ r i − r j ∣ + ∣ c i − c j ∣ = ( ∑ i = 0 k − 1 ∑ j = i + 1 k − 1 ∣ r i − r j ∣ ) + ( ∑ i = 0 k − 1 ∑ j = i + 1 k − 1 ∣ c i − c j ∣ ) {\large\sum_{i=0}^{k-1} \sum_{j=i+1}^{k-1} |r_i - r_j| + |c_i - c_j| = \left(\sum_{i=0}^{k-1} \sum_{j=i+1}^{k-1} |r_i - r_j|\right) + \left(\sum_{i=0}^{k-1} \sum_{j=i+1}^{k-1} |c_i - c_j|\right)} i=0k1j=i+1k1rirj+cicj=(i=0k1j=i+1k1rirj)+(i=0k1j=i+1k1cicj)

r理解为x,c理解为y,可以分开求

We will compute the first sum, the second sum is analogous. Let an array s s s be equal to r, but sorted in increasing order. Then:

对r数组排序,得到s,因为排序了所以绝对值 r j − r i r_j - r_i rjri一定是正的,可以去掉绝对值,,接着拆开

∑ i = 0 k − 1 ∑ j = i + 1 k − 1 ∣ r i − r j ∣ = ∑ i = 0 k − 1 ∑ j = i + 1 k − 1 s j − s i = ( ∑ i = 0 k − 1 ∑ j = i + 1 k − 1 s j ) + ( ∑ i = 0 k − 1 ∑ j = i + 1 k − 1 − s i ) \large\sum_{i=0}^{k-1} \sum_{j=i+1}^{k-1} |r_i - r_j| = \sum_{i=0}^{k-1} \sum_{j=i+1}^{k-1} s_j - s_i = \left(\sum_{i=0}^{k-1} \sum_{j=i+1}^{k-1} s_j\right) + \left(\sum_{i=0}^{k-1} \sum_{j=i+1}^{k-1} -s_i\right) i=0k1j=i+1k1rirj=i=0k1j=i+1k1sjsi=(i=0k1j=i+1k1sj)+(i=0k1j=i+1k1si)

The value s j s_j sj occurs in the first double sum exactly j j j times, the value − s i -s_i sioccurs in the second sum exactly k − 1 − i k-1-i k1itimes. Then, the value is equal to:

前面部分, ( ∑ i = 0 k − 1 ∑ j = i + 1 k − 1 s j ) \left(\sum_{i=0}^{k-1} \sum_{j=i+1}^{k-1} s_j\right) (i=0k1j=i+1k1sj) 因为只有j没有i,

我们考虑化简,我们写出循环,可以化为 ∑ j = 0 k − 1 j s j \large\sum_{j=0}^{k-1} js_j j=0k1jsj

后面部分,只有i,所以j可以直接代进去,上面k-1,下面j=i+1,相减再加一, k − 1 − i k-1-i k1i s i s_i si

∑ j = 0 k − 1 j s j + ∑ i = 0 k − 1 − ( k − 1 − i ) s i = ∑ i = 0 k − 1 ( 2 i + 1 − k ) s i \large\sum_{j=0}^{k-1} js_j + \sum_{i=0}^{k-1} -(k-1-i)s_i = \sum_{i=0}^{k-1} (2i+1-k)s_i j=0k1jsj+i=0k1(k1i)si=i=0k1(2i+1k)si

The last sum can be computed in O(k), the time complexity to sort an array is O ( k log ⁡ k ) O(k \log k) O(klogk). The overall complexity is O(nm\log(nm)). We can also sort arrays of coordinates by adding cells to lists in the right order. This yields an O(nm) solution.

所以就是算最后一条式子。

x,y遍历一遍就好

#include <iostream>
#include <algorithm>
#include <vector>
#include <set>
typedef unsigned long long ll;
using namespace std;
int x, y;
set<int >se;//存颜色
vector<pair<int, int > > a[1000003];//存xy

bool cmp(pair<int, int> a, pair<int, int > b) {
	return a.first < b.first;
}

bool cmp2(pair<int, int> a, pair<int, int > b) {
	return a.second < b.second;
}

int main() {
	cin >> x >> y;
	int tmp;
	for (int i = 1; i <= x; i++) {
		for (int j = 1; j <= y; j++) {
			cin >> tmp;
			se.insert(tmp);
			a[tmp].push_back({i, j});
		}
	}
	ll res = 0;
	int sum = se.size();
	//cout<<sum<<endl;
	for (auto c : se) {
		sort(a[c].begin(), a[c].end(), cmp);
		int len = a[c].size();
		for (int i = 0; i < len; i++) {
			res += (2 * i + 1 - len) * 1LL * a[c][i].first;
		}
		//cout<<res<<endl;
		sort(a[c].begin(), a[c].end(), cmp2);
		for (int i = 0; i < len; i++) {
			res += (2 * i + 1 - len) * 1LL * a[c][i].second;
		}
	}
	cout << res;

}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值