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

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

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

struct F {
  int U;
  int N;
  vector<vint> A;
  vint R, C;
  vpint la;
  int dgn, dgn2;
  int sc;
  void init(int n, int u, vector<vint> a) {
    N = n;
    U = u;
    A = a;
    R = C = vint(N);
    la = vpint(1000000, {-1, -1});
    rep(i, N) rep(j, N) {
      a[i][j]--;
      la[a[i][j]] = {i, j};
    }
    sc = dgn = dgn2 = 0;
  }
  void add(int n) {
    n--;
    if (la[n].fi == -1)
      return;
    int y, x;
    tie(y, x) = la[n];
    if (++R[y] == N)
      sc++;
    if (++C[x] == N)
      sc++;
    if (y == x && (++dgn == N))
      sc++;
    if (N - 1 - y == x && (++dgn2 == N))
      sc++;
  }
  bool ok() {
    if (N == 1)
      return min(1ll, sc) >= U;
    return sc >= U;
  }
};

signed main() {
  int N, u, v, m;
  cin >> N >> u >> v >> m;
  vector<vint> A(N, vint(N));
  rep(i, N) rep(j, N) cin >> A[i][j];

  F usa;
  usa.init(N, u, A);
  rep(i, N) rep(j, N) cin >> A[i][j];
  F neko;
  neko.init(N, v, A);

  vint qs(m);
  rep(i, m) cin >> qs[i];
  rep(i, m) {
    usa.add(qs[i]);
    neko.add(qs[i]);
    if (usa.ok() && neko.ok()) {
      cout << "DRAW" << endl;
      return 0;
    }
    if (usa.ok()) {
      cout << "USAGI" << endl;
      return 0;
    }
    if (neko.ok()) {
      cout << "NEKO" << endl;
      return 0;
    }
  }
  cout << "DRAW" << endl;
  return 0;
}

Error: TLE

Fixed submission:

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

#define int long long
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<pint> vpint;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define reps(i, f, n) for (int i = (f); i < (n); i++)
#define all(v) (v).begin(), (v).end()
#define each(it, v)                                                            \\
  for (__typeof((v).begin()) it = (v).begin(); it != (v).end(); it++)
#define pb push_back
#define fi first
#define se second
template <typename A, typename B> inline void chmin(A &a, B b) {
  if (a > b)
    a = b;
}
template <typename A, typename B> inline void chmax(A &a, B b) {
  if (a < b)
    a = b;
}

struct F {
  int U;
  int N;
  vector<vint> A;
  vint R, C;
  vpint la;
  int dgn, dgn2;
  int sc;
  void init(int n, int u, vector<vint> a) {
    N = n;
    U = u;
    A = a;
    R = C = vint(N);
    la = vpint(1000000);
    rep(i, N) rep(j, N) {
      a[i][j]--;
      if (a[i][j] < 0 || a[i][j] >= N * N)
        for (;;)
          ;
      la[a[i][j]] = {i, j};
    }
    sc = dgn = dgn2 = 0;
  }
  void add(int n) {
    n--;
    if (n >= N * N)
      return;
    int y, x;
    tie(y, x) = la[n];
    if (++R[y] == N)
      sc++;
    if (++C[x] == N)
      sc++;
    if (y == x && (++dgn == N))
      sc++;
    if (N - 1 - y == x && (++dgn2 == N))
      sc++;
  }
  bool ok() { return sc >= U; }
};

signed main() {
  int N, u, v, m;
  cin >> N >> u >> v >> m;
  vector<vint> A(N, vint(N));
  rep(i, N) rep(j, N) cin >> A[i][j];

  F usa;
  usa.init(N, u, A);
  rep(i, N) rep(j, N) cin >> A[i][j];
  F neko;
  neko.init(N, v, A);

  vint qs(m);
  rep(i, m) cin >> qs[i];
  rep(i, m) {
    usa.add(qs[i]);
    neko.add(qs[i]);
    if (usa.ok() && neko.ok()) {
      cout << "DRAW" << endl;
      return 0;
    }
    if (usa.ok()) {
      cout << "USAGI" << endl;
      return 0;
    }
    if (neko.ok()) {
      cout << "NEKO" << endl;
      return 0;
    }
  }
  cout << "DRAW" << endl;
  return 0;
}