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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = (k); i <= (int)(n); ++i)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FIND(m, w) ((m).find(w) != (m).end())
map<PII, int> memo;
int dfs(int h, int r, int n, vector<PII> &ich, vector<PII> &ji) {
  if (memo.find(PII(h, r)) != memo.end()) {
    int m = memo[PII(h, r)];
    if (m >= n)
      return m;
  }
  memo[PII(h, r)] = n;
  int ret = n;
  REP(i, ich.size()) {
    if (ich[i].first < h && ich[i].second < r) {
      ret = max(ret, dfs(ich[i].first, ich[i].second, n + 1, ich, ji));
      break;
    }
  }
  REP(i, ji.size()) {
    if (ji[i].first < h && ji[i].second < r) {
      ret = max(ret, dfs(ji[i].first, ji[i].second, n + 1, ich, ji));
      break;
    }
  }
  return ret;
}
int main(void) {
  int n, m;
  while (cin >> n, n) {
    memo.clear();
    vector<PII> ich;
    vector<PII> ji;
    REP(i, n) {
      int h, r;
      cin >> h >> r;
      ich.push_back(PII(h, r));
    }
    cin >> m;
    REP(i, m) {
      int h, r;
      cin >> h >> r;
      ji.push_back(PII(h, r));
    }
    reverse(ich.begin(), ich.end());
    reverse(ji.begin(), ji.end());
    cout << dfs(INF, INF, 0, ich, ji) << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef complex<double> P;
static const double EPS = 1e-8;
static const int INF = (int)1e8;

#define FOR(i, k, n) for (int i = (k); i < (int)(n); ++i)
#define REP(i, n) for (int i = 0; i < (int)(n); ++i)
#define FOREQ(i, k, n) for (int i = (k); i <= (int)(n); ++i)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define FIND(m, w) ((m).find(w) != (m).end())
map<PII, int> memo;
int dfs(int h, int r, int n, vector<PII> &ich, vector<PII> &ji) {
  if (memo.find(PII(h, r)) != memo.end()) {
    int m = memo[PII(h, r)];
    if (m >= n)
      return m;
  }
  int ret = n;
  REP(i, ich.size()) {
    if (ich[i].first < h && ich[i].second < r) {
      ret = max(ret, dfs(ich[i].first, ich[i].second, n + 1, ich, ji));
      break;
    }
  }
  REP(i, ji.size()) {
    if (ji[i].first < h && ji[i].second < r) {
      ret = max(ret, dfs(ji[i].first, ji[i].second, n + 1, ich, ji));
      break;
    }
  }
  return ret;
}
int main(void) {
  int n, m;
  while (cin >> n, n) {
    vector<PII> ich;
    vector<PII> ji;
    REP(i, n) {
      int h, r;
      cin >> h >> r;
      ich.push_back(PII(h, r));
    }
    cin >> m;
    REP(i, m) {
      int h, r;
      cin >> h >> r;
      ji.push_back(PII(h, r));
    }
    reverse(ich.begin(), ich.end());
    reverse(ji.begin(), ji.end());
    cout << dfs(INF, INF, 0, ich, ji) << endl;
  }
  return 0;
}