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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;

struct Edge {
  int to, from, cost;
};

template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define mp make_pair
#define PUTLINE cout << "LINE:" << __LINE__ << endl;

const int INF = 2147483647;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

int us[510][510];
int ne[510][510];
int usa[510][510];
int nek[510][510];
pint usag[1000100];
pint neko[1000100];
int n, u, v, m;

bool inside(pint p) {
  return 0 <= p.first && p.first < n && 0 <= p.second && p.second < n;
}

int main() {
  cin >> n >> u >> v >> m;
  rep(i, 1000100) {
    usag[i].first = usag[i].second = -1;
    neko[i].first = neko[i].second = -1;
  }
  rep(i, n) rep(j, n) {
    cin >> usa[i][j];
    usag[usa[i][j]].first = j;
    usag[usa[i][j]].second = i;
  }
  rep(i, n) rep(j, n) {
    cin >> nek[i][j];
    neko[nek[i][j]].first = j;
    neko[nek[i][j]].second = i;
  }
  int uc = 0, nc = 0;
  rep(i, m) {
    int a = in(), c;
    pint p;
    if (usag[a].first >= 0) {
      us[usag[a].second][usag[a].first] = 1;
      if (n > 1) {
        rep(j, 4) {
          c = 0;
          for (p = usag[a]; inside(p);
               p.first += dx[2 * j], p.second += dy[2 * j]) {
            if (us[p.second][p.first])
              ++c;
            else
              break;
          }
          for (p = usag[a]; inside(p);
               p.first += dx[2 * j + 1], p.second += dy[2 * j + 1]) {
            if (us[p.second][p.first])
              ++c;
            else
              break;
          }
          if (c > n)
            ++uc;
        }
      } else {
        ++uc;
      }
    }
    if (neko[a].first >= 0) {
      ne[neko[a].second][neko[a].first] = 1;
      if (n > 1) {
        rep(j, 4) {
          c = 0;
          for (p = neko[a]; inside(p);
               p.first += dx[2 * j], p.second += dy[2 * j]) {
            if (ne[p.second][p.first])
              ++c;
            else
              break;
          }
          for (p = neko[a]; inside(p);
               p.first += dx[2 * j + 1], p.second += dy[2 * j + 1]) {
            if (ne[p.second][p.first])
              ++c;
            else
              break;
          }
          if (c > n)
            ++nc;
        }
      } else {
        ++nc;
      }
    }
    if (uc >= u && nc < v) {
      cout << "USAGI" << endl;
      return 0;
    }
    if (uc < u && nc >= v) {
      cout << "NEKO" << endl;
      return 0;
    }
    if (uc >= u && nc >= v)
      break;
  }
  cout << "DRAW" << endl;
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

typedef long long Int;
typedef vector<int> vint;
typedef pair<int, int> pint;
typedef vector<string> vstring;

struct Edge {
  int to, from, cost;
};

template <class T> void chmin(T &t, T f) {
  if (t > f)
    t = f;
}
template <class T> void chmax(T &t, T f) {
  if (t < f)
    t = f;
}
int in() {
  int x;
  scanf("%d", &x);
  return x;
}

#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rep0(i, n) for (i = 0; i < (n); ++i)
#define repn(i, m, n) for (int i = (m); i <= (n); ++i)
#define all(n) n.begin(), n.end()
#define sz(n) ((int)(n).size())
#define mp make_pair
#define PUTLINE cout << "LINE:" << __LINE__ << endl;

const int INF = 2147483647;
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int dx[] = {1, -1, 0, 0, 1, -1, 1, -1, 0};
const int dy[] = {0, 0, 1, -1, 1, -1, -1, 1, 0};

int us[510][510];
int ne[510][510];
int usa[510][510];
int nek[510][510];
pint usag[1000100];
pint neko[1000100];
int n, u, v, m;

bool inside(pint p) {
  return 0 <= p.first && p.first < n && 0 <= p.first && p.first < n;
}

int main() {
  cin >> n >> u >> v >> m;
  rep(i, 1000100) {
    usag[i].first = usag[i].second = -1;
    neko[i].first = neko[i].second = -1;
  }
  rep(i, n) rep(j, n) {
    cin >> usa[i][j];
    usag[usa[i][j]].first = j;
    usag[usa[i][j]].second = i;
  }
  rep(i, n) rep(j, n) {
    cin >> nek[i][j];
    neko[nek[i][j]].first = j;
    neko[nek[i][j]].second = i;
  }
  // rep(i,12)cout<<i<<" "<<neko[i].first<<endl;
  int uc = 0, nc = 0;
  rep(i, m) {
    int a = in(), c;
    pint p;
    if (usag[a].first >= 0) {
      us[usag[a].second][usag[a].first] = 1;
      rep(j, 4) {
        c = 0;
        for (p = usag[a]; inside(p);
             p.first += dx[2 * j], p.second += dy[2 * j]) {
          if (us[p.second][p.first])
            ++c;
          else
            break;
        }
        for (p = usag[a]; inside(p);
             p.first += dx[2 * j + 1], p.second += dy[2 * j + 1]) {
          if (us[p.second][p.first])
            ++c;
          else
            break;
        }
        if (c > n)
          ++uc;
      }
    }
    /*
    cout<<"usagi "<<uc<<endl;
    rep(y,n){
    rep(x,n){
    cout<<us[y][x]<<" ";
    }
    cout<<endl;
    }
    cout<<endl;
    */
    if (neko[a].first >= 0) {
      // PUTLINE
      ne[neko[a].second][neko[a].first] = 1;
      rep(j, 4) {
        c = 0;
        for (p = neko[a]; inside(p);
             p.first += dx[2 * j], p.second += dy[2 * j]) {
          if (ne[p.second][p.first])
            ++c;
          else
            break;
        }
        for (p = neko[a]; inside(p);
             p.first += dx[2 * j + 1], p.second += dy[2 * j + 1]) {
          if (ne[p.second][p.first])
            ++c;
          else
            break;
        }
        if (c > n)
          ++nc;
      }
    }
    /*
    cout<<"neko "<<nc<<endl;
    rep(y,n){
    rep(x,n){
    cout<<ne[y][x]<<" ";
    }
    cout<<endl;
    }
    cout<<endl;
    */
    if (uc >= u && nc < v) {
      cout << "USAGI" << endl;
      return 0;
    }
    if (uc < u && nc >= v) {
      cout << "NEKO" << endl;
      return 0;
    }
    if (uc >= u && nc >= v)
      break;
  }
  cout << "DRAW" << endl;
  return 0;
}