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

/*input
6
11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10
29 28 33 12 6 5 30
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define eb emplace_back
#define pb push_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \\
  (sort(c.begin(), c.end()),                                                   \\
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (int)(lower_bound(c.begin(), c.end(), x) - c.begin())
#define EL cout << '\\n'
#define BS(a, x) binary_search(ALL(a), x)
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> P) {
  out << '(' << P.F << ',' << P.S << ')';
  return out;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> V) {
  REP(i, SZ(V)) out << V[i] << ((i != SZ(V) - 1) ? " " : "");
  return out;
}
//}}}
const ll maxn = 505;
const ll maxlg = 20;
const ll INF64 = 1e18;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const ld PI = acos(-1);
const ld eps = 1e-9;
// const ll p=880301;
// const ll P=31;

ll mypow(ll a, ll b) {
  ll res = 1LL;
  while (b) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

int n;
int p[maxn * maxn];

int d[maxn][maxn];

bool vis[maxn][maxn];
vector<pii> q;

bool isValid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < n; }

ll sumD;

int main() {
  IOS;
  cin >> n;

  REP(i, n) REP(j, n) {
    d[i][j] = min({i + 1, n - i, j + 1, n - j});
    sumD += d[i][j];
  }
  REP(i, n) REP(j, n) vis[i][j] = 1;

  int ans = 0;

  REP(I, n * n) {
    int u, v;
    {
      int tmp;
      cin >> tmp;
      tmp--;
      u = tmp / n;
      v = tmp % n;
    }
    ans += d[u][v];
    d[u][v]--;
    vis[u][v] = 0;

    int pt = 0;
    q.eb(u, v);

    while (pt < SZ(q)) {
      int x = q[pt].F, y = q[pt].S;
      pt++;

      {
        int _x = x + 1, _y = y;
        if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
          d[_x][_y] = d[x][y] + vis[_x][_y];
          q.eb(_x, _y);
        }
      }
      {
        int _x = x - 1, _y = y;
        if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
          d[_x][_y] = d[x][y] + vis[_x][_y];
          q.eb(_x, _y);
        }
      }
      {
        int _x = x, _y = y + 1;
        if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
          d[_x][_y] = d[x][y] + vis[_x][_y];
          q.eb(_x, _y);
        }
      }
      {
        int _x = x, _y = y - 1;
        if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
          d[_x][_y] = d[x][y] + vis[_x][_y];
          q.eb(_x, _y);
        }
      }
    }
    // REP(i,n) REP(j,n) cout<<d[i][j]<<" \\n"[j==n-1];
    // cout<<endl;

    q.clear();
  }
  cout << ans - n * n << '\\n';
  return 0;
}


Error: TLE

Fixed submission:

/*input
6
11 21 35 22 7 36 27 34 8 20 15 13 16 1 24 3 2 17 26 9 18 32 31 23 19 14 4 25 10
29 28 33 12 6 5 30
*/
#include <bits/stdc++.h>
#pragma GCC optimize("unroll-loops,no-stack-protector")
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef pair<ld, ld> pdd;
#define IOS                                                                    \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0)
#define ALL(a) a.begin(), a.end()
#define SZ(a) ((int)a.size())
#define F first
#define S second
#define REP(i, n) for (int i = 0; i < ((int)n); i++)
#define eb emplace_back
#define pb push_back
#define MP(a, b) make_pair(a, b)
#define SORT_UNIQUE(c)                                                         \\
  (sort(c.begin(), c.end()),                                                   \\
   c.resize(distance(c.begin(), unique(c.begin(), c.end()))))
#define GET_POS(c, x) (int)(lower_bound(c.begin(), c.end(), x) - c.begin())
#define EL cout << '\\n'
#define BS(a, x) binary_search(ALL(a), x)
template <typename T1, typename T2>
ostream &operator<<(ostream &out, pair<T1, T2> P) {
  out << '(' << P.F << ',' << P.S << ')';
  return out;
}
template <typename T> ostream &operator<<(ostream &out, vector<T> V) {
  REP(i, SZ(V)) out << V[i] << ((i != SZ(V) - 1) ? " " : "");
  return out;
}
//}}}
const ll maxn = 505;
const ll maxlg = 20;
const ll INF64 = 1e18;
const int INF = 0x3f3f3f3f;
const ll MOD = ll(1e9 + 7);
const ld PI = acos(-1);
const ld eps = 1e-9;
// const ll p=880301;
// const ll P=31;

ll mypow(ll a, ll b) {
  ll res = 1LL;
  while (b) {
    if (b & 1)
      res = res * a % MOD;
    a = a * a % MOD;
    b >>= 1;
  }
  return res;
}

int n;
int p[maxn * maxn];

int d[maxn][maxn];

bool vis[maxn][maxn];
vector<pii> q;

bool isValid(int x, int y) { return x >= 0 && x < n && y >= 0 && y < n; }

ll sumD;

int main() {
  IOS;
  cin >> n;

  REP(i, n) REP(j, n) {
    d[i][j] = min({i + 1, n - i, j + 1, n - j});
    sumD += d[i][j];
  }
  REP(i, n) REP(j, n) vis[i][j] = 1;

  int ans = 0;

  REP(I, n * n) {
    int u, v;
    {
      int tmp;
      cin >> tmp;
      tmp--;
      u = tmp / n;
      v = tmp % n;
    }
    ans += d[u][v];
    d[u][v]--;
    vis[u][v] = 0;

    int pt = 0;
    q.eb(u, v);

    while (pt < SZ(q)) {
      int x = q[pt].F, y = q[pt].S;
      pt++;

      {
        int _x = x + 1, _y = y;
        if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
          d[_x][_y] = d[x][y] + vis[_x][_y];
          q.eb(_x, _y);
        }
      }
      {
        int _x = x - 1, _y = y;
        if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
          d[_x][_y] = d[x][y] + vis[_x][_y];
          q.eb(_x, _y);
        }
      }
      {
        int _x = x, _y = y + 1;
        if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
          d[_x][_y] = d[x][y] + vis[_x][_y];
          q.eb(_x, _y);
        }
      }
      {
        int _x = x, _y = y - 1;
        if (isValid(_x, _y) && d[_x][_y] > d[x][y] + vis[_x][_y]) {
          d[_x][_y] = d[x][y] + vis[_x][_y];
          q.eb(_x, _y);
        }
      }
    }
    // REP(i,n) REP(j,n) cout<<d[i][j]<<" \\n"[j==n-1];
    // cout<<endl;
    ll s = 0;
    REP(i, n) REP(j, n) s += d[i][j];
    assert(SZ(q) <= sumD - s);
    sumD = s;
    q.clear();
  }
  cout << ans - n * n << '\\n';
  return 0;
}