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

#include <iostream>
#include <map>
#include <vector>

using namespace std;

int main(void) {
  int n, u, v, m;
  cin >> n >> u >> v >> m;
  map<int, int> rcol, ccol;
  map<int, int> rrow, crow;
  map<int, bool> rdia, cdia;
  map<int, bool> rrdia, crdia;
  int temp;
  if (n > 1) {
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cin >> temp;
        rcol[temp] = j + 1;
        rrow[temp] = i + 1;
        if (i == j) {
          rdia[temp] = true;
        }
        if (i + j == n - 1) {
          rrdia[temp] = true;
        }
      }
    }
    for (int i = 0; i < n; ++i) {
      for (int j = 0; j < n; ++j) {
        cin >> temp;
        ccol[temp] = j + 1;
        crow[temp] = i + 1;
        if (i == j) {
          cdia[temp] = true;
        }
        if (i + j == n - 1) {
          crdia[temp] = true;
        }
      }
    }
  } else {
    cin >> temp;
    rdia[temp] = true;
    cin >> temp;
    cdia[temp] = true;
  }

  int card, rc, cc;
  vector<int> rc_col(n, 0), cc_col(n, 0);
  vector<int> rc_row(n, 0), cc_row(n, 0);
  vector<int> rc_dia(2, 0), cc_dia(2, 0);
  rc = cc = 0;
  bool fin = false;
  for (int i = 0; i < m; ++i) {
    cin >> card;
    if (!fin) {
      if (rcol[card] > 0) {
        ++rc_col[rcol[card] - 1];
        if (rc_col[rcol[card] - 1] == n) {
          ++rc;
        }
      }
      if (rrow[card] > 0) {
        ++rc_row[rrow[card] - 1];
        if (rc_row[rrow[card] - 1] == n) {
          ++rc;
        }
      }
      if (rdia[card]) {
        ++rc_dia[0];
        if (rc_dia[0] == n) {
          ++rc;
        }
      }
      if (rrdia[card]) {
        ++rc_dia[1];
        if (rc_dia[1] == n) {
          ++rc;
        }
      }
      if (ccol[card] > 0) {
        ++cc_col[ccol[card] - 1];
        if (cc_col[ccol[card] - 1] == n) {
          ++cc;
        }
      }
      if (crow[card] > 0) {
        ++cc_row[crow[card] - 1];
        if (cc_row[crow[card] - 1] == n) {
          ++cc;
        }
      }
      if (cdia[card]) {
        ++cc_dia[0];
        if (cc_dia[0] == n) {
          ++cc;
        }
      }
      if (crdia[card]) {
        ++cc_dia[1];
        if (cc_dia[1] == n) {
          ++cc;
        }
      }

      if (rc >= u && cc >= v) {
        cout << "DRAW" << endl;
        fin = true;
      } else if (rc >= u) {
        cout << "USAGI" << endl;
        fin = true;
      } else if (cc >= v) {
        cout << "NEKO" << endl;
        fin = true;
      }
    }
  }
  if (!fin) {
    cout << "DRAW" << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int main(void) {
  int n, u, v, m;
  cin >> n >> u >> v >> m;
  vector<vector<int>> rab(n, vector<int>(n));
  vector<vector<int>> cat(n, vector<int>(n));
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> rab[i][j];
    }
  }
  for (int i = 0; i < n; ++i) {
    for (int j = 0; j < n; ++j) {
      cin >> cat[i][j];
    }
  }

  if (n > 1) {
    int card, cr, cc;
    cr = cc = 0;
    vector<int> cr_col(n, 0), cc_col(n, 0);
    vector<int> cr_row(n, 0), cc_row(n, 0);
    vector<int> cr_dia(2, 0), cc_dia(2, 0);
    bool find, fin = false;
    for (int i = 0; i < m; ++i) {
      cin >> card;
      if (!fin) {
        find = false;
        for (int i = 0; !find && i < n; ++i) {
          for (int j = 0; !find && j < n; ++j) {
            if (rab[i][j] == card) {
              find = true;
              ++cr_col[j];
              if (cr_col[j] == n) {
                ++cr;
              }
              ++cr_row[i];
              if (cr_row[i] == n) {
                ++cr;
              }
              if (i == j) {
                ++cr_dia[0];
                if (cr_dia[0] == n) {
                  ++cr;
                }
              }
              if (i + j == n - 1) {
                ++cr_dia[1];
                if (cr_dia[1] == n) {
                  ++cr;
                }
              }
            }
          }
        }

        find = false;
        for (int i = 0; !find && i < n; ++i) {
          for (int j = 0; !find && j < n; ++j) {
            if (cat[i][j] == card) {
              find = true;
              ++cc_col[j];
              if (cc_col[j] == n) {
                ++cc;
              }
              ++cc_row[i];
              if (cc_row[i] == n) {
                ++cc;
              }
              if (i == j) {
                ++cc_dia[0];
                if (cc_dia[0] == n) {
                  ++cc;
                }
              }
              if (i + j == n - 1) {
                ++cc_dia[1];
                if (cc_dia[1] == n) {
                  ++cc;
                }
              }
            }
          }
        }
        if (cr >= u && cc >= v) {
          cout << "DRAW" << endl;
          fin = true;
        } else if (cr >= u) {
          cout << "USAGI" << endl;
          fin = true;
        } else if (cc >= v) {
          cout << "NEKO" << endl;
          fin = true;
        }
      }
    }
    if (!fin) {
      cout << "DRAW" << endl;
    }
  } else {
    bool wr, wc;
    wr = wc = true;
    if (u > 1) {
      wr = false;
    }
    if (v > 1) {
      wc = false;
    }
    int card;
    bool fin, usa, neko;
    fin = usa = neko = false;
    for (int i = 0; i < m; ++i) {
      cin >> card;
      if (!fin) {
        if (wr) {
          if (rab[0][0] == card) {
            usa = true;
          }
        }
        if (wc) {
          if (cat[0][0] == card) {
            neko = true;
          }
        }
        if (usa && neko) {
          cout << "DRAW" << endl;
          fin = true;
        } else if (usa) {
          cout << "USAGI" << endl;
          fin = true;
        } else if (neko) {
          cout << "NEKO" << endl;
          fin = true;
        }
      }
    }
    if (!fin) {
      cout << "DRAW" << endl;
    }
  }
  return 0;
}