Problem p01358 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi M_PI

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

const int N = 100010;

int main() {
  int n, u, v, m;
  cin >> n >> u >> v >> m;
  vector<P> pu(N, make_pair(-1, -1)), pn(N, make_pair(-1, -1));
  REP(i, n) REP(j, n) {
    int a;
    cin >> a;
    pu[a] = make_pair(i, j);
  }
  REP(i, n) REP(j, n) {
    int a;
    cin >> a;
    pn[a] = make_pair(i, j);
  }
  VI xu(n), yu(n), ou(2), xn(n), yn(n), on(2);
  while (m-- > 0 && u > 0 && v > 0) {
    int a, i, j;
    cin >> a;
    i = pu[a].first;
    j = pu[a].second;
    if (i != -1) {
      if (++xu[i] == n)
        u--;
      if (++yu[j] == n)
        u--;
      if (i == j)
        if (++ou[0] == n)
          u--;
      if (i + j == n - 1)
        if (++ou[1] == n)
          u--;
    }

    i = pn[a].first;
    j = pn[a].second;
    if (i != -1) {
      if (++xn[i] == n)
        v--;
      if (++yn[j] == n)
        v--;
      if (i == j)
        if (++on[0] == n)
          v--;
      if (i + j == n - 1)
        if (++on[1] == n)
          v--;
    }
  }
  if (n == 1) {
    u += 3;
    v += 3;
  }
  string ans;
  if (u <= 0 && v > 0)
    ans = "USAGI";
  else if (v <= 0 && u > 0)
    ans = "NEKO";
  else
    ans = "DRAW";
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <fstream>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>
using namespace std;

#define REP(i, n) for (int i = 0; i < n; ++i)
#define FOR(i, a, b) for (int i = a; i <= b; ++i)
#define FORR(i, a, b) for (int i = a; i >= b; --i)
#define pi M_PI

typedef long long ll;
typedef vector<int> VI;
typedef vector<ll> VL;
typedef vector<VI> VVI;
typedef pair<int, int> P;
typedef pair<ll, ll> PL;

const int N = 1000010;

int main() {
  int n, u, v, m;
  cin >> n >> u >> v >> m;
  vector<P> pu(N, make_pair(-1, -1)), pn(N, make_pair(-1, -1));
  REP(i, n) REP(j, n) {
    int a;
    cin >> a;
    pu[a] = make_pair(i, j);
  }
  REP(i, n) REP(j, n) {
    int a;
    cin >> a;
    pn[a] = make_pair(i, j);
  }
  VI xu(n), yu(n), ou(2), xn(n), yn(n), on(2);
  while (m-- > 0 && u > 0 && v > 0) {
    int a, i, j;
    cin >> a;
    i = pu[a].first;
    j = pu[a].second;
    if (i != -1) {
      if (++xu[i] == n)
        u--;
      if (++yu[j] == n)
        u--;
      if (i == j)
        if (++ou[0] == n)
          u--;
      if (i + j == n - 1)
        if (++ou[1] == n)
          u--;
    }

    i = pn[a].first;
    j = pn[a].second;
    if (i != -1) {
      if (++xn[i] == n)
        v--;
      if (++yn[j] == n)
        v--;
      if (i == j)
        if (++on[0] == n)
          v--;
      if (i + j == n - 1)
        if (++on[1] == n)
          v--;
    }
  }
  if (n == 1) {
    u += 3;
    v += 3;
  }
  string ans;
  if (u <= 0 && v > 0)
    ans = "USAGI";
  else if (v <= 0 && u > 0)
    ans = "NEKO";
  else
    ans = "DRAW";
  cout << ans << endl;
  return 0;
}