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

#include <bits/stdc++.h>
#define pb push_back
#define pll pair<ll, ll>
#define mp make_pair
#define pyshnapyshnakaa                                                        \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#define plll pair<ll, pair<ll, ll>>
#define pllll pair<pair<ll, ll>, pair<ll, ll>>
#define psl pair<string, ll>
#define all(a) a.begin(), a.end()
#define vvl vector<vector<ll>>

typedef long long ll;

typedef long double ld;

using namespace std;

const ll maxn = 5e2 + 10;

ll n, m, k, t;

bool lft[maxn][maxn];

vector<pll> go = {{0, 1}, {1, 0}, {0, -1}, {-1, 0}};

vector<ll> A;

inline bool in(ll x, ll y) { return x >= 0 && y >= 0 && x < n && y < n; }

inline pll get_coord(ll c) {
  ll x = c / n, y = c % n;
  return mp(x, y);
}

inline ll geti(ll x, ll y) { return x * n + y; }

ll ANS[maxn][maxn];

void dfs(ll x, ll y) {
  for (auto p : go) {
    ll nx = x + p.first, ny = y + p.second;
    if (!in(nx, ny)) {
      continue;
    }
    ll dans = ANS[x][y];
    if (!lft[x][y]) {
      dans++;
    }
    if (dans < ANS[nx][ny]) {
      ANS[nx][ny] = dans;
      dfs(nx, ny);
    }
  }
}

int main() {
  pyshnapyshnakaa;
  ll q, w, e, a, b, c;
  cin >> n;
  A.resize(n * n);
  for (q = 0; q < n * n; q++) {
    cin >> A[q];
    A[q]--;
  }
  for (q = 0; q < n; q++) {
    for (w = 0; w < n; w++) {
      ANS[q][w] = min({q, n - q - 1, w, n - w - 1});
    }
  }
  ll ans = 0;
  for (q = 0; q < n * n; q++) {
    pll p = get_coord(A[q]);
    ans += ANS[p.first][p.second];
    lft[p.first][p.second] = 1;
    dfs(p.first, p.second);
  }
  cout << ans;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define pb push_back
#define pll pair<ll, ll>
#define mp make_pair
#define pyshnapyshnakaa                                                        \\
  ios_base ::sync_with_stdio(0);                                               \\
  cin.tie(0);                                                                  \\
  cout.tie(0);
#define x first
#define y second
#pragma GCC optimize("O3")
// #pragma GCC optimize("Ofast")
// #pragma GCC optimize("unroll-loops")
#define plll pair<ll, pair<ll, ll>>
#define pllll pair<pair<ll, ll>, pair<ll, ll>>
#define psl pair<string, ll>
#define all(a) a.begin(), a.end()
#define vvl vector<vector<ll>>

typedef int ll;

typedef long double ld;

using namespace std;

const ll maxn = 5e2 + 10;
// const ll inf = 1e9;

ll n, m, k, t;

// bool used[maxn][maxn][maxn];

ll dp[maxn][maxn][maxn];

ll I[maxn][maxn];

vector<pll> P[maxn * maxn];

vector<pll> go{{0, 1}, {0, -1}, {1, 0}, {-1, 0}};

inline bool in(ll x, ll y) { return x >= 0 && y >= 0 && x < n && y < n; }

inline pll get_coord(ll c) {
  ll x = c / n, y = c % n;
  return mp(x, y);
}

inline ll geti(ll x, ll y) { return x * n + y; }

inline ll getdp(ll x, ll y, ll h) {
  // if (used[x][y][h]) {
  //     return 0;
  // }
  // cout << x << " " << y << " " << h << endl;
  dp[x][y][h] = n * n;
  // used[x][y][h] = 1;
  for (auto p : go) {
    ll x1 = x + p.first, y1 = y + p.second;
    if (!in(x1, y1)) {
      dp[x][y][h] = 0;
      continue;
    }
    if (h != 0) {
      ll dans1 = dp[x1][y1][h - 1];
      dp[x][y][h] = min(dp[x][y][h], dans1);
    }
    // ll dans2 = max(getdp(x1, y1, h), I[x1][y1]);
    // dp[x][y][h] = min(dp[x][y][h], dans2);
  }
  return dp[x][y][h];
}

// void getdp1(ll x, ll y, ll h) {
//     for (auto p : go) {
//         ll x1 = x + p.first, y1 = y + p.second;
//         if (!in(x1, y1)) {
//             continue;
//         }
//         // if (h != 0) {
//         //     ll dans1 = getdp(x1, y1, h - 1);
//         //     dp[x][y][h] = min(dp[x][y][h], dans1);
//         // }
//         ll dans2 = max(dp[x][y][h], I[x][y]);
//         dp[x1][y1][h] = min(dp[x1][y1][h], dans2);
//     }
// }

int main() {
  pyshnapyshnakaa;
  ll q, w, e, a, b, c;
  cin >> n;
  vector<ll> A(n * n);
  for (q = 0; q < n * n; q++) {
    cin >> A[q];
    A[q]--;
    pll p = get_coord(A[q]);
    I[p.first][p.second] = q;
  }
  // for (q = 0; q < n; q++) {
  //     for (w = 0; w < n; w++) {
  //         if (q == 0 || w == 0 || q == n - 1 || w == n - 1) {
  //             for (e = 0; e <= n; e++) {
  //                 used[q][w][e] = 1;
  //             }
  //         }
  //     }
  // }
  for (e = 0; e <= n; e++) {
    for (q = 0; q < n; q++) {
      for (w = 0; w < n; w++) {
        getdp(q, w, e);
      }
    }
    // vector <vector <pll> > P(n * n + 1);
    for (q = 0; q <= n * n; q++) {
      P[q].clear();
    }
    // vector <bool> used(n * n + 5);
    for (q = 0; q < n; q++) {
      for (w = 0; w < n; w++) {
        P[dp[q][w][e]].pb(mp(q, w));
        // cout << q << " " << w << " " << e << " " << dp[q][w][e] << endl;
        // P.insert(mp(dp[q][w][e], mp(q, w)));
      }
    }
    for (ll i = 0; i <= n * n; i++) {
      // cout << "FALL " << i << endl;
      for (ll j = 0; j < P[i].size(); j++) {
        pll p = P[i][j];
        if (dp[p.first][p.second][e] != i) {
          continue;
        }
        ll x = p.first, y = p.second, h = e;
        for (auto p : go) {
          ll x1 = x + p.first, y1 = y + p.second;
          if (!in(x1, y1)) {
            continue;
          }
          if (dp[x1][y1][h] > dp[x][y][h]) {
            ll dans2 = max(dp[x][y][h], I[x][y]);
            ll prev = dp[x1][y1][h];
            dp[x1][y1][h] = min(dp[x1][y1][h], dans2);
            if (prev != dp[x1][y1][h]) {
              P[dp[x1][y1][h]].pb(mp(x1, y1));
            }
          }
        }
      }
    }
  }
  ll ans = 0;
  for (q = 0; q < n; q++) {
    for (w = 0; w < n; w++) {
      for (e = 0; e <= n; e++) {
        // cout << q << " " << w << " " << e << " " << dp[q][w][e] << endl;
        if (dp[q][w][e] <= I[q][w]) {
          ans += e;
          break;
        }
      }
    }
  }
  cout << ans;
  return 0;
}