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

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= a; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

struct Point {
  int x, y;
};

typedef vector<vector<P>> Matrix;

int W, H;
Matrix A;
vector<P> Human;

Matrix dot(const Matrix &a, const Matrix &b) {
  Matrix res(H, vector<P>(W, {-1, -1}));
  REP(y, H) REP(x, W) {
    P p = a[y][x];
    res[y][x] = b[p.second][p.first];
  }
  return res;
}
vector<P> dot(const vector<P> &human, const Matrix &conv) {
  vector<P> res;
  EACH(h, human) { res.pb(conv[h.second][h.first]); }
  return res;
}
Matrix E(int w, int h) {
  Matrix res(h, vector<P>(w, {-1, -1}));
  REP(y, h) REP(x, w) { res[y][x] = {x, y}; }
  return res;
}
Matrix power(Matrix X, int n) {
  Matrix res = E(W, H);
  for (ll i = 1; i <= n; i <<= 1) {
    if (i & n)
      res = dot(res, X);
    X = dot(X, X);
  }
  return res;
}

bool is_conflict(int step) {
  vector<P> after = dot(Human, power(A, step));
  set<P> s;
  EACH(p, after) s.insert(p);
  return s.size() != after.size();
}
bool is_dup(vector<P> after) {
  int ps = after.size();
  sort(ALL(after));
  after.erase(unique(ALL(after)), after.end());
  return after.size() != ps;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> H >> W >> n;
  A.assign(H, vector<P>(W, {-1, -1}));
  REP(y, H) REP(x, W) {
    int ny, nx;
    cin >> ny >> nx;
    A[y][x] = {nx, ny};
  }
  REP(i, n) {
    int y, x;
    cin >> y >> x;
    Human.pb({x, y});
  }
  ll l = 0, r = W * H;
  if (!is_conflict(r)) {
    cout << -1 << endl;
  } else {
    int lg = 0;
    for (ll i = 1; i <= W * H; i <<= 1, ++lg)
      ;
    vector<Matrix> powX;
    powX.pb(A);
    REP(i, lg) { powX.pb(dot(powX[i], powX[i])); }
    Matrix X = E(W, H);
    ll ans = 0;
    RREP(i, lg) {
      Matrix nX = dot(X, powX[i]);
      if (!is_dup(dot(Human, nX))) {
        ans += (1 << i);
        X = nX;
      }
    }
    ++ans;
    cout << ans << endl;
    // REP(t, 30) {
    //     int m = (l + r) / 2;
    //     if (is_conflict(m)) {
    //         r = m;
    //     }
    //     else {
    //         l = m;
    //     }
    // }
    // cout << r << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef pair<ll, ll> P;

#define EACH(i, a) for (auto &i : a)
#define FOR(i, a, b) for (ll i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (ll i = (b)-1; i >= a; i--)
#define REP(i, n) FOR(i, 0, n)
#define RREP(i, n) RFOR(i, 0, n)
#define pb push_back
#define ALL(a) (a).begin(), (a).end()

const ll linf = 1e18;
const int inf = 1e9;
const double eps = 1e-12;
const double pi = acos(-1);

template <typename T> istream &operator>>(istream &is, vector<T> &vec) {
  EACH(x, vec) is >> x;
  return is;
}
template <typename T> ostream &operator<<(ostream &os, vector<T> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << " ";
    os << vec[i];
  }
  return os;
}
template <typename T> ostream &operator<<(ostream &os, vector<vector<T>> &vec) {
  REP(i, vec.size()) {
    if (i)
      os << endl;
    os << vec[i];
  }
  return os;
}

struct Point {
  int x, y;
};

typedef vector<vector<P>> Matrix;

int W, H;
Matrix A;
vector<P> Human;

Matrix dot(const Matrix &a, const Matrix &b) {
  Matrix res(H, vector<P>(W, {-1, -1}));
  REP(y, H) REP(x, W) {
    P p = a[y][x];
    res[y][x] = b[p.second][p.first];
  }
  return res;
}
vector<P> dot(const vector<P> &human, const Matrix &conv) {
  vector<P> res;
  EACH(h, human) { res.pb(conv[h.second][h.first]); }
  return res;
}
Matrix E(int w, int h) {
  Matrix res(h, vector<P>(w, {-1, -1}));
  REP(y, h) REP(x, w) { res[y][x] = {x, y}; }
  return res;
}
Matrix power(Matrix X, int n) {
  Matrix res = E(W, H);
  for (ll i = 1; i <= n; i <<= 1) {
    if (i & n)
      res = dot(res, X);
    X = dot(X, X);
  }
  return res;
}

bool is_conflict(int step) {
  vector<P> after = dot(Human, power(A, step));
  set<P> s;
  EACH(p, after) s.insert(p);
  return s.size() != after.size();
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n;
  cin >> H >> W >> n;
  A.assign(H, vector<P>(W, {-1, -1}));
  REP(y, H) REP(x, W) {
    int ny, nx;
    cin >> ny >> nx;
    A[y][x] = {nx, ny};
  }
  REP(i, n) {
    int y, x;
    cin >> y >> x;
    Human.pb({x, y});
  }
  ll l = 0, r = W * H;
  if (!is_conflict(r)) {
    cout << -1 << endl;
  } else {
    REP(t, 30) {
      int m = (l + r) / 2;
      if (is_conflict(m)) {
        r = m;
      } else {
        l = m;
      }
    }
    cout << r << endl;
  }
}