A. Weird Sum-Codeforces Round #775 (Div. 1, based on Moscow Open Olympiad in Informatics)

Problem - 1648A - Codeforces

A. Weird Sum

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

Egor has a table of size n×mn×m, with lines numbered from 11 to nn and columns numbered from 11 to mm. Each cell has a color that can be presented as an integer from 11 to 105105.

Let us denote the cell that lies in the intersection of the rr-th row and the cc-th column as (r,c)(r,c). We define the manhattan distance between two cells (r1,c1)(r1,c1) and (r2,c2)(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×43×4 the manhattan distance between (1,2)(1,2) and (3,3)(3,3) is 33, one of the shortest paths is the following: (1,2)→(2,2)→(2,3)→(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 nn and mm (1≤n≤m1≤n≤m, n⋅m≤100000n⋅m≤100000) — number of rows and columns in the table.

Each of next nn lines describes a row of the table. The ii-th line contains mm integers ci1,ci2,…,cimci1,ci2,…,cim (1≤cij≤1000001≤cij≤100000) — colors of cells in the ii-th row.

Output

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

Examples

input

Copy

2 3
1 2 3
3 2 1

output

Copy

7

input

Copy

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

output

Copy

76

input

Copy

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

output

Copy

129

Note

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

==================================================================================================================================================

常规方法无非是对于每个数字的行列坐标存进容器n^2遍历,但很显然会超时。这里利用前缀和知识,原本我们是要对坐标进行双重循环遍历,然后再除以2,这里我们先排序,只计算一次大减小即可。对于一个v[i],v[j]小于v[i], 得到的结果是 v[i]-v[1]+v[i]-v[2]+v[i]-v[3]+...+v[i]-v[i-1]

也就是 v[i]*(i-1) +sum[i-1] 前缀和表示一下即可。

#include <bits/stdc++.h>
# define inf 0x7f7f7f7f7f7f7f7f
using namespace std;

typedef long long int ll;

vector<ll>v1[100010];

vector<ll>v2[100010];

ll sum[100010];

int main()
{

     int n,m;

     cin>>n>>m;

     for(int i=1;i<=n;i++)
     {
         for(int j=1;j<=m;j++)
         {

             ll x;

             cin>>x;

             v1[x].push_back(i);

             v2[x].push_back(j);

         }
     }

     ll ans=0;


     for(ll i=1;i<=100000;i++)
     {
         sort(v1[i].begin(),v1[i].end());

         sort(v2[i].begin(),v2[i].end());


         for(ll j=0;j<v1[i].size();j++)
         {
             sum[j+1]=sum[j]+v1[i][j];

         }

         // ai-a1 ai-a2 ai-a3 ai-ai-1
         for(ll j=0;j<v1[i].size();j++)
         {

             ans+=j*v1[i][j];

             ans-=sum[j];

         }


         for(ll j=0;j<v2[i].size();j++)
         {
             sum[j+1]=sum[j]+v2[i][j];

         }

         // ai-a1 ai-a2 ai-a3 ai-ai-1
         for(ll j=0;j<v2[i].size();j++)
         {

             ans+=j*v2[i][j];

             ans-=sum[j];

         }




     }

     cout<<ans;


    return 0;
}

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

秦三码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值