Problem p02670 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer",            \\
                     "inline") // Optimization flags
#pragma GCC target("avx2")     // Enable AVX

using namespace std;

using ll = long long;
using ld = long double;

#define mp make_pair

const int p = 17;

int mul(int a, int b) { return (1LL * a * b) % p; }

int add(int a, int b) {
  int s = (a + b);
  if (s >= p)
    s -= p;
  return s;
}

int sub(int a, int b) {
  int s = (a + p - b);
  if (s >= p)
    s -= p;
  return s;
}

int po(int a, int deg) {
  if (deg == 0)
    return 1;
  if (deg % 2 == 1)
    return mul(a, po(a, deg - 1));
  int t = po(a, deg / 2);
  return mul(t, t);
}

int inv(int n) { return po(n, p - 2); }

mt19937 rnd(time(0));

struct DSU {
  vector<int> sz;
  vector<int> parent;

  void make_set(int v) {
    parent[v] = v;
    sz[v] = 1;
  }

  int find_set(int v) {
    if (v == parent[v])
      return v;
    return parent[v] = find_set(parent[v]);
  }

  void union_sets(int a, int b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
      if (sz[a] < sz[b])
        swap(a, b);
      parent[b] = a;
      sz[a] += sz[b];
    }
  }

  DSU(int n) {
    sz.resize(n);
    parent.resize(n);
    for (int i = 0; i < n; i++)
      make_set(i);
  }
};

/*
const int N = 1200000;

vector<int> facs(N), invfacs(N);

void init()
{
    facs[0] = 1;
    for (int i = 1; i<N; i++) facs[i] = mul(facs[i-1], i);
    invfacs[N-1] = inv(facs[N-1]);
    for (int i = N-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1);
}

int C(int n, int k)
{
    if (n<k) return 0;
    return mul(facs[n], mul(invfacs[k], invfacs[n-k]));
}
*/

/*

pair<int, int> solve1(ll l, ll r)
{
    int answer = 0;
    while (l>= answer+1 || r>=answer+1)
    {
        answer++;
        if (l>=r) l-=answer; else r-=answer;
    }
    //cout<<"Case #:"<<answer<<' '<<l<<' '<<r<<endl;
    return mp(l, r);
}*/

int d[500][500];
bool in[500][500];
int n;

vector<pair<int, int>> neighbours(int x, int y) {
  vector<pair<int, int>> ans;
  if (x > 0)
    ans.push_back(mp(x - 1, y));
  if (x < n - 1)
    ans.push_back(mp(x + 1, y));
  if (y > 0)
    ans.push_back(mp(x, y - 1));
  if (y < n - 1)
    ans.push_back(mp(x, y + 1));
  return ans;
}

void dfs(int x, int y) {
  auto n = neighbours(x, y);
  int d1 = d[x][y];
  if (in[x][y])
    d1++;
  for (auto it : n) {
    if (d1 < d[it.first][it.second]) {
      d[it.first][it.second] = d1;
      dfs(it.first, it.second);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);

  cin >> n;

  /*vector<int> prm(n*n);



  for (int i = 0; i<n*n; i++) prm[i] = i;

  shuffle(prm.begin(), prm.end(), rnd);

  for (auto it: prm) cout<<it<<' ';
  cout<<endl;

*/

  vector<pair<int, int>> a(n * n);
  for (int i = 0; i < n * n; i++) {
    int t;
    cin >> t;
    t--;
    a[i] = mp(t / n, t % n);
  }

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      in[i][j] = true;

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      d[i][j] = min({i, n - i - 1, j, n - j - 1});

  ll res = 0;
  for (auto it : a) {
    // cout<<d[it.first][it.second]<<endl;
    res += d[it.first][it.second];
    in[it.first][it.second] = false;
    dfs(it.first, it.second);
  }

  cout << res << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#pragma GCC optimize("Ofast", "unroll-loops", "omit-frame-pointer",            \\
                     "inline") // Optimization flags
#pragma GCC target("avx2")     // Enable AVX

using namespace std;

using ll = long long;
using ld = long double;

#define mp make_pair

const int p = 17;

int mul(int a, int b) { return (1LL * a * b) % p; }

int add(int a, int b) {
  int s = (a + b);
  if (s >= p)
    s -= p;
  return s;
}

int sub(int a, int b) {
  int s = (a + p - b);
  if (s >= p)
    s -= p;
  return s;
}

int po(int a, int deg) {
  if (deg == 0)
    return 1;
  if (deg % 2 == 1)
    return mul(a, po(a, deg - 1));
  int t = po(a, deg / 2);
  return mul(t, t);
}

int inv(int n) { return po(n, p - 2); }

mt19937 rnd(time(0));

struct DSU {
  vector<int> sz;
  vector<int> parent;

  void make_set(int v) {
    parent[v] = v;
    sz[v] = 1;
  }

  int find_set(int v) {
    if (v == parent[v])
      return v;
    return parent[v] = find_set(parent[v]);
  }

  void union_sets(int a, int b) {
    a = find_set(a);
    b = find_set(b);
    if (a != b) {
      if (sz[a] < sz[b])
        swap(a, b);
      parent[b] = a;
      sz[a] += sz[b];
    }
  }

  DSU(int n) {
    sz.resize(n);
    parent.resize(n);
    for (int i = 0; i < n; i++)
      make_set(i);
  }
};

/*
const int N = 1200000;

vector<int> facs(N), invfacs(N);

void init()
{
    facs[0] = 1;
    for (int i = 1; i<N; i++) facs[i] = mul(facs[i-1], i);
    invfacs[N-1] = inv(facs[N-1]);
    for (int i = N-2; i>=0; i--) invfacs[i] = mul(invfacs[i+1], i+1);
}

int C(int n, int k)
{
    if (n<k) return 0;
    return mul(facs[n], mul(invfacs[k], invfacs[n-k]));
}
*/

/*

pair<int, int> solve1(ll l, ll r)
{
    int answer = 0;
    while (l>= answer+1 || r>=answer+1)
    {
        answer++;
        if (l>=r) l-=answer; else r-=answer;
    }
    //cout<<"Case #:"<<answer<<' '<<l<<' '<<r<<endl;
    return mp(l, r);
}*/

vector<vector<int>> d;
vector<vector<bool>> in;
int n;

vector<pair<int, int>> neighbours(int x, int y) {
  vector<pair<int, int>> ans;
  if (x > 0)
    ans.push_back(mp(x - 1, y));
  if (x < n - 1)
    ans.push_back(mp(x + 1, y));
  if (y > 0)
    ans.push_back(mp(x, y - 1));
  if (y < n - 1)
    ans.push_back(mp(x, y + 1));
  return ans;
}

void dfs(int x, int y) {
  auto n = neighbours(x, y);
  for (auto it : n) {
    int d1 = d[x][y];
    if (in[x][y])
      d1++;
    if (d1 < d[it.first][it.second]) {
      d[it.first][it.second] = d1;
      dfs(it.first, it.second);
    }
  }
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(nullptr);

  cin >> n;

  /*vector<int> prm(n*n);



  for (int i = 0; i<n*n; i++) prm[i] = i;

  shuffle(prm.begin(), prm.end(), rnd);

  for (auto it: prm) cout<<it<<' ';
  cout<<endl;

*/

  vector<pair<int, int>> a(n * n);
  for (int i = 0; i < n * n; i++) {
    int t;
    cin >> t;
    t--;
    a[i] = mp(t / n, t % n);
  }

  d = vector<vector<int>>(n, vector<int>(n));
  in = vector<vector<bool>>(n, vector<bool>(n, true));

  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      d[i][j] = min({i, n - i - 1, j, n - j - 1});

  ll res = 0;
  for (auto it : a) {
    // cout<<d[it.first][it.second]<<endl;
    res += d[it.first][it.second];
    in[it.first][it.second] = false;
    dfs(it.first, it.second);
  }

  cout << res << endl;
}