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

#include <bits/stdc++.h>
#define M 100000
#define Y second
#define X first
#define N 500
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> P1;
int n, u, v, m, a[2][N][N], b, f1, f2;
bool usa[N][N], nek[N][N];
int usac[N * 2 + 2], cu;
int nekc[N * 2 + 2], cn;
vector<P1> x;
vector<P> d;
set<int> s;
map<int, P> z[2];

int Bynary_Search(int k) {
  int L = 0, R = d.size();
  while (L < R) {
    int Mid = (L + R) / 2;
    if (d[Mid].X < k)
      L = Mid + 1;
    else
      R = Mid;
  }
  if (d[L].X == k)
    return L;
  return -1;
}

int main() {
  cin >> n >> u >> v >> m;
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++) {
        cin >> a[i][j][k];
        z[i].insert(make_pair(a[i][j][k], P(j, k)));
        s.insert(a[i][j][k]);
      }
  for (int i = 0; i < m; i++) {
    cin >> b;
    d.push_back(P(b, i));
  }
  sort(d.begin(), d.end());
  set<int>::iterator ite;
  for (ite = s.begin(); ite != s.end(); ite++) {
    int r = Bynary_Search(*ite);
    if (r >= 0) {
      if (z[0].find(d[r].X) != z[0].end())
        x.push_back(P1(P(d[r].Y, 0), P(z[0][d[r].X].X, z[0][d[r].X].Y)));
      if (z[1].find(d[r].X) != z[1].end())
        x.push_back(P1(P(d[r].Y, 1), P(z[1][d[r].X].X, z[1][d[r].X].Y)));
    }
  }
  sort(x.begin(), x.end());
  for (int i = 0; i < x.size(); i++) {
    int p = x[i].X.Y, px = x[i].Y.X, py = x[i].Y.Y;
    if (!p) {
      usac[px]++;
      if (n != 1) {
        usac[py + n]++;
        if (px == py) {
          usac[n * 2]++;
          if (usac[n * 2] == n)
            cu++;
        }
        if (px + py == n - 1) {
          usac[n * 2 + 1]++;
          if (usac[n * 2 + 1] == n)
            cu++;
        }
      }
      if (usac[px] == n)
        cu++;
      if (usac[py + n] == n)
        cu++;
    } else {
      nekc[px]++;
      if (n != 1) {
        nekc[py + n]++;
        if (px == py) {
          nekc[n * 2]++;
          if (nekc[n * 2] == n)
            cn++;
        }
        if (px + py == n - 1) {
          nekc[n * 2 + 1]++;
          if (nekc[n * 2 + 1] == n)
            cn++;
        }
      }
      if (nekc[px] == n)
        cn++;
      if (nekc[py + n] == n)
        cn++;
    }
    if (i + 1 < x.size() && x[i].X.X != x[i + 1].X.X) {
      if (cu >= u)
        f1 = 1;
      if (cn >= v)
        f2 = 1;
      if (f1 || f2)
        break;
    }
  }
  if (cu >= u)
    f1 = 1;
  if (cn >= v)
    f2 = 1;
  if ((!f1 && !f2) || (f1 && f2))
    cout << "DRAW" << endl;
  else if (f1)
    cout << "USAGI" << endl;
  else
    cout << "NEKO" << endl;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define M 100000
#define Y second
#define X first
#define N 500
using namespace std;
typedef pair<int, int> P;
typedef pair<P, P> P1;
int n, u, v, m, a[2][N][N], b, f1, f2;
bool usa[N][N], nek[N][N];
int usac[N * 2 + 2], cu;
int nekc[N * 2 + 2], cn;
vector<P1> x;
vector<P> d;
set<int> s;

int Bynary_Search(int k) {
  int L = 0, R = d.size();
  while (L < R) {
    int Mid = (L + R) / 2;
    if (d[Mid].X < k)
      L = Mid + 1;
    else
      R = Mid;
  }
  if (d[L].X == k)
    return L;
  return -1;
}

int main() {
  cin >> n >> u >> v >> m;
  for (int i = 0; i < 2; i++)
    for (int j = 0; j < n; j++)
      for (int k = 0; k < n; k++) {
        cin >> a[i][j][k];
        s.insert(a[i][j][k]);
      }
  for (int i = 0; i < m; i++) {
    cin >> b;
    d.push_back(P(b, i));
  }
  sort(d.begin(), d.end());
  set<int>::iterator ite;
  for (ite = s.begin(); ite != s.end(); ite++) {
    int r = Bynary_Search(*ite);
    if (r >= 0) {
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          if (d[r].X == a[0][i][j])
            x.push_back(P1(P(d[r].Y, 0), P(i, j)));
      for (int i = 0; i < n; i++)
        for (int j = 0; j < n; j++)
          if (d[r].X == a[1][i][j])
            x.push_back(P1(P(d[r].Y, 1), P(i, j)));
    }
  }
  sort(x.begin(), x.end());
  for (int i = 0; i < x.size(); i++) {
    int p = x[i].X.Y, px = x[i].Y.X, py = x[i].Y.Y;
    if (!p) {
      usac[px]++;
      if (n != 1) {
        usac[py + n]++;
        if (px == py) {
          usac[n * 2]++;
          if (usac[n * 2] == n)
            cu++;
        }
        if (px + py == n - 1) {
          usac[n * 2 + 1]++;
          if (usac[n * 2 + 1] == n)
            cu++;
        }
      }
      if (usac[px] == n)
        cu++;
      if (usac[py + n] == n)
        cu++;
    } else {
      nekc[px]++;
      if (n != 1) {
        nekc[py + n]++;
        if (px == py) {
          nekc[n * 2]++;
          if (nekc[n * 2] == n)
            cn++;
        }
        if (px + py == n - 1) {
          nekc[n * 2 + 1]++;
          if (nekc[n * 2 + 1] == n)
            cn++;
        }
      }
      if (nekc[px] == n)
        cn++;
      if (nekc[py + n] == n)
        cn++;
    }
    if (i + 1 < x.size() && x[i].X.X != x[i + 1].X.X) {
      if (cu >= u)
        f1 = 1;
      if (cn >= v)
        f2 = 1;
      if (f1 || f2)
        break;
    }
  }
  if (cu >= u)
    f1 = 1;
  if (cn >= v)
    f2 = 1;
  if ((!f1 && !f2) || (f1 && f2))
    cout << "DRAW" << endl;
  else if (f1)
    cout << "USAGI" << endl;
  else
    cout << "NEKO" << endl;
  return 0;
}