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

#include <bits/stdc++.h>
using namespace std;

#define li long long int
#define rep(i, to) for (li i = 0; i < ((li)(to)); i++)
#define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++)
#define pb push_back
#define sz(v) ((li)(v).size())
#define bgn(v) ((v).begin())
#define eend(v) ((v).end())
#define allof(v) (v).begin(), (v).end()
#define dodp(v, n) memset(v, (li)n, sizeof(v))
#define bit(n) (1ll << (li)(n))
#define mp(a, b) make_pair(a, b)
#define rin rep(i, n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;

#define INF bit(60)

#define DBGP 1

#define idp if (DBGP)
#define F first
#define S second
#define p2(a, b) idp cout << a << "\\t" << b << endl
#define p3(a, b, c) idp cout << a << "\\t" << b << "\\t" << c << endl
#define p4(a, b, c, d)                                                         \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << endl
#define p5(a, b, c, d, e)                                                      \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << "\\t" << e << endl
#define p6(a, b, c, d, e, f)                                                   \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << "\\t" << e << "\\t"    \\
           << f << endl
#define p7(a, b, c, d, e, f, g)                                                \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << "\\t" << e << "\\t"    \\
           << f << "\\t" << g << endl
#define p8(a, b, c, d, e, f, g, h)                                             \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << "\\t" << e << "\\t"    \\
           << f << "\\t" << g << "\\t" << h << endl
#define p9(a, b, c, d, e, f, g, h, i)                                          \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << "\\t" << e << "\\t"    \\
           << f << "\\t" << g << "\\t" << h << "\\t" << i << endl
#define p10(a, b, c, d, e, f, g, h, i, j)                                      \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << "\\t" << e << "\\t"    \\
           << f << "\\t" << g << "\\t" << h << "\\t" << i << "\\t" << j << endl
#define foreach(it, v)                                                         \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define p2p(x) idp p2((x).F, (x).S)
#define dump(x, n)                                                             \\
  idp {                                                                        \\
    rep(i, n) { cout << x[i] << " "; }                                         \\
    puts("");                                                                  \\
  }
#define dump2(x, n)                                                            \\
  idp {                                                                        \\
    rep(i, n) { cout << "[" << x[i].F << " , " << x[i].S << "] "; }            \\
    puts("");                                                                  \\
  }
#define dumpi(x)                                                               \\
  idp {                                                                        \\
    foreach (it, x) {                                                          \\
      cout << (*it) << " ";                                                    \\
    }                                                                          \\
    puts("");                                                                  \\
  }
#define dumpi2(x)                                                              \\
  idp {                                                                        \\
    foreach (it, x) {                                                          \\
      cout << "[" << (it)->F << " , " << (it)->S << "] ";                      \\
    }                                                                          \\
    puts("");                                                                  \\
  }

#define read2d(a, w, h) rep(i, h) rep(j, w) cin >> a[i][j]
#define dump2d(a, w, h)                                                        \\
  rep(i, h) {                                                                  \\
    rep(j, w) cout << a[i][j] << " ";                                          \\
    puts("");                                                                  \\
  }

typedef pair<li, li> PI;

li n;
li nn;

li value[505050];
bool live[505050];

inline PI toPos(li x) { return {x / n, x % n}; }

inline li fromPos(PI p) { return p.F * n + p.S; }

li dx[] = {-1, 1, 0, 0};
li dy[] = {0, 0, -1, 1};

int main() {
  cin >> n;
  li nn = n * n;
  rep(i, n) {
    rep(j, n) {
      li dx = min(j, n - j - 1);
      li dy = min(i, n - i - 1);
      value[fromPos({i, j})] = min(dx, dy);
      live[fromPos({i, j})] = true;
    }
  }
  li res = 0;
  rep(i, n * n) {
    li pos;
    cin >> pos;
    pos--;
    li tres = value[pos];
    res += tres;
    live[pos] = false;

    // run dijkstra
    queue<PI> pq;
    pq.push({-tres, pos});
    while (!pq.empty()) {
      li now_cost = -pq.front().F;
      li now = pq.front().S;
      pq.pop();
      if (value[now] < now_cost) {
        continue;
      }
      if (value[now] == now_cost && now != pos) {
        continue;
      }
      value[now] = now_cost;
      rep(k, 4) {
        PI next = {now / n + dy[k], now % n + dx[k]};
        if (min(next.F, next.S) < 0 || max(next.F, next.S) >= n) {
          continue;
        }
        li pn = fromPos(next);
        li next_cost = now_cost;
        if (live[now]) {
          next_cost++;
        }
        if (value[pn] <= next_cost) {
          continue;
        }

        pq.push({-next_cost, pn});
      }
    }
    if (0) {
      p2(i, tres);
      rep(j, n) {
        rep(k, n) {
          if (live[j * n + k]) {
            cout << value[j * n + k] << " ";
          } else {
            cout << "X ";
          }
        }
        cout << endl;
      }
    }
  }

  cout << res << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define li long long int
#define rep(i, to) for (li i = 0; i < ((li)(to)); i++)
#define repp(i, start, to) for (li i = (li)(start); i < ((li)(to)); i++)
#define pb push_back
#define sz(v) ((li)(v).size())
#define bgn(v) ((v).begin())
#define eend(v) ((v).end())
#define allof(v) (v).begin(), (v).end()
#define dodp(v, n) memset(v, (li)n, sizeof(v))
#define bit(n) (1ll << (li)(n))
#define mp(a, b) make_pair(a, b)
#define rin rep(i, n)
#define EPS 1e-12
#define ETOL 1e-8
#define MOD 1000000007
typedef pair<li, li> PI;

#define INF bit(60)

#define DBGP 1

#define idp if (DBGP)
#define F first
#define S second
#define p2(a, b) idp cout << a << "\\t" << b << endl
#define p3(a, b, c) idp cout << a << "\\t" << b << "\\t" << c << endl
#define p4(a, b, c, d)                                                         \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << endl
#define p5(a, b, c, d, e)                                                      \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << "\\t" << e << endl
#define p6(a, b, c, d, e, f)                                                   \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << "\\t" << e << "\\t"    \\
           << f << endl
#define p7(a, b, c, d, e, f, g)                                                \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << "\\t" << e << "\\t"    \\
           << f << "\\t" << g << endl
#define p8(a, b, c, d, e, f, g, h)                                             \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << "\\t" << e << "\\t"    \\
           << f << "\\t" << g << "\\t" << h << endl
#define p9(a, b, c, d, e, f, g, h, i)                                          \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << "\\t" << e << "\\t"    \\
           << f << "\\t" << g << "\\t" << h << "\\t" << i << endl
#define p10(a, b, c, d, e, f, g, h, i, j)                                      \\
  idp cout << a << "\\t" << b << "\\t" << c << "\\t" << d << "\\t" << e << "\\t"    \\
           << f << "\\t" << g << "\\t" << h << "\\t" << i << "\\t" << j << endl
#define foreach(it, v)                                                         \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); ++it)
#define p2p(x) idp p2((x).F, (x).S)
#define dump(x, n)                                                             \\
  idp {                                                                        \\
    rep(i, n) { cout << x[i] << " "; }                                         \\
    puts("");                                                                  \\
  }
#define dump2(x, n)                                                            \\
  idp {                                                                        \\
    rep(i, n) { cout << "[" << x[i].F << " , " << x[i].S << "] "; }            \\
    puts("");                                                                  \\
  }
#define dumpi(x)                                                               \\
  idp {                                                                        \\
    foreach (it, x) {                                                          \\
      cout << (*it) << " ";                                                    \\
    }                                                                          \\
    puts("");                                                                  \\
  }
#define dumpi2(x)                                                              \\
  idp {                                                                        \\
    foreach (it, x) {                                                          \\
      cout << "[" << (it)->F << " , " << (it)->S << "] ";                      \\
    }                                                                          \\
    puts("");                                                                  \\
  }

#define read2d(a, w, h) rep(i, h) rep(j, w) cin >> a[i][j]
#define dump2d(a, w, h)                                                        \\
  rep(i, h) {                                                                  \\
    rep(j, w) cout << a[i][j] << " ";                                          \\
    puts("");                                                                  \\
  }

typedef pair<li, li> PI;

li n;
li nn;

li value[505050];
bool live[505050];

inline PI toPos(li x) { return {x / n, x % n}; }

inline li fromPos(PI p) { return p.F * n + p.S; }

li dx[] = {-1, 1, 0, 0};
li dy[] = {0, 0, -1, 1};

int main() {
  cin >> n;
  li nn = n * n;
  rep(i, n) {
    rep(j, n) {
      li dx = min(j, n - j - 1);
      li dy = min(i, n - i - 1);
      value[fromPos({i, j})] = min(dx, dy);
      live[fromPos({i, j})] = true;
    }
  }
  li res = 0;
  rep(i, n * n) {
    li pos;
    cin >> pos;
    pos--;
    li tres = value[pos];
    res += tres;
    live[pos] = false;

    // run dijkstra
    priority_queue<PI> pq;
    pq.push({-tres, pos});
    while (!pq.empty()) {
      li now_cost = -pq.top().F;
      li now = pq.top().S;
      pq.pop();
      // p3(i, now, now_cost);
      if (value[now] < now_cost) {
        continue;
      }
      if (value[now] == now_cost && now != pos) {
        continue;
      }
      value[now] = now_cost;
      rep(k, 4) {
        PI next = {now / n + dy[k], now % n + dx[k]};
        if (min(next.F, next.S) < 0 || max(next.F, next.S) >= n) {
          continue;
        }
        li pn = fromPos(next);
        li next_cost = now_cost;
        if (live[now]) {
          next_cost++;
        }
        if (value[pn] <= next_cost) {
          continue;
        }
        pq.push({-next_cost, pn});
      }
    }
    if (0) {
      p2(i, tres);
      rep(j, n) {
        rep(k, n) {
          if (live[j * n + k]) {
            cout << value[j * n + k] << " ";
          } else {
            cout << "X ";
          }
        }
        cout << endl;
      }
    }
  }

  cout << res << endl;

  return 0;
}