Problem p01358 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")

#include <bits/stdc++.h>

// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i, n) for (long long i = 0, i##_len = (n); i < i##_len; i++)
#define RANGE(i, is, ie)                                                       \\
  for (long long i = (is), i##_end = (ie); i <= i##_end; i++)
#define DSRNG(i, is, ie)                                                       \\
  for (long long i = (is), i##_end = (ie); i >= i##_end; i--)
#define UNIQUE(v)                                                              \\
  {                                                                            \\
    sort((v).begin(), (v).end());                                              \\
    (v).erase(unique((v).begin(), (v).end()), (v).end());                      \\
  }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) (q ? "Yes" : "No")
#define YES(q) (q ? "YES" : "NO")
#define DUMP(q)                                                                \\
  cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q)                                                             \\
  cerr << "[DEBUG] " #q ": [";                                                 \\
  REP(dumpall_i, (q).size()) {                                                 \\
    cerr << q[dumpall_i] << (dumpall_i == (q).size() - 1 ? "" : ", ");         \\
  }                                                                            \\
  cerr << "] at " __FILE__ ":" << __LINE__ << endl

// gcc拡張マクロ
#define gcd __gcd
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// エイリアス
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using namespace std;

// モジュール

// 処理内容
int main() {

  ll n, u, v, m;
  cin >> n >> u >> v >> m;
  vector<vector<ll>> udrawn(n, vector<ll>(n)), vdrawn(n, vector<ll>(n));
  REP(i, n) REP(j, n) cin >> udrawn[i][j];
  REP(i, n) REP(j, n) cin >> vdrawn[i][j];
  vector<ll> cards(m);
  REP(i, m) cin >> cards[i];

  if (n == 1) {
    ll udr = udrawn[0][0];
    ll vdr = vdrawn[0][0];
    ll upt = 0, vpt = 0;
    REP(i, m) {
      if (cards[i] == udr)
        upt++;
      if (cards[i] == vdr)
        vpt++;
      if (upt >= u && vpt >= v) {
        std::cout << "DRAW" << endl;
        return 0;
      } else if (vpt >= v) {
        std::cout << "NEKO" << endl;
        return 0;
      } else if (upt >= u) {
        std::cout << "USAGI" << endl;
        return 0;
      }
    }
    std::cout << "DRAW" << endl;
    return 0;
  }

  vector<ll> udic(1000001, -1), vdic(1000001, -1);
  REP(i, n) REP(j, n) {
    udic[udrawn[i][j]] = i * n + j;
    vdic[vdrawn[i][j]] = i * n + j;
  }

  vector<ll> uhitr(n, 0), uhitc(n, 0);
  ll uhitd0 = 0, uhitd1 = 0;
  vector<ll> vhitr(n, 0), vhitc(n, 0);
  ll vhitd0 = 0, vhitd1 = 0;
  ll ubingo = 0, vbingo = 0;
  REP(i, m) {
    ll iu = udic[cards[i]];
    ll iv = vdic[cards[i]];
    if (iu != -1) {
      ll i = iu / n;
      ll j = iu % n;
      uhitr[i]++;
      uhitc[j]++;
      if (i == j)
        uhitd0++;
      if (i + j == n - 1)
        uhitd1++;
      if (uhitr[i] == n)
        ubingo++;
      if (uhitc[j] == n)
        ubingo++;
      if (i == j)
        if (uhitd0 == n)
          ubingo++;
      if (i + j == n - 1)
        if (uhitd1 == n)
          ubingo++;
    }
    if (iv != -1) {
      ll i = iv / n;
      ll j = iv % n;
      vhitr[i]++;
      vhitc[j]++;
      if (i == j)
        vhitd0++;
      if (i + j == n - 1)
        vhitd1++;
      if (vhitr[i] == n)
        vbingo++;
      if (vhitc[j] == n)
        vbingo++;
      if (i == j)
        if (vhitd0 == n)
          vbingo++;
      if (i + j == n - 1)
        if (vhitd1 == n)
          vbingo++;
    }
    bool uwon = ubingo >= u;
    bool vwon = vbingo >= v;
    // cerr << "TURN " << (i+1) << ": " << ubingo << " v.s. " << vbingo << endl;
    if (uwon && vwon) {
      std::cout << "DRAW" << endl;
      return 0;
    } else if (uwon) {
      std::cout << "USAGI" << endl;
      return 0;
    } else if (vwon) {
      std::cout << "NEKO" << endl;
      return 0;
    }
  }

  std::cout << "DRAW" << endl;
}


Error: 0
TURN 1: 0 v.s. 0
TURN 2: 0 v.s. 0
TURN 3: 0 v.s. 0
TURN 4: 0 v.s. 0
TURN 5: 0 v.s. 0
TURN 6: 0 v.s. 0
TURN 7: 0 v.s. 0
TURN 8: 2 v.s. 1


Fixed submission:

#pragma GCC optimize("O3")
#pragma GCC target("tune=native")
#pragma GCC target("avx")

#include <bits/stdc++.h>

// 汎用マクロ
#define ALL_OF(x) (x).begin(), (x).end()
#define REP(i, n) for (long long i = 0, i##_len = (n); i < i##_len; i++)
#define RANGE(i, is, ie)                                                       \\
  for (long long i = (is), i##_end = (ie); i <= i##_end; i++)
#define DSRNG(i, is, ie)                                                       \\
  for (long long i = (is), i##_end = (ie); i >= i##_end; i--)
#define UNIQUE(v)                                                              \\
  {                                                                            \\
    sort((v).begin(), (v).end());                                              \\
    (v).erase(unique((v).begin(), (v).end()), (v).end());                      \\
  }
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
#define INF 0x7FFFFFFF
#define LINF 0x7FFFFFFFFFFFFFFFLL
#define Yes(q) (q ? "Yes" : "No")
#define YES(q) (q ? "YES" : "NO")
#define DUMP(q)                                                                \\
  cerr << "[DEBUG] " #q ": " << (q) << " at " __FILE__ ":" << __LINE__ << endl
#define DUMPALL(q)                                                             \\
  cerr << "[DEBUG] " #q ": [";                                                 \\
  REP(dumpall_i, (q).size()) {                                                 \\
    cerr << q[dumpall_i] << (dumpall_i == (q).size() - 1 ? "" : ", ");         \\
  }                                                                            \\
  cerr << "] at " __FILE__ ":" << __LINE__ << endl

// gcc拡張マクロ
#define gcd __gcd
#define popcount __builtin_popcount
#define popcountll __builtin_popcountll

// エイリアス
using ll = long long;
using ull = unsigned long long;
using ld = long double;
using namespace std;

// モジュール

// 処理内容
int main() {

  ll n, u, v, m;
  cin >> n >> u >> v >> m;
  vector<vector<ll>> udrawn(n, vector<ll>(n)), vdrawn(n, vector<ll>(n));
  REP(i, n) REP(j, n) cin >> udrawn[i][j];
  REP(i, n) REP(j, n) cin >> vdrawn[i][j];
  vector<ll> cards(m);
  REP(i, m) cin >> cards[i];

  if (n == 1) {
    ll udr = udrawn[0][0];
    ll vdr = vdrawn[0][0];
    ll upt = 0, vpt = 0;
    REP(i, m) {
      if (cards[i] == udr)
        upt++;
      if (cards[i] == vdr)
        vpt++;
      if (upt >= u && vpt >= v) {
        std::cout << "DRAW" << endl;
        return 0;
      } else if (vpt >= v) {
        std::cout << "NEKO" << endl;
        return 0;
      } else if (upt >= u) {
        std::cout << "USAGI" << endl;
        return 0;
      }
    }
    return 0;
  }

  vector<ll> udic(1000001, -1), vdic(1000001, -1);
  REP(i, n) REP(j, n) {
    udic[udrawn[i][j]] = i * n + j;
    vdic[vdrawn[i][j]] = i * n + j;
  }

  vector<ll> uhitr(n, 0), uhitc(n, 0);
  ll uhitd0 = 0, uhitd1 = 0;
  vector<ll> vhitr(n, 0), vhitc(n, 0);
  ll vhitd0 = 0, vhitd1 = 0;
  ll ubingo = 0, vbingo = 0;
  REP(i, m) {
    ll iu = udic[cards[i]];
    ll iv = vdic[cards[i]];
    if (iu != -1) {
      ll i = iu / n;
      ll j = iu % n;
      uhitr[i]++;
      uhitc[j]++;
      if (i == j)
        uhitd0++;
      if (i + j == n - 1)
        uhitd1++;
      if (uhitr[i] == n)
        ubingo++;
      if (uhitc[j] == n)
        ubingo++;
      if (i == j)
        if (uhitd0 == n)
          ubingo++;
      if (i + j == n - 1)
        if (uhitd1 == n)
          ubingo++;
    }
    if (iv != -1) {
      ll i = iv / n;
      ll j = iv % n;
      vhitr[i]++;
      vhitc[j]++;
      if (i == j)
        vhitd0++;
      if (i + j == n - 1)
        vhitd1++;
      if (vhitr[i] == n)
        vbingo++;
      if (vhitc[j] == n)
        vbingo++;
      if (i == j)
        if (vhitd0 == n)
          vbingo++;
      if (i + j == n - 1)
        if (vhitd1 == n)
          vbingo++;
    }
    bool uwon = ubingo >= u;
    bool vwon = vbingo >= v;
    cerr << "TURN " << (i + 1) << ": " << ubingo << " v.s. " << vbingo << endl;
    if (uwon && vwon) {
      std::cout << "DRAW" << endl;
      return 0;
    } else if (uwon) {
      std::cout << "USAGI" << endl;
      return 0;
    } else if (vwon) {
      std::cout << "NEKO" << endl;
      return 0;
    }
  }

  std::cout << "DRAW" << endl;
}