fork download
  1. #include <bits/stdc++.h>
  2. #include <limits.h>
  3. #include <ext/pb_ds/assoc_container.hpp>
  4. #include <ext/pb_ds/tree_policy.hpp>
  5. using namespace __gnu_pbds;
  6. #define ll long long
  7. using namespace std;
  8. template<typename T>
  9. using ordered_set = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
  10. #define lsb(x) (__builtin_ctzll(x)) //pos of least set bit (first 1)
  11. #define msb(x) (63 - __builtin_clzll(x)) //pos of most set bit (last 1)
  12. ll dx[8] = {-1,0,1,0,-1,-1,1,1};
  13. ll dy[8] = {0,1,0,-1,-1,1,1,-1};
  14. void fileIO(void) {
  15.  
  16. #ifndef ONLINE_JUDGE
  17.  
  18. freopen("input.txt", "r", stdin);
  19. freopen("output.txt", "w", stdout);
  20.  
  21. #endif
  22.  
  23. }
  24. void fastIO() {
  25. ios::sync_with_stdio(false);
  26. cin.tie(NULL);
  27. cout.tie(NULL);
  28. }
  29.  
  30. ll calc(vector<ll> v){
  31. sort(v.begin(),v.end());
  32. ll x=0, pref=0;
  33. for(ll i=0; i<v.size(); i++){
  34. x += i*v[i] - pref;
  35. pref+=v[i];
  36. }
  37. return x;
  38. }
  39.  
  40. void solve(){
  41. ll n,m; cin>>n>>m;
  42. const ll maxc=1e5;
  43. vector<vector<ll>> col_rows(maxc+1) , col_columns(maxc+1);
  44.  
  45. for(ll i=0; i<n; i++){
  46. for(ll j=0; j<m; j++){
  47. ll color; cin>>color;
  48. col_rows[color].push_back(i);
  49. col_columns[color].push_back(j);
  50. }
  51. }
  52. ll ans=0;
  53. for(ll i=1; i<=maxc; i++){
  54. ans += calc(col_rows[i]);
  55. ans += calc(col_columns[i]);
  56. }
  57. cout<<ans;
  58. }
  59.  
  60. int main() {
  61. fileIO();
  62. fastIO();
  63. ll testcases=1;
  64. //cin>>testcases;
  65. while(testcases--){
  66. solve();
  67. }
  68.  
  69. }
Success #stdin #stdout 0.01s 7684KB
stdin
Standard input is empty
stdout
Standard output is empty