Advertisement
Josif_tepe

Untitled

Apr 7th, 2025
311
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.04 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <map>
  5. #include <fstream>
  6. #include <cstring>
  7. using namespace std;
  8. typedef long long ll;
  9. const int maxn = 1e6 + 10;
  10.  
  11. int dist(int si, int sj, int ei, int ej) {
  12.     return abs(si - ei) + abs(sj - ej);
  13. }
  14. pair<int, int> idx[maxn];
  15. //vector<pair<int, int>> graph[maxn];
  16. int pref_sum[maxn];
  17. int cords[maxn];
  18. int mat[10001][10001];
  19. bool visited[maxn];
  20. int main() {
  21. //    ifstream cin("in.txt");
  22.     ios_base::sync_with_stdio(false);
  23.     cin.tie(0);
  24.    
  25.     memset(visited, false, sizeof visited);
  26.     int r, c;
  27.     cin >> r >> c;
  28.     ll res = 0;
  29.     int cnt = 1;
  30.     for(int i = 0; i < r; i++) {
  31.         for(int j = 0; j < c; j++) {
  32.             cin >> mat[i][j];
  33.             idx[cnt] = {i, j};
  34.             cnt++;
  35.         }
  36.     }
  37.     cnt = 1;
  38.        for(int i = 0; i < r; i++) {
  39.            for(int j = 0; j < c; j++) {
  40.                if(cnt != mat[i][j]) {
  41.                    
  42.                    pair<int, int> c = make_pair(i, j);
  43.                    if(!visited[cnt]) {
  44.                        bool ok = false;
  45. //                       cout << cnt << endl;
  46.                        int sum = 0;
  47.                        while(!ok) {
  48.                            if(mat[c.first][c.second] == cnt) {
  49.                                ok = true;
  50.                            }
  51.                            if(visited[mat[c.first][c.second]]) {
  52.                                break;
  53.                            }
  54.                            visited[mat[c.first][c.second]] = true;
  55.                            sum += dist(c.first, c.second, idx[mat[c.first][c.second]].first, idx[mat[c.first][c.second]].second);
  56.                            pref_sum[mat[c.first][c.second]] = sum;
  57.                            cords[mat[c.first][c.second]] = cnt;
  58.                            c = idx[mat[c.first][c.second]];
  59.                            if(ok) {
  60.                                break;
  61.                            }
  62.                            
  63.                        }
  64. //                       cout << cnt << " " << sum << endl;
  65.                        pref_sum[cnt] = sum;
  66.                        visited[cnt] = true;
  67.                        
  68.                    }
  69.                }
  70.                cnt++;
  71.            }
  72.        }
  73.    
  74.     cnt = 1;
  75.     for(int i = 0; i < r; i++) {
  76.         for(int j = 0; j < c; j++) {
  77.             int a = mat[i][j];
  78.             int b = cnt;
  79.             if(a != b) {
  80.                  
  81.                  if(cords[a] == b) {
  82.                     res += pref_sum[b] - pref_sum[a];
  83.                 }
  84.                 else {
  85.                     res += pref_sum[cords[a]] - pref_sum[a] + pref_sum[b];
  86. //                    cout << cords[a] << " " << pref_sum[cords[a]] << " " << pref_sum[a] << " " << pref_sum[b] << endl;
  87. //                    cout << b << " " << a << " " << pref_sum[cords[a]] - pref_sum[a] + pref_sum[b] << endl;
  88.                 }
  89. //                break;
  90.             }
  91.             cnt++;
  92.         }
  93.     }
  94.     cout << res << endl;
  95.     return 0;
  96.      
  97. }
  98.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement