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

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long int i64;
typedef pair<int, int> pint;

map<int, pint> usam, nekom;
int usum[2][501], nsum[2][501];
int m[100000];
int main() {
  int N, U, V, M;
  scanf("%d%d%d%d", &N, &U, &V, &M);
  int num;
  rep(i, N) rep(j, N) {
    scanf("%d", &num);
    usam[num] = pint(j, i);
  }
  rep(i, N) rep(j, N) {
    scanf("%d", &num);
    nekom[num] = pint(j, i);
  }
  rep(i, M) scanf("%d", m + i);
  rep(i, M) {
    if (usam.count(m[i])) {
      usum[0][usam[m[i]].first]++;
      usum[1][usam[m[i]].second]++;
      if (usam[m[i]].first == usam[m[i]].second)
        usum[0][N]++;
      if (usam[m[i]].first == N - 1 - usam[m[i]].second)
        usum[1][N]++;
    }
    if (nekom.count(m[i])) {
      nsum[0][nekom[m[i]].first]++;
      nsum[1][nekom[m[i]].second]++;
      if (nekom[m[i]].first == nekom[m[i]].second)
        nsum[0][N]++;
      if (nekom[m[i]].first == N - 1 - nekom[m[i]].second)
        nsum[1][N]++;
    }
    int un = 0, vn = 0;
    rep(i, 2) rep(j, N + 1) if (usum[i][j] == N) un++;
    rep(i, 2) rep(j, N + 1) if (nsum[i][j] == N) vn++;
    if (N == 1)
      un = min(un, 1), vn = min(vn, 1);
    if (un >= U && vn >= V)
      break;
    else if (un >= U)
      goto USA;
    else if (vn >= V)
      goto NEKO;
  }
  puts("DRAW");
  return 0;
  ;
USA:
  puts("USAGI");
  return 0;
NEKO:
  puts("NEKO");
  return 0;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
const int INF = 1000000000;
#define REP(i, s, n) for (int i = (int)(s); i < (int)(n); ++i)
#define rep(i, n) REP(i, 0, n)
typedef long long int i64;
typedef pair<int, int> pint;

map<int, pint> usam, nekom;
int usum[2][501], nsum[2][501];
int m[5010];
bitset<1000001> memo(0);
int main() {
  int N, U, V, M;
  scanf("%d%d%d%d", &N, &U, &V, &M);
  int num;
  rep(i, N) rep(j, N) {
    scanf("%d", &num);
    usam[num] = pint(j, i);
    memo[num] = 1;
  }
  rep(i, N) rep(j, N) {
    scanf("%d", &num);
    nekom[num] = pint(j, i);
    memo[num] = 1;
  }
  int it = 0;
  rep(i, M) {
    scanf("%d", &num);
    if (memo[num])
      m[it++] = num;
  }
  rep(i, it) {
    if (usam.count(m[i])) {
      usum[0][usam[m[i]].first]++;
      usum[1][usam[m[i]].second]++;
      if (usam[m[i]].first == usam[m[i]].second)
        usum[0][N]++;
      if (usam[m[i]].first == N - 1 - usam[m[i]].second)
        usum[1][N]++;
    }
    if (nekom.count(m[i])) {
      nsum[0][nekom[m[i]].first]++;
      nsum[1][nekom[m[i]].second]++;
      if (nekom[m[i]].first == nekom[m[i]].second)
        nsum[0][N]++;
      if (nekom[m[i]].first == N - 1 - nekom[m[i]].second)
        nsum[1][N]++;
    }
    bool ok;
    int un = 0, vn = 0;
    rep(i, 2) rep(j, N + 1) if (usum[i][j] == N) un++;
    rep(i, 2) rep(j, N + 1) if (nsum[i][j] == N) vn++;
    if (N == 1)
      un = min(un, 1), vn = min(vn, 1);
    if (un >= U && vn >= V)
      break;
    else if (un >= U)
      goto USA;
    else if (vn >= V)
      goto NEKO;
  }
  puts("DRAW");
  return 0;
  ;
USA:
  puts("USAGI");
  return 0;
NEKO:
  puts("NEKO");
  return 0;
}