Advertisement
Josif_tepe

Untitled

Mar 30th, 2025
497
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <queue>
  4. #include <map>
  5. using namespace std;
  6. typedef long long ll;
  7. const int maxn = 1e6 + 10;
  8.  
  9. int dist(int si, int sj, int ei, int ej) {
  10.     return abs(si - ei) + abs(sj - ej);
  11. }
  12. pair<int, int> idx[maxn];
  13. vector<pair<int, int>> graph[maxn];
  14. ll pref_sum[maxn];
  15. ll pref_sum2[maxn];
  16. int main() {
  17.     ios_base::sync_with_stdio(false);
  18.     int r, c;
  19.     cin >> r >> c;
  20.    
  21.     vector<vector<int>> mat(r, vector<int>(c));
  22.     int cnt = 1;
  23.     for(int i = 0; i < r; i++) {
  24.         for(int j = 0; j < c; j++) {
  25.             cin >> mat[i][j];
  26.             idx[cnt] = {i, j};
  27.             cnt++;
  28.         }
  29.     }
  30.    
  31.     cnt = 1;
  32.     for(int i = 0; i < r; i++) {
  33.         for(int j = 0; j < c; j++) {
  34.            
  35.             int a = mat[i][j];
  36.             int b = mat[idx[a].first][idx[a].second];
  37.             int c = dist(i, j, idx[a].first, idx[a].second);
  38.            
  39.             graph[a].push_back(make_pair(b, c));
  40.             cnt++;
  41.         }
  42.     }
  43.    
  44.     int n = r * c;
  45.     vector<bool> visited(n + 1, false);
  46.     for(int i = 1; i <= n; i++) {
  47.         if(!visited[i]) {
  48.             queue<int> q;
  49.             q.push(i);
  50.            
  51.             vector<int> tmp;
  52.             tmp.push_back(i);
  53.            
  54.             while(!q.empty()) {
  55.                 int c = q.front();
  56.                 q.pop();
  57.                
  58.                 for(int i = 0; i < (int) graph[c].size(); i++) {
  59.                     int neighbour = graph[c][i].first, weight = graph[c][i].second;
  60.                     if(!visited[neighbour]) {
  61.                         visited[neighbour] = true;
  62.                         q.push(neighbour);
  63.                         tmp.push_back(weight);
  64.                         tmp.push_back(neighbour);
  65.                        
  66.                     }
  67.                 }
  68.             }
  69.            
  70.             ll sum = 0;
  71.             for(int i = 0; i < (int) tmp.size(); i++) {
  72.                 if(i % 2 == 1) {
  73.                     sum += tmp[i];
  74.                     cout << "|" << tmp[i] << "| ";
  75.                 }
  76.                 else {
  77.                     cout << tmp[i] << " ";
  78.                     if(i > 0) {
  79.                         pref_sum[tmp[i]] = sum - tmp[i - 1];
  80.                     }
  81.                     else {
  82.                         pref_sum[tmp[i]] = sum;
  83.                     }
  84.                 }
  85.             }
  86.             for(int i = 0; i < (int) tmp.size(); i++) {
  87.                 if(i % 2 == 1) {
  88.                     sum += tmp[i];
  89.                     cout << "|" << tmp[i] << "| ";
  90.  
  91.                    
  92.                 }
  93.                 else {
  94.                     cout << tmp[i] << " ";
  95.  
  96.                     pref_sum2[tmp[i]] = sum;
  97.                 }
  98.                
  99.             }
  100.             cout << endl;
  101.         }
  102.     }
  103.     cnt = 1;
  104.     ll res = 0;
  105.     for(int i = 0; i < r; i++) {
  106.         for(int j = 0; j < c; j++) {
  107.             if(cnt != mat[i][j]) {
  108.                 res += pref_sum2[cnt] - pref_sum[mat[i][j]];
  109.             }
  110.         }
  111.     }
  112.     cout << res << endl;
  113.    
  114.     return 0;
  115.    
  116. }
  117.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement