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

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
using namespace std;

const int MAXN = 502;
const int MAXL = 1000005;

int N, U[2], M;
int X[2][MAXL], Y[2][MAXL];
int tate[2][MAXN], yoko[2][MAXN], migishita[2], migiue[2];

int main() {
  while (cin >> N >> U[0] >> U[1] >> M) {
    if (N == 1) {
      U[0] *= 4;
      U[1] *= 4;
    }
    fill(X[0], X[2], -1);
    fill(Y[0], Y[2], -1);
    for (int k = 0; k < 2; ++k) {
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          int a;
          cin >> a;
          X[k][a] = j;
          Y[k][a] = i;
        }
      }
    }

    fill(tate[0], tate[2], 0);
    fill(yoko[0], yoko[2], 0);
    fill(migishita, migishita + 2, 0);
    fill(migiue, migiue + 2, 0);
    string res = "";
    while (M--) {
      int a;
      cin >> a;
      if (res != "")
        continue;
      for (int k = 0; k < 2; ++k) {
        if (X[k][a] == -1)
          continue;
        U[k] -= ++tate[k][X[k][a]] == N;
        U[k] -= ++yoko[k][Y[k][a]] == N;
        if (X[k][a] == Y[k][a])
          U[k] -= ++migishita[k] == N;
        if (X[k][a] == N - 1 - Y[k][a])
          U[k] -= ++migiue[k] == N;
      }
      if (U[0] <= 0 || U[1] <= 0) {
        if (U[0] <= 0 && U[1] <= 0)
          res = "DRAW";
        else if (U[0] <= 0)
          res = "USAGI";
        else
          res = "NEKO";
      }
    }
    if (res == "")
      res = "DRAW";
    cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <iostream>
#include <map>
using namespace std;

const int MAXN = 502;
const int MAXL = 1000005;

int N, U[2], M;
int G[2][MAXN][MAXN];
map<int, int> tate[2][MAXN], yoko[2][MAXN], migishita[2], hidariue[2];
int tate2[2][MAXN], yoko2[2][MAXN], migishita2[2], hidariue2[2];

int main() {
  while (cin >> N >> U[0] >> U[1] >> M) {
    for (int k = 0; k < 2; ++k) {
      for (int i = 0; i < MAXN; ++i) {
        tate[k][i].clear();
        tate2[k][i] = 0;
        yoko[k][i].clear();
        yoko2[k][i] = 0;
      }
      migishita[k].clear();
      migishita2[k] = 0;
      hidariue[k].clear();
      hidariue2[k] = 0;
    }

    for (int k = 0; k < 2; ++k) {
      for (int i = 0; i < N; ++i) {
        for (int j = 0; j < N; ++j) {
          cin >> G[k][i][j];
        }
      }
    }

    for (int k = 0; k < 2; ++k) {
      if (N != 1) {
        for (int i = 0; i < N; ++i) {
          for (int j = 0; j < N; ++j) {
            assert(++yoko[k][i][G[k][i][j]] <= 1);
          }
        }
        for (int j = 0; j < N; ++j) {
          for (int i = 0; i < N; ++i) {
            assert(++tate[k][j][G[k][i][j]] <= 1);
          }
        }
        for (int i = 0; i < N; ++i) {
          assert(++migishita[k][G[k][i][i]] <= 1);
        }
      }
      for (int i = 0; i < N; ++i) {
        assert(++hidariue[k][G[k][N - 1 - i][i]] <= 1);
      }
    }

    string res = "";
    while (M--) {
      int a;
      cin >> a;
      if (res != "")
        continue;
      for (int k = 0; k < 2; ++k) {
        for (int i = 0; i < N; ++i) {
          if (yoko[k][i].count(a)) {
            yoko2[k][i] += yoko[k][i][a];
            U[k] -= yoko2[k][i] == N;
          }
        }
        for (int j = 0; j < N; ++j) {
          if (tate[k][j].count(a)) {
            tate2[k][j] += tate[k][j][a];
            U[k] -= tate2[k][j] == N;
          }
        }
        if (migishita[k].count(a)) {
          migishita2[k] += migishita[k][a];
          U[k] -= migishita2[k] == N;
        }
        if (hidariue[k].count(a)) {
          hidariue2[k] += hidariue[k][a];
          U[k] -= hidariue2[k] == N;
        }
      }
      if (U[0] <= 0 || U[1] <= 0) {
        if (U[0] <= 0 && U[1] <= 0)
          res = "DRAW";
        else if (U[0] <= 0)
          res = "USAGI";
        else
          res = "NEKO";
      }
    }
    if (res == "")
      res = "DRAW";
    cout << res << endl;
  }
  return 0;
}