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

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

const int gmax_n = 505;

int cntX[2][gmax_n] = {0};
int cntY[2][gmax_n] = {0};
bool usa[gmax_n][gmax_n] = {{0}}, neko[gmax_n][gmax_n] = {{0}};

int checkPointU(int n, int u, pair<int, int> p) {
  int y = p.first;
  int x = p.second;
  usa[y][x] = 1;

  int cnt = 0;
  cntX[0][x]++;
  cntY[0][y]++;

  rep(i, n) {
    if (cntX[0][i] == n)
      cnt++;
    if (cntY[0][i] == n)
      cnt++;
  }

  bool f = true;
  rep(i, n) if (usa[i][i] == 0) f = false;
  if (f)
    cnt++;

  f = true;
  rep(i, n) if (usa[i][n - 1 - i] == 0) f = false;
  if (f)
    cnt++;
  // y = find(y,parY[0][x], cntY[0][x]);
  // x = find(x,parX[0][y], cntX[0][y]);
  // show(cntX[0][y][x])
  // show(cntY[0][x][y])
  // show(cnt)
  if (n == 1)
    cnt = 1;
  if (cnt >= u || cnt >= u)
    return 1;
  else
    return 0;
}

int checkPointN(int n, int v, pair<int, int> p) {
  int y = p.first;
  int x = p.second;
  neko[y][x] = 1;

  int cnt = 0;
  cntX[1][x]++;
  cntY[1][y]++;

  rep(i, n) {
    if (cntX[1][i] == n)
      cnt++;
    if (cntY[1][i] == n)
      cnt++;
  }

  bool f = true;
  rep(i, n) if (neko[i][i] == 0) f = false;
  if (f)
    cnt++;

  f = true;
  rep(i, n) if (neko[i][n - 1 - i] == 0) f = false;
  if (f)
    cnt++;
  // y = find(y,parY[0][x], cntY[0][x]);
  // x = find(x,parX[0][y], cntX[0][y]);
  // show(cntX[0][y][x])
  // show(cntY[0][x][y])

  if (n == 1)
    cnt = 1;
  if (cnt >= v || cnt >= v)
    return 1;
  else
    return 0;
}

int main() {
  int n, u, v, m;
  cin >> n >> u >> v >> m;

  int x;
  map<int, pair<int, int>> up, np;
  rep(i, n) {
    rep(j, n) {
      cin >> x;
      up[x] = make_pair(i, j);
    }
  }
  rep(i, n) {
    rep(j, n) {
      cin >> x;
      np[x] = make_pair(i, j);
    }
  }

  int ans = 0;
  rep(i, m) {
    int a;
    cin >> a;
    // cout << a << endl;
    if (ans == 0) {
      int utmp = -1, vtmp = -1;
      utmp = (up.count(a) ? checkPointU(n, u, up[a]) : 0);
      vtmp = (np.count(a) ? checkPointN(n, v, np[a]) : 0);
      //            show(utmp)
      // show(vtmp)
      if (utmp == 0 && vtmp != 0)
        ans = 1;
      else if (utmp != 0 && vtmp == 0)
        ans = 2;
      else if (utmp != 0 && vtmp != 0)
        ans = 3;
      else if (utmp == 0 && vtmp == 0)
        ;
      else
        assert(0);
    }
    //       show(ans)
    //           rep(i,n){ rep(j,n){ cout << usa[i][j]; } cout << endl; }
    //       cout << endl;
    //           rep(i,n){ rep(j,n){ cout << neko[i][j]; } cout << endl; }
  }
  if (ans == 1)
    cout << "NEKO" << endl;
  else if (ans == 2)
    cout << "USAGI" << endl;
  else
    cout << "DRAW" << endl;
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define range(i, a, b) for (int i = (a); i < (b); i++)
#define rep(i, b) for (int i = 0; i < (b); i++)
#define all(a) (a).begin(), (a).end()
#define show(x) cerr << #x << " = " << (x) << endl;
using namespace std;

const int gmax_n = 505;

int parX[2][gmax_n][gmax_n];   //???
int parY[2][gmax_n][gmax_n];   //???
int depthX[2][gmax_n][gmax_n]; //??¨?????±???
int depthY[2][gmax_n][gmax_n]; //??¨?????±???
int cntX[2][gmax_n][gmax_n];
int cntY[2][gmax_n][gmax_n];
bool usa[gmax_n][gmax_n] = {{0}}, neko[gmax_n][gmax_n] = {{0}};

void init(int n) {
  rep(i, 2) {
    rep(j, n) {
      rep(k, n) {
        parX[i][j][k] = parY[i][j][k] = k;
        depthX[i][j][k] = depthY[i][j][k] = 0;
        cntX[i][j][k] = cntY[i][j][k] = 1;
      }
    }
  }
}

int find(int x, int par[gmax_n], int cnt[gmax_n]) {
  if (par[x] == x) {
    return x;
  } else {
    int tmp = find(par[x], par, cnt);
    cnt[x] = max(cnt[x], cnt[tmp]);
    return par[x] = tmp;
  }
}

void unite(int x, int y, int par[gmax_n], int depth[gmax_n], int cnt[gmax_n]) {
  x = find(x, par, cnt);
  y = find(y, par, cnt);
  cnt[x] += cnt[y];
  cnt[y] = cnt[x];
  if (x == y)
    return;

  if (depth[x] < depth[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (depth[x] == depth[y])
      depth[x]++;
  }
}

bool same(int x, int y, int par[gmax_n], int cnt[gmax_n]) {
  return find(x, par, cnt) == find(y, par, cnt);
}

int checkPointU(int n, int u, pair<int, int> p) {
  int y = p.first;
  int x = p.second;
  usa[y][x] = 1;

  if (y - 1 > 0) {
    if (usa[y - 1][x] == 1)
      unite(y - 1, y, parY[0][x], depthY[0][x], cntY[0][x]);
  }
  if (x - 1 > 0) {
    if (usa[y][x - 1] == 1)
      unite(x - 1, x, parX[0][y], depthX[0][y], cntX[0][y]);
  }
  if (y + 1 < n) {
    if (usa[y + 1][x] == 1)
      unite(y + 1, y, parY[0][x], depthY[0][x], cntY[0][x]);
  }
  if (x + 1 < n) {
    if (usa[y][x + 1] == 1)
      unite(x + 1, x, parX[0][y], depthX[0][y], cntX[0][y]);
  }

  int cnt = 0;
  rep(i, n) {
    y = find(0, parY[0][i], cntY[0][i]);
    if (cntY[0][i][y] == n)
      cnt++;
  }
  rep(i, n) {
    x = find(0, parX[0][i], cntX[0][i]);
    if (cntX[0][i][x] == n)
      cnt++;
  }

  bool f = true;
  rep(i, n) if (usa[i][i] == 0) f = false;
  if (f)
    cnt++;

  f = true;
  rep(i, n) if (usa[i][n - 1 - i] == 0) f = false;
  if (f)
    cnt++;
  // y = find(y,parY[0][x], cntY[0][x]);
  // x = find(x,parX[0][y], cntX[0][y]);
  // show(cntX[0][y][x])
  // show(cntY[0][x][y])
  // show(cnt)
  if (cnt >= u || cnt >= u)
    return 1;
  else
    return 0;
}

int checkPointN(int n, int v, pair<int, int> p) {
  int y = p.first;
  int x = p.second;
  neko[y][x] = 1;

  if (y - 1 > 0)
    if (neko[y - 1][x] == 1)
      unite(y - 1, y, parY[1][x], depthY[1][x], cntY[1][x]);
  if (x - 1 > 0)
    if (neko[y][x - 1] == 1)
      unite(x - 1, x, parX[1][y], depthX[1][y], cntX[1][y]);
  if (y + 1 < n)
    if (neko[y + 1][x] == 1)
      unite(y + 1, y, parY[1][x], depthY[1][x], cntY[1][x]);
  if (x + 1 < n)
    if (neko[y][x + 1] == 1)
      unite(x + 1, x, parX[1][y], depthX[1][y], cntX[1][y]);

  int cnt = 0;
  rep(i, n) {
    y = find(0, parY[1][i], cntY[1][i]);
    if (cntY[1][i][y] == n)
      cnt++;
  }
  rep(i, n) {
    x = find(0, parX[1][i], cntX[1][i]);
    if (cntX[1][i][x] == n)
      cnt++;
  }

  bool f = true;
  rep(i, n) if (neko[i][i] == 0) f = false;
  if (f)
    cnt++;

  f = true;
  rep(i, n) if (neko[i][n - 1 - i] == 0) f = false;
  if (f)
    cnt++;
  // y = find(y,parY[0][x], cntY[0][x]);
  // x = find(x,parX[0][y], cntX[0][y]);
  // show(cntX[0][y][x])
  // show(cntY[0][x][y])
  // show(cnt)
  if (cnt >= v || cnt >= v)
    return 1;
  else
    return 0;
}

int main() {
  int n, u, v, m;
  cin >> n >> u >> v >> m;

  init(gmax_n - 4);

  int x;
  map<int, pair<int, int>> up, np;
  rep(i, n) {
    rep(j, n) {
      cin >> x;
      up[x] = make_pair(i, j);
    }
  }
  rep(i, n) {
    rep(j, n) {
      cin >> x;
      np[x] = make_pair(i, j);
    }
  }

  int ans = 0;
  rep(i, m) {
    int a;
    cin >> a;
    // cout << a << endl;
    if (ans == 0) {
      int utmp = -1, vtmp = -1;
      utmp = (up.count(a) ? checkPointU(n, u, up[a]) : 0);
      vtmp = (np.count(a) ? checkPointN(n, v, np[a]) : 0);
      //            show(utmp)
      // show(vtmp)
      if (utmp == 0 && vtmp != 0)
        ans = 1;
      else if (utmp != 0 && vtmp == 0)
        ans = 2;
      else if (utmp != 0 && vtmp != 0)
        ans = 3;
      else if (utmp == 0 && vtmp == 0)
        ;
      else
        assert(0);
    }
    //       show(ans)
    //           rep(i,n){ rep(j,n){ cout << usa[i][j]; } cout << endl; }
    //       cout << endl;
    //           rep(i,n){ rep(j,n){ cout << neko[i][j]; } cout << endl; }
  }
  if (ans == 1)
    cout << "NEKO" << endl;
  else if (ans == 2)
    cout << "USAGI" << endl;
  else if (ans == 3)
    cout << "DRAW" << endl;
  else
    assert(0);
}