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

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

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define Y first
#define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  VI first_card(2);
  cin >> N >> first_card[0] >> first_card[1] >> M;

  vector<map<int, PII>> num2pos(2);

  for (int i = 0; i < 2; i++) {
    for (int y = 0; y < N; y++) {
      for (int x = 0; x < N; x++) {
        int a;
        cin >> a;

        num2pos[i][a] = MP(y, x);
      }
    }
  }

  VI ms(M);
  for (auto &m : ms)
    cin >> m;

  // count
  vector<VI> cnts(2, VI(N * 2 + 2, 0));
  VI oks(2);
  for (int i = 0; i < M; i++) {
    int m = ms[i];

    for (int j = 0; j < 2; j++) {
      if (!num2pos[j].count(m))
        continue;

      // find
      PII pos = num2pos[j][m];

      oks[j] += ++cnts[j][pos.Y] == N;
      oks[j] += ++cnts[j][N + pos.X] == N;

      if (pos.X == pos.Y) {
        oks[j] += ++cnts[j][N * 2] == N;
      }
      if (pos.X + pos.Y == N - 1) {
        oks[j] += ++cnts[j][N * 2 + 1] == N;
      }

      if (N == 1) {
        oks[j] = 1;
      }
    }

    // ????????????
    VB ends(2);
    for (int j = 0; j < 2; j++) {
      ends[j] = oks[j] >= first_card[j];
    }

    if (ends[0] ^ ends[1]) {
      cout << (ends[0] ? "USAGI" : "NEKO") << endl;
      return 0;
    }
    if (ends[0] && ends[1]) {
      cout << "DRAW" << endl;
      return 0;
    }
  }

  cout << "DRAW" << endl;

  return 0;
}

Error: TLE

Fixed submission:

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

typedef ostringstream OSS;
typedef istringstream ISS;

typedef long long LL;
typedef pair<int, int> PII;

typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<LL> VLL;
typedef vector<VLL> VVLL;
typedef vector<string> VS;
typedef vector<VS> VVS;
typedef vector<bool> VB;
typedef vector<VB> VVB;
typedef vector<PII> VPII;

#define Y first
#define X second
#define MP make_pair
#define PB push_back
#define EB emplace_back
#define ALL(x) (x).begin(), (x).end()
#define RANGE(x, y, maxX, maxY)                                                \\
  (0 <= (x) && 0 <= (y) && (x) < (maxX) && (y) < (maxY))
#define DUMP(x) cerr << #x << " = " << (x) << endl

template <typename T> inline T fromString(const string &s) {
  T res;
  ISS iss(s);
  iss >> res;
  return res;
};
template <typename T> inline string toString(const T &a) {
  OSS oss;
  oss << a;
  return oss.str();
};

const int INF = 0x3f3f3f3f;
const LL INFL = 0x3f3f3f3f3f3f3f3fLL;
const int DX[] = {1, 0, -1, 0}, DY[] = {0, -1, 0, 1};

int main(void) {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int N, M;
  VI first_card(2);
  cin >> N >> first_card[0] >> first_card[1] >> M;

  vector<VVI> lines(2, VVI(N * 2 + 2, VI(N)));
  VVI all_nums(2);

  for (int i = 0; i < 2; i++) {
    for (int y = 0; y < N; y++) {
      for (int x = 0; x < N; x++) {
        int a;
        cin >> a;

        all_nums[i].PB(a);
        lines[i][y][x] = a;
        lines[i][N + x][y] = a;

        if (y == x) { // ???
          lines[i][N * 2][y] = a;
        }
        if (y + x == N - 1) { // ???
          lines[i][N * 2 + 1][y] = a;
        }
      }
    }
  }

  VI ms(M);
  for (auto &m : ms)
    cin >> m;

  // sort
  for (int i = 0; i < 2; i++) {
    for (auto &l : lines[i]) {
      sort(ALL(l));
    }
    sort(ALL(all_nums[i]));
  }

  // input m and count
  vector<VI> cnts(2, VI(N * 2 + 2, 0));
  VI oks(2);
  for (int i = 0; i < M; i++) {
    int m = ms[i];

    for (int j = 0; j < 2; j++) {
      if (!binary_search(ALL(all_nums[j]), m))
        continue;

      for (int k = 0; k < N * 2 + 2; k++) {
        if (cnts[j][k] < N && binary_search(ALL(lines[j][k]), m)) {
          cnts[j][k]++;

          if (cnts[j][k] == N) {
            oks[j]++;
          }
        }
      }

      if (N == 1 && lines[j][0][0] == m) {
        oks[j] = 1;
      }
    }

    // ????????????
    VB ends(2);
    for (int j = 0; j < 2; j++) {
      ends[j] = oks[j] >= first_card[j];
    }

    if (ends[0] ^ ends[1]) {
      cout << (ends[0] ? "USAGI" : "NEKO") << endl;
      return 0;
    }
    if (ends[0] && ends[1]) {
      cout << "DRAW" << endl;
      return 0;
    }
  }

  cout << "DRAW" << endl;

  return 0;
}