Problem p01358 (C++) — buggy submission, verdict: Memory Limit Exceeded

// template {{{

#include <bits/stdc++.h>
using namespace std;

#define loop(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) loop(i, 0, n)
#define rloop(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define rrep(i, n) rloop(i, 0, n)
#define pb push_back
#define pf push_front
#define eb emplace_back
#define ef emplace_front
#define mp std::make_pair
#define mt std::make_tuple
#define fi first
#define se second

using ll = long long;
using ull = unsigned long long;

template <typename T, size_t H, size_t W>
using matrix = std::array<std::array<T, W>, H>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
const ll LLINF = 1e18 + 10;

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

template <typename T> inline T sq(T x) { return x * x; }

template <typename T, typename U> inline void chmax(T &x, U y) {
  x = std::max<T>(x, y);
}

template <typename T, typename U> inline void chmin(T &x, U y) {
  x = std::min<T>(x, y);
}

template <typename T> inline void sort(T &c) {
  std::sort(std::begin(c), std::end(c));
}

template <typename T> inline void reverse(T &c) {
  std::reverse(std::begin(c), std::end(c));
}

template <typename T> inline void unique(T &c) {
  sort(c);
  c.erase(std::unique(std::begin(c), std::end(c)), std::end(c));
}

// }}}

int n, u[2], m;
int a[2][500][500], b[100000];
int cx[2][500], cy[2][500], cz[2][2];
vector<int> vx[2][100000], vy[2][100000], vz[2][100000];

int main() {
  cin >> n >> u[0] >> u[1] >> m;

  vector<int> z;
  rep(i, 2) rep(j, n) rep(k, n) { cin >> a[i][j][k]; }

  rep(i, m) {
    cin >> b[i];
    z.pb(b[i]);
  }
  z.pb(INF);
  unique(z);

  rep(i, 2) rep(j, n) rep(k, n) {
    int t = lower_bound(begin(z), end(z), a[i][j][k]) - begin(z);
    if (a[i][j][k] != z[t])
      continue;
    vx[i][t].pb(j);
    if (n > 1) {
      vy[i][t].pb(k);
      if (j == k)
        vz[i][t].pb(0);
      if (j + k == n - 1)
        vz[i][t].pb(1);
    }
  }

  string res = "DRAW";
  rep(i, m) {
    int t = lower_bound(begin(z), end(z), b[i]) - begin(z);
    rep(j, 2) {
      for (int k : vx[j][t]) {
        if (++cx[j][k] == n)
          u[j]--;
      }
      for (int k : vy[j][t]) {
        if (++cy[j][k] == n)
          u[j]--;
      }
      for (int k : vz[j][t]) {
        if (++cz[j][k] == n)
          u[j]--;
      }
    }

    if (u[0] <= 0 && u[1] <= 0)
      break;
    if (u[0] <= 0) {
      res = "USAGI";
      break;
    }
    if (u[1] <= 0) {
      res = "NEKO";
      break;
    }
  }

  cout << res << endl;
}

Error: MLE

Fixed submission:

// template {{{

#include <bits/stdc++.h>
using namespace std;

#define loop(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define rep(i, n) loop(i, 0, n)
#define rloop(i, a, b) for (int i = (int)(b)-1; i >= (int)(a); i--)
#define rrep(i, n) rloop(i, 0, n)
#define pb push_back
#define pf push_front
#define eb emplace_back
#define ef emplace_front
#define mp std::make_pair
#define mt std::make_tuple
#define fi first
#define se second

using ll = long long;
using ull = unsigned long long;

template <typename T, size_t H, size_t W>
using matrix = std::array<std::array<T, W>, H>;

const int MOD = 1e9 + 7;
const int INF = 1e9 + 10;
const ll LLINF = 1e18 + 10;

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

template <typename T> inline T sq(T x) { return x * x; }

template <typename T, typename U> inline void chmax(T &x, U y) {
  x = std::max<T>(x, y);
}

template <typename T, typename U> inline void chmin(T &x, U y) {
  x = std::min<T>(x, y);
}

template <typename T> inline void sort(T &c) {
  std::sort(std::begin(c), std::end(c));
}

template <typename T> inline void reverse(T &c) {
  std::reverse(std::begin(c), std::end(c));
}

template <typename T> inline void unique(T &c) {
  sort(c);
  c.erase(std::unique(std::begin(c), std::end(c)));
}

// }}}

int n, u[2], m;
int a[2][500][500], b[100000];
int cx[2][500], cy[2][500], cz[2][2];
vector<short> vx[2][350000], vy[2][350000], vz[2][350000];

int main() {
  cin >> n >> u[0] >> u[1] >> m;

  vector<int> z;
  rep(i, 2) rep(j, n) rep(k, n) {
    cin >> a[i][j][k];
    z.pb(a[i][j][k]);
  }

  rep(i, m) {
    cin >> b[i];
    z.pb(b[i]);
  }

  unique(z);

  rep(i, 2) rep(j, n) rep(k, n) {
    int t = lower_bound(begin(z), end(z), a[i][j][k]) - begin(z);
    vx[i][t].pb(j);
    if (n > 1) {
      vy[i][t].pb(k);
      if (j == k)
        vz[i][t].pb(0);
      if (j + k == n - 1)
        vz[i][t].pb(1);
    }
  }

  string res = "DRAW";
  rep(i, m) {
    int t = lower_bound(begin(z), end(z), b[i]) - begin(z);
    rep(j, 2) {
      for (int k : vx[j][t]) {
        if (++cx[j][k] == n)
          u[j]--;
      }
      for (int k : vy[j][t]) {
        if (++cy[j][k] == n)
          u[j]--;
      }
      for (int k : vz[j][t]) {
        if (++cz[j][k] == n)
          u[j]--;
      }
    }

    if (u[0] <= 0 && u[1] <= 0)
      break;
    if (u[0] <= 0) {
      res = "USAGI";
      break;
    }
    if (u[1] <= 0) {
      res = "NEKO";
      break;
    }
  }

  cout << res << endl;
}