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

#include <bits/stdc++.h>

#define ll long long
#define llong long long
#define REP(i, n) for (auto i = 0; i < n; ++i)
#define REPI(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define FOR(i, a, b) for (auto i = a; i < b++ i)
#define ALL(v) v.begin(), v.end()
#define pb push_back

using namespace std;

int win(vector<vector<int>> &v, vector<int> &c, int u) {
  int n = v.size(), m = c.size();
  vector<vector<bool>> ismark(n, vector<bool>(n, false));
  map<int, pair<int, int>> idxs;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++) {
      idxs[v[i][j]] = make_pair(i, j);
    }
  vector<bool> bingo(2 * n + 2, false);
  for (int i = 0; i < m; i++) {
    auto element = idxs.find(c[i]);
    if (element == idxs.end())
      continue;
    auto idx = element->second;
    ismark[idx.first][idx.second] = true;
    bool row = true, col = true;
    for (int j = 0; j < n; j++)
      if (!ismark[j][idx.second]) {
        row = false;
        break;
      }
    for (int j = 0; j < n; j++)
      if (!ismark[idx.first][j]) {
        col = false;
        break;
      }
    bingo[idx.first] = row || bingo[idx.first];
    if (n != 1) {
      bingo[n + idx.second] = col || bingo[n + idx.second];
    }
    {
      bool s = true, bs = true;
      for (int j = 0; j < n; j++)
        if (!ismark[j][j]) {
          s = false;
          break;
        }
      for (int j = 0; j < n; j++)
        if (!ismark[j][n - j - 1]) {
          bs = false;
          break;
        }
      if (n != 1) {
        bingo[2 * n] = s || bingo[2 * n];
        bingo[2 * n + 1] = bs || bingo[2 * n + 1];
      }
    }
    int b = 0;
    for (auto e : bingo) {
      if (e)
        b++;
    }
    if (b >= u)
      return i;
  }
  return m;
}

int main() {
  int n, u, v, m;
  cin >> n >> u >> v >> m;
  vector<vector<int>> usa(n, vector<int>(n)), nek(n, vector<int>(n));
  for (auto &v : usa)
    for (auto &e : v)
      cin >> e;
  for (auto &v : nek)
    for (auto &e : v)
      cin >> e;
  vector<int> c(m);
  for (auto &e : c)
    cin >> e;

  int usagi = win(usa, c, u);
  int neko = win(nek, c, v);
  if (usagi < neko)
    cout << "USAGI" << endl;
  else if (usagi > neko)
    cout << "NEKO" << endl;
  else
    cout << "DRAW" << endl;

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define ll long long
#define llong long long
#define REP(i, n) for (auto i = 0; i < n; ++i)
#define REPI(itr, v) for (auto itr = v.begin(); itr != v.end(); ++itr)
#define FOR(i, a, b) for (auto i = a; i < b++ i)
#define ALL(v) v.begin(), v.end()
#define pb push_back

using namespace std;

pair<int, int> find(vector<vector<int>> &v, int num) {
  for (int i = 0; i < (int)v.size(); i++) {
    for (int j = 0; j < (int)v[i].size(); j++) {
      if (v[i][j] == num) {
        return make_pair(i, j);
      }
    }
  }
  return make_pair(-1, -1);
}

int win(vector<vector<int>> &v, vector<int> &c, int u) {
  int n = v.size(), m = c.size();
  vector<vector<bool>> ismark(n, vector<bool>(n, false));
  vector<bool> bingo(2 * n + 2, false);
  for (int i = 0; i < m; i++) {
    auto idx = find(v, c[i]);
    if (idx == make_pair(-1, -1))
      continue;
    ismark[idx.first][idx.second] = true;
    bool row = true, col = true;
    for (int j = 0; j < n; j++)
      if (!ismark[j][idx.second]) {
        row = false;
        break;
      }
    for (int j = 0; j < n; j++)
      if (!ismark[idx.first][j]) {
        col = false;
        break;
      }
    bingo[idx.first] = row || bingo[idx.first];
    if (n != 1) {
      bingo[n + idx.second] = col || bingo[n + idx.second];
    }
    {
      bool s = true, bs = true;
      for (int j = 0; j < n; j++)
        if (!ismark[j][j]) {
          s = false;
          break;
        }
      for (int j = 0; j < n; j++)
        if (!ismark[j][n - j - 1]) {
          bs = false;
          break;
        }
      if (n != 1) {
        bingo[2 * n] = s || bingo[2 * n];
        bingo[2 * n + 1] = bs || bingo[2 * n + 1];
      }
    }
    int b = 0;
    for (auto e : bingo) {
      if (e)
        b++;
    }
    if (b >= u)
      return i;
  }
  return m;
}

int main() {
  int n, u, v, m;
  cin >> n >> u >> v >> m;
  vector<vector<int>> usa(n, vector<int>(n)), nek(n, vector<int>(n));
  for (auto &v : usa)
    for (auto &e : v)
      cin >> e;
  for (auto &v : nek)
    for (auto &e : v)
      cin >> e;
  vector<int> c(m);
  for (auto &e : c)
    cin >> e;

  int usagi = win(usa, c, u);
  int neko = win(nek, c, v);
  if (usagi < neko)
    cout << "USAGI" << endl;
  else if (usagi > neko)
    cout << "NEKO" << endl;
  else
    cout << "DRAW" << endl;

  return 0;
}