fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define int long long
  4.  
  5. signed main() {
  6. ios::sync_with_stdio(false);
  7. cin.tie(nullptr);
  8.  
  9. int n, m;
  10. cin >> n >> m;
  11.  
  12. unordered_map<int, vector<int>> rows, cols;
  13.  
  14. for(int i = 0; i < n; i++) {
  15. for(int j = 0; j < m; j++) {
  16. int x;
  17. cin >> x;
  18. rows[x].push_back(i);
  19. cols[x].push_back(j);
  20. }
  21. }
  22.  
  23. long long ans = 0;
  24.  
  25. for(auto &[color, v] : rows) {
  26. sort(v.begin(), v.end());
  27.  
  28. long long pref = 0;
  29.  
  30. for(int i = 0; i < (int)v.size(); i++) {
  31. ans += 1LL * v[i] * i - pref;
  32. pref += v[i];
  33. }
  34. }
  35.  
  36. for(auto &[color, v] : cols) {
  37. sort(v.begin(), v.end());
  38.  
  39. long long pref = 0;
  40.  
  41. for(int i = 0; i < (int)v.size(); i++) {
  42. ans += 1LL * v[i] * i - pref;
  43. pref += v[i];
  44. }
  45. }
  46.  
  47. cout << ans << '\n';
  48.  
  49. return 0;
  50. }
Success #stdin #stdout 0s 5308KB
stdin
3 4
1 1 2 2
2 1 1 2
2 2 1 1
stdout
76