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

#include <algorithm>
#include <array>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define REV(v) rbegin(v), rend(v)
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second

using namespace std;

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

const int N = 510, M = 1e5 + 10;
P pos[2][M * 10];
int x[M];

int n, u, v, m;

int board[N][N];
bool check(int turn, P *pos, int uv) {
  MEMSET(board, 0);

  for (int i = 0; i < turn; ++i) {
    board[pos[x[i]].Y][pos[x[i]].X] = 1;
  }
  int cnt[2] = {};
  FOR(i, 1, n + 1) {
    if (board[i][i])
      ++cnt[0];
    if (board[i][n + 1 - i])
      ++cnt[1];
  }
  rep(i, n + 1) rep(j, n + 1) {
    board[i + 1][j] += board[i][j];
    board[i][j + 1] += board[i][j];
    board[i + 1][j + 1] -= board[i][j];
  }

  int res = (cnt[0] == n) + (cnt[1] == n);
  FOR(i, 1, n + 1) {
    if (board[i][n + 1] == n)
      ++res;
    if (board[n + 1][i] == n)
      ++res;
  }

  if (n == 1)
    res = !!res;

  return res >= uv;
}

int main() {
  cin >> n >> u >> v >> m;
  rep(i, 2) rep(j, n) rep(k, n) {
    int xx;
    cin >> xx;
    pos[i][xx] = MP(k + 1, j + 1);
  }
  rep(i, m) cin >> x[i];

  bool end = false;
  int lb = 0, ub = m + 1;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    bool usa = check(mid, pos[0], u);
    bool neko = check(mid, pos[1], v);
    if (usa != neko) {
      if (usa)
        cout << "USAGI" << endl;
      else
        cout << "NEKO" << endl;
      end = true;
      break;
    }
    if (usa)
      ub = mid;
    else
      lb = mid;
  }
  if (!end)
    cout << "DRAW" << endl;

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <array>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <vector>

// BEGIN CUT HERE
#ifdef _MSC_VER
#include <agents.h>
#endif
// END CUT HERE

#define FOR(i, a, b) for (int i = (a); i < (int)(b); ++i)
#define rep(i, n) FOR(i, 0, n)
#define ALL(v) begin(v), end(v)
#define REV(v) rbegin(v), rend(v)
#define MEMSET(v, s) memset(v, s, sizeof(v))
#define MP make_pair
#define MT make_tuple
#define X first
#define Y second

using namespace std;

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

const int N = 510, M = 1e5 + 10;
P pos[2][M];
int x[M];

int n, u, v, m;

int board[N][N];
bool check(int turn, P *pos, int uv) {
  MEMSET(board, 0);

  for (int i = 0; i < turn; ++i) {
    board[pos[x[i]].Y][pos[x[i]].X] = 1;
  }
  int cnt[2] = {};
  FOR(i, 1, n + 1) {
    if (board[i][i])
      ++cnt[0];
    if (board[i][n + 1 - i])
      ++cnt[1];
  }
  rep(i, n + 1) rep(j, n + 1) {
    board[i + 1][j] += board[i][j];
    board[i][j + 1] += board[i][j];
    board[i + 1][j + 1] -= board[i][j];
  }

  int res = (cnt[0] == n) + (cnt[1] == n);
  FOR(i, 1, n + 1) {
    if (board[i][n + 1] == n)
      ++res;
    if (board[n + 1][i] == n)
      ++res;
  }

  if (n == 1)
    res = !!res;

  return res >= uv;
}

int main() {
  cin >> n >> u >> v >> m;
  rep(i, 2) rep(j, n) rep(k, n) {
    int xx;
    cin >> xx;
    pos[i][xx] = MP(k + 1, j + 1);
  }
  rep(i, m) cin >> x[i];

  bool end = false;
  int lb = 0, ub = m + 1;
  while (ub - lb > 1) {
    int mid = (ub + lb) / 2;
    bool usa = check(mid, pos[0], u);
    bool neko = check(mid, pos[1], v);
    if (usa != neko) {
      if (usa)
        cout << "USAGI" << endl;
      else
        cout << "NEKO" << endl;
      end = true;
      break;
    }
    if (usa)
      ub = mid;
    else
      lb = mid;
  }
  if (!end)
    cout << "DRAW" << endl;

  return 0;
}