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

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (ll i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

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

ll N;
struct pos {
  ll x, y;
  bool operator<(const pos &a) const {
    if (x == a.x)
      return y < a.y;
    else
      return x < a.x;
  }
};

pos mpos(ll x, ll y) {
  pos tmp = {x, y};
  return tmp;
}

ll calc(pos a) { return a.x + 1 + a.y * N; }

vector<ll> grid;
vector<bool> gridbool;

void del(pos now, ll c);

void update(pos now, ll c) {
  grid[calc(now)] = c;
  rep(i, 0, 4) {
    if (now.x + dx[i] <= N - 1 && now.x + dx[i] >= 0 && now.y + dy[i] >= 0 &&
        now.y + dy[i] <= N - 1) {
      pos nxt = mpos(now.x + dx[i], now.y + dy[i]);
      if (grid[calc(nxt)] > grid[calc(now)] + 1) {
        if (gridbool[calc(nxt)])
          update(nxt, grid[calc(now)] + 1);
        else
          del(nxt, grid[calc(now)] + 1);
      }
    }
  }
}

void del(pos now, ll c) {
  grid[calc(now)] = c;
  rep(i, 0, 4) {
    if (now.x + dx[i] <= N - 1 && now.x + dx[i] >= 0 && now.y + dy[i] >= 0 &&
        now.y + dy[i] <= N - 1) {
      pos nxt = mpos(now.x + dx[i], now.y + dy[i]);
      if (grid[calc(nxt)] > grid[calc(now)]) {
        if (gridbool[calc(nxt)])
          update(nxt, grid[calc(now)]);
        else
          del(nxt, grid[calc(now)]);
      }
    }
  }
}

int main() {
  cin >> N;
  vector<pos> p(N * N);
  grid.resize(N * N + 1);
  gridbool.resize(N * N + 1);
  rep(i, 0, N * N) {
    ll a;
    cin >> a;
    ll tmp;
    if (a % N == 0)
      tmp = N;
    else
      tmp = a % N;
    p[i].x = tmp - 1;
    p[i].y = (a - 1) / N;
  }
  ll ans = 0;
  rep(i, 0, N) rep(j, 0, N) {
    pos tmp = mpos(i, j);
    grid[calc(tmp)] = min(min(N - 1 - i, i), min(N - 1 - j, j));
    gridbool[calc(tmp)] = true;
  }

  rep(i, 0, N * N) {
    ans += grid[calc(p[i])];
    // cout<<p[i].x<<p[i].y<<" "<<grid[p[i]]<<endl;
    gridbool[calc(p[i])] = false;
    del(p[i], grid[calc(p[i])]);
  }
  cout << ans;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

using namespace std;
typedef long long ll;
typedef vector<ll> v;
typedef vector<vector<ll>> vv;

#define MOD 1000000007
#define INF 1001001001
#define MIN -1001001001
#define rep(i, k, N) for (ll i = k; i < N; i++)
#define MP make_pair
#define MT make_tuple // tie,make_tuple は別物
#define PB push_back
#define PF push_front
#define all(x) (x).begin(), (x).end()

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

ll N;
struct pos {
  ll x, y;
  bool operator<(const pos &a) const {
    if (x == a.x)
      return y < a.y;
    else
      return x < a.x;
  }
};

pos mpos(ll x, ll y) {
  pos tmp = {x, y};
  return tmp;
}

map<pos, ll> grid;
map<pos, bool> gridbool;

void del(pos now, ll c);

void update(pos now, ll c) {
  grid[now] = c;
  rep(i, 0, 4) {
    if (now.x + dx[i] <= N - 1 && now.x + dx[i] >= 0 && now.y + dy[i] >= 0 &&
        now.y + dy[i] <= N - 1) {
      pos nxt = mpos(now.x + dx[i], now.y + dy[i]);
      if (grid[nxt] > grid[now] + 1) {
        if (gridbool[nxt])
          update(nxt, grid[now] + 1);
        else
          del(nxt, grid[now] + 1);
      }
    }
  }
}

void del(pos now, ll c) {
  grid[now] = c;
  rep(i, 0, 4) {
    if (now.x + dx[i] <= N - 1 && now.x + dx[i] >= 0 && now.y + dy[i] >= 0 &&
        now.y + dy[i] <= N - 1) {
      pos nxt = mpos(now.x + dx[i], now.y + dy[i]);
      if (grid[nxt] > grid[now]) {
        if (gridbool[nxt])
          update(nxt, grid[now]);
        else
          del(nxt, grid[now]);
      }
    }
  }
}

int main() {
  cin >> N;
  vector<pos> p(N * N);
  rep(i, 0, N * N) {
    ll a;
    cin >> a;
    ll tmp;
    if (a % N == 0)
      tmp = N;
    else
      tmp = a % N;
    p[i].x = tmp - 1;
    p[i].y = (a - 1) / N;
  }
  ll ans = 0;
  rep(i, 0, N) rep(j, 0, N) {
    pos tmp = mpos(i, j);
    grid[tmp] = min(min(N - 1 - i, i), min(N - 1 - j, j));
    gridbool[tmp] = true;
  }

  rep(i, 0, N * N) {
    ans += grid[p[i]];
    // cout<<p[i].x<<p[i].y<<" "<<grid[p[i]]<<endl;
    gridbool[p[i]] = false;
    del(p[i], grid[p[i]]);
  }
  cout << ans;

  return 0;
}