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

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, u, v, m;
  static int rab[512][512], cat[512][512];
  static int doner[512][512], donec[512][512];
  static pair<int, int> rabpos[1000001], catpos[1000001];
  scanf("%d %d %d %d", &n, &u, &v, &m);

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &rab[i][j]);
      rabpos[rab[i][j]] = make_pair(i, j);
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &cat[i][j]);
      catpos[cat[i][j]] = make_pair(i, j);
    }
  }

  for (int i = 1; i <= m; i++) {
    int x;
    scanf("%d", &x);
    pair<int, int> r = rabpos[x];
    doner[r.first][r.second] = i;
    r = catpos[x];
    donec[r.first][r.second] = i;
  }

  vector<int> rabmin, catmin;

  for (int i = 0; i < 13; i++) {
    rabmin.push_back(1000000000);
    catmin.push_back(1000000000);
  }

  for (int i = 1; i <= n; i++) {
    int rabmax = 0, catmax = 0;
    bool rabok = 1, catok = 1;
    for (int j = 1; j <= n; j++) {
      rabmax = max(rabmax, doner[i][j]);
      catmax = max(catmax, donec[i][j]);
      rabok &= (!!doner[i][j]);
      catok &= (!!donec[i][j]);
    }
    if (rabok)
      rabmin.push_back(rabmax);
    if (catok)
      catmin.push_back(catmax);

    if (n != 1) {
      rabmax = 0, catmax = 0;
      rabok = 1, catok = 1;
      for (int j = 1; j <= n; j++) {
        rabmax = max(rabmax, doner[j][i]);
        catmax = max(catmax, donec[j][i]);
        rabok &= (!!doner[j][i]);
        catok &= (!!donec[j][i]);
      }
      if (rabok)
        rabmin.push_back(rabmax);
      if (catok)
        catmin.push_back(catmax);
    }
  }

  if (n != 1) {
    int rabmax = 0, catmax = 0;
    bool rabok = 1, catok = 1;
    for (int i = 1; i <= n; i++) {
      rabmax = max(rabmax, doner[i][i]);
      catmax = max(catmax, donec[i][i]);
      rabok &= (!!doner[i][i]);
      catok &= (!!donec[i][i]);
    }
    if (rabok)
      rabmin.push_back(rabmax);
    if (catok)
      catmin.push_back(catmax);

    rabmax = 0, catmax = 0;
    rabok = 1, catok = 1;
    for (int i = 1; i <= n; i++) {
      rabmax = max(rabmax, doner[i][n - i + 1]);
      catmax = max(catmax, donec[i][n - i + 1]);
      rabok &= (!!doner[i][n - i + 1]);
      catok &= (!!donec[i][n - i + 1]);
    }
    if (rabok)
      rabmin.push_back(rabmax);
    if (catok)
      catmin.push_back(catmax);
  }

  sort(rabmin.begin(), rabmin.end());
  sort(catmin.begin(), catmin.end());

  --u;
  --v;
  if (rabmin[u] == catmin[v] || min(rabmin[u], catmin[v]) > m) {
    return (!printf("DRAW\\n"));
  }
  printf("%s\\n", rabmin[u] < catmin[v] ? "USAGI" : "NEKO");

  return (0);
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

int main() {
  int n, u, v, m;
  static int rab[512][512], cat[512][512];
  static int doner[512][512], donec[512][512];
  static pair<int, int> rabpos[512 * 512 + 1], catpos[512 * 512 + 1];
  scanf("%d %d %d %d", &n, &u, &v, &m);

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &rab[i][j]);
      rabpos[rab[i][j]] = make_pair(i, j);
    }
  }

  for (int i = 1; i <= n; i++) {
    for (int j = 1; j <= n; j++) {
      scanf("%d", &cat[i][j]);
      catpos[cat[i][j]] = make_pair(i, j);
    }
  }

  for (int i = 1; i <= m; i++) {
    int x;
    scanf("%d", &x);
    if (x <= 1000000) {
      pair<int, int> r = rabpos[x];
      doner[r.first][r.second] = i;
      r = catpos[x];
      donec[r.first][r.second] = i;
    }
  }

  vector<int> rabmin, catmin;

  for (int i = 0; i < 13; i++) {
    rabmin.push_back(1000000000);
    catmin.push_back(1000000000);
  }

  for (int i = 1; i <= n; i++) {
    int rabmax = 0, catmax = 0;
    bool rabok = 1, catok = 1;
    for (int j = 1; j <= n; j++) {
      rabmax = max(rabmax, doner[i][j]);
      catmax = max(catmax, donec[i][j]);
      rabok &= (!!doner[i][j]);
      catok &= (!!donec[i][j]);
    }
    if (rabok)
      rabmin.push_back(rabmax);
    if (catok)
      catmin.push_back(catmax);

    if (n != 1) {
      rabmax = 0, catmax = 0;
      rabok = 1, catok = 1;
      for (int j = 1; j <= n; j++) {
        rabmax = max(rabmax, doner[j][i]);
        catmax = max(catmax, donec[j][i]);
        rabok &= (!!doner[j][i]);
        catok &= (!!donec[j][i]);
      }
      if (rabok)
        rabmin.push_back(rabmax);
      if (catok)
        catmin.push_back(catmax);
    }
  }

  if (n != 1) {
    int rabmax = 0, catmax = 0;
    bool rabok = 1, catok = 1;
    for (int i = 1; i <= n; i++) {
      rabmax = max(rabmax, doner[i][i]);
      catmax = max(catmax, donec[i][i]);
      rabok &= (!!doner[i][i]);
      catok &= (!!donec[i][i]);
    }
    if (rabok)
      rabmin.push_back(rabmax);
    if (catok)
      catmin.push_back(catmax);

    rabmax = 0, catmax = 0;
    rabok = 1, catok = 1;
    for (int i = 1; i <= n; i++) {
      rabmax = max(rabmax, doner[i][n - i + 1]);
      catmax = max(catmax, donec[i][n - i + 1]);
      rabok &= (!!doner[i][n - i + 1]);
      catok &= (!!donec[i][n - i + 1]);
    }
    if (rabok)
      rabmin.push_back(rabmax);
    if (catok)
      catmin.push_back(catmax);
  }

  sort(rabmin.begin(), rabmin.end());
  sort(catmin.begin(), catmin.end());

  --u;
  --v;
  if (rabmin[u] == catmin[v] || min(rabmin[u], catmin[v]) > m) {
    return (!printf("DRAW\\n"));
  }
  printf("%s\\n", rabmin[u] < catmin[v] ? "USAGI" : "NEKO");

  return (0);
}