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

#include <cmath>
#include <iostream>
#include <set>
#include <vector>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

#define MAX 510
typedef long long ll;
int H, W, N;
int r[MAX][MAX], c[MAX][MAX];
int Y[MAX * MAX], X[MAX * MAX];

const bool debug = false;

vector<int> calc(vector<int> &A, vector<int> &B) {
  vector<int> C((int)A.size());
  rep(i, (int)A.size()) { C[i] = B[A[i]]; }
  return C;
}

vector<int> tes(vector<vector<int>> &An, ll t) {
  vector<int> C(H * W);
  rep(i, H * W) C[i] = i;
  rep(i, 20) {
    if ((t >> i) & 1) {
      C = calc(An[i], C);
    }
  }
  return C;
}

bool check(ll t, vector<vector<int>> &An) {
  vector<int> C(H * W);
  rep(i, H * W) C[i] = i;
  rep(i, 20) {
    if ((t >> i) & 1) {
      C = calc(An[i], C);
    }
  }
  set<int> S;
  rep(i, N) {
    int x = X[i];
    int y = Y[i];
    if (S.count(C[y * W + x]))
      return false;
    S.insert(C[y * W + x]);
  }
  return true;
}

void compute() {
  vector<int> A(H * W);
  rep(i, H) rep(j, W) {
    int x = c[i][j];
    int y = r[i][j];
    A[i * W + j] = y * W + x;
  }
  if (debug) {
    cout << "A = ";
    rep(i, (int)A.size()) cout << A[i] << " ";
    puts("");
  }
  vector<vector<int>> An;
  An.push_back(A);
  rep(i, 20) { An.push_back(calc(An[i], An[i])); }
  if (debug) {
    rep(i, 5) {
      cout << "i = " << i << " (" << (1 << i) << ") :" << endl;
      rep(j, (int)An[i].size()) cout << An[i][j] << " ";
      puts("");
    }
  }

  if (debug) {
    puts("--->");
    vector<int> C = tes(An, 0);
    rep(i, H * W) cout << C[i] << " ";
    puts("");
    vector<int> a(H * W);
    rep(i, H * W) a[i] = i;
    a = calc(a, C);
    rep(i, H * W) cout << a[i] << " ";
    puts("");
    rep(i, N) {
      int x = X[i], y = Y[i];
      cout << x << "," << y << endl;
      cout << y * W + x << endl;
    }
    puts("<---");
  }

  ll L = 0, R = 300000LL;
  while (R - L) {
    ll M = (L + R) / 2LL;
    if (check(M, An))
      L = M + 1LL;
    else
      R = M;
  }
  if (L - 1LL == 300000LL - 1LL) {
    puts("-1");
  } else {
    cout << L << endl;
  }
}

int main() {
  cin >> H >> W >> N;
  rep(i, H) rep(j, W) { cin >> r[i][j] >> c[i][j]; }
  rep(i, N) cin >> Y[i] >> X[i];
  compute();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define REP(i, s, n) for (int i = s; i < n; ++i)
#define rep(i, n) REP(i, 0, n)

using namespace std;

#define MAX 510
typedef long long ll;
int H, W, N;
int r[MAX][MAX], c[MAX][MAX];
int Y[MAX * MAX], X[MAX * MAX];

const bool debug = false;

vector<int> calc(vector<int> &A, vector<int> &B) {
  vector<int> C((int)A.size());
  rep(i, (int)A.size()) { C[i] = B[A[i]]; }
  return C;
}

vector<int> tes(vector<vector<int>> &An, ll t) {
  vector<int> C(H * W);
  rep(i, H * W) C[i] = i;
  rep(i, 63) {
    if ((t >> i) & 1) {
      C = calc(An[i], C);
    }
  }
  return C;
}

bool check(ll t, vector<vector<int>> &An) {
  vector<int> C(H * W);
  rep(i, H * W) C[i] = i;
  rep(i, 63) {
    if ((t >> i) & 1) {
      C = calc(An[i], C);
    }
  }
  set<int> S;
  rep(i, N) {
    int x = X[i];
    int y = Y[i];
    if (S.count(C[y * W + x]))
      return false;
    S.insert(C[y * W + x]);
  }
  return true;
}

void compute() {
  vector<int> A(H * W);
  rep(i, H) rep(j, W) {
    int x = c[i][j];
    int y = r[i][j];
    A[i * W + j] = y * W + x;
  }
  if (debug) {
    cout << "A = ";
    rep(i, (int)A.size()) cout << A[i] << " ";
    puts("");
  }
  vector<vector<int>> An;
  An.push_back(A);
  rep(i, 50) { An.push_back(calc(An[i], An[i])); }
  if (debug) {
    rep(i, 5) {
      cout << "i = " << i << " (" << (1 << i) << ") :" << endl;
      rep(j, (int)An[i].size()) cout << An[i][j] << " ";
      puts("");
    }
  }

  if (debug) {
    puts("--->");
    vector<int> C = tes(An, 0);
    rep(i, H * W) cout << C[i] << " ";
    puts("");
    vector<int> a(H * W);
    rep(i, H * W) a[i] = i;
    a = calc(a, C);
    rep(i, H * W) cout << a[i] << " ";
    puts("");
    rep(i, N) {
      int x = X[i], y = Y[i];
      cout << x << "," << y << endl;
      cout << y * W + x << endl;
    }
    puts("<---");
  }

  ll L = 0, R = 1000000000000LL;
  while (R - L) {
    ll M = (L + R) / 2LL;
    if (check(M, An))
      L = M + 1LL;
    else
      R = M;
  }
  if (L - 1LL == 999999999999LL) {
    puts("-1");
  } else {
    cout << L << endl;
  }
}

int main() {
  cin >> H >> W >> N;
  rep(i, H) rep(j, W) { cin >> r[i][j] >> c[i][j]; }
  rep(i, N) cin >> Y[i] >> X[i];
  compute();
  return 0;
}