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

#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;

template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
  out << "[";
  for (int x : vec)
    out << x << ", ";
  out << "]";
  return out;
}

#if DEBUG && !ONLINE_JUDGE
#define dbg_var(x) cerr << #x << ": " << x << endl;
#endif

int N;
int P[500 * 500];
int cinema[500 * 500 + 1];
int q[500 * 500 + 1];

void compute() {
  int x = 0;
  vector<int> curr;
  vector<int> pross;
  vector<bool> inserito(N * N + 1, false);

  for (int i = 1; i <= N; i++)
    if (cinema[i])
      pross.push_back(i);
    else
      curr.push_back(i);
  for (int i = N * N - N + 1; i <= N * N; i++)
    if (cinema[i])
      pross.push_back(i);
    else
      curr.push_back(i);
  for (int i = N + 1; i < N * N - N; i += N)
    if (cinema[i])
      pross.push_back(i);
    else
      curr.push_back(i);
  for (int i = 2 * N; i < N * N; i += N)
    if (cinema[i])
      pross.push_back(i);
    else
      curr.push_back(i);

  for (int p : curr)
    inserito[p] = true;
  for (int p : pross)
    inserito[p] = true;

  for (int i = 0; i < N * N; i++) {
    if (curr.empty()) {
      x++;
      curr = pross;
      pross.clear();
    }
    int p = curr.back();
    curr.pop_back();
    q[p] = x;

    if (p > N and not inserito[p - N]) {
      inserito[p - N] = true;
      if (cinema[p - N])
        pross.push_back(p - N);
      else
        curr.push_back(p - N);
    }
    if (p <= (N - 1) * N and not inserito[p + N]) {
      inserito[p + N] = true;
      if (cinema[p + N])
        pross.push_back(p + N);
      else
        curr.push_back(p + N);
    }
    if ((p % N != 0) and not inserito[p + 1]) {
      inserito[p + 1] = true;
      if (cinema[p + 1])
        pross.push_back(p + 1);
      else
        curr.push_back(p + 1);
    }
    if ((p % N != 1) and not inserito[p - 1]) {
      inserito[p - 1] = true;
      if (cinema[p - 1])
        pross.push_back(p - 1);
      else
        curr.push_back(p - 1);
    }
  }
}

void aggiorna(int pos) {
  cinema[pos] = 0;
  vector<int> curr = {pos};
  vector<bool> aggiornato(N * N + 1, false);

  while (not curr.empty()) {
    int p = curr.back();
    curr.pop_back();
    if (aggiornato[p])
      continue;
    q[p]--;
    aggiornato[p] = true;

    if (p > N and q[p - N] > q[p] + cinema[p - N])
      curr.push_back(p - N);
    if (p <= (N - 1) * N and q[p + N] > q[p] + cinema[p + N])
      curr.push_back(p + N);
    if ((p % N != 0) and q[p + 1] > q[p] + cinema[p + 1])
      curr.push_back(p + 1);
    if ((p % N != 1) and q[p - 1] > q[p] + cinema[p - 1])
      curr.push_back(p - 1);
  }
}

void print() {
  for (int i = 0; i < N; i++) {
    for (int j = 1; j <= N; j++) {
      cerr << q[N * i + j] << ' ';
    }
    cerr << endl;
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N;
  for (int i = 0; i < N * N; i++) {
    cin >> P[i];
    cinema[i + 1] = 1;
  }
  compute();
  //~ print();
  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    aggiorna(P[i]);
    //~ cerr << "AGG " << P[i] << endl;
    //~ print();
    //~ cerr << P[i] << ' ' << q[P[i]] << endl;
    ans += q[P[i]];
  }
  cout << ans << '\\n';

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long int LL;

template <typename T> ostream &operator<<(ostream &out, const vector<T> &vec) {
  out << "[";
  for (int x : vec)
    out << x << ", ";
  out << "]";
  return out;
}

#if DEBUG && !ONLINE_JUDGE
#define dbg_var(x) cerr << #x << ": " << x << endl;
#endif

int N;
int P[500 * 500];
int cinema[500 * 500 + 1];
int q[500 * 500 + 1];

void compute() {
  int x = 0;
  vector<int> curr;
  vector<int> pross;
  vector<bool> inserito(N * N + 1, false);

  for (int i = 1; i <= N; i++)
    if (cinema[i])
      pross.push_back(i);
    else
      curr.push_back(i);
  for (int i = N * N - N + 1; i <= N * N; i++)
    if (cinema[i])
      pross.push_back(i);
    else
      curr.push_back(i);
  for (int i = N + 1; i < N * N - N; i += N)
    if (cinema[i])
      pross.push_back(i);
    else
      curr.push_back(i);
  for (int i = 2 * N; i < N * N; i += N)
    if (cinema[i])
      pross.push_back(i);
    else
      curr.push_back(i);

  for (int p : curr)
    inserito[p] = true;
  for (int p : pross)
    inserito[p] = true;

  for (int i = 0; i < N * N; i++) {
    if (curr.empty()) {
      x++;
      curr = pross;
      pross.clear();
    }
    int p = curr.back();
    curr.pop_back();
    q[p] = x;

    if (p > N and not inserito[p - N]) {
      inserito[p - N] = true;
      if (cinema[p - N])
        pross.push_back(p - N);
      else
        curr.push_back(p - N);
    }
    if (p <= (N - 1) * N and not inserito[p + N]) {
      inserito[p + N] = true;
      if (cinema[p + N])
        pross.push_back(p + N);
      else
        curr.push_back(p + N);
    }
    if ((p % N != 0) and not inserito[p + 1]) {
      inserito[p + 1] = true;
      if (cinema[p + 1])
        pross.push_back(p + 1);
      else
        curr.push_back(p + 1);
    }
    if ((p % N != 1) and not inserito[p - 1]) {
      inserito[p - 1] = true;
      if (cinema[p - 1])
        pross.push_back(p - 1);
      else
        curr.push_back(p - 1);
    }
  }
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);

  cin >> N;
  for (int i = 0; i < N * N; i++) {
    cin >> P[i];
    cinema[i] = 1;
  }

  int ans = 0;
  for (int i = 0; i < N * N; i++) {
    cinema[P[i]] = 0;
    compute();
    ans += q[P[i]];
  }
  cout << ans << '\\n';

  return 0;
}