Problem p01540 (C++) — buggy submission, verdict: Memory Limit Exceeded

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = 1e9 + 10;

int dp[5010][5010];
int exist[5010][5010];
int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  vector<PII> ps(N);
  VI xs(N), ys(N);
  // x??§?¨???¨y??§?¨??????\\??????????????????
  REP(i, N) {
    scanf("%d %d", &ps[i].first, &ps[i].second);
    xs[i] = ps[i].first, ys[i] = ps[i].second;
  }
  xs.PB(-INF), xs.PB(INF);
  ys.PB(-INF), ys.PB(INF);
  SORT(xs);
  SORT(ys);

  // x -> x', 0 <= x' < N+2
  map<int, int> compX, compY;
  int MX = 0;
  REP(i, N + 2)
  if (!compX.count(xs[i])) {
    compX[xs[i]] = MX++;
  }
  int MY = 0;
  REP(i, N + 2)
  if (!compY.count(ys[i])) {
    compY[ys[i]] = MY++;
  }

  REP(i, N) { ++exist[compY[ps[i].second]][compX[ps[i].first]]; }
  for (int y = 0; y < MY; ++y)
    for (int x = 0; x < MX; ++x)
      dp[y][x] = (y > 0 ? dp[y - 1][x] : 0) + (x > 0 ? dp[y][x - 1] : 0) -
                 (y > 0 && x > 0 ? dp[y - 1][x - 1] : 0) + exist[y][x];

  while (M--) {
    int sx, sy, ex, ey;
    scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
    int sxi = compX.lower_bound(sx)->second;
    int syi = compY.lower_bound(sy)->second;
    auto it = compX.upper_bound(ex);
    --it;
    int exi = it->second;
    it = compY.upper_bound(ey);
    --it;
    int eyi = it->second;

    int ans = dp[eyi][exi] - (sxi > 0 ? dp[eyi][sxi - 1] : 0) -
              (syi > 0 ? dp[syi - 1][exi] : 0) +
              (syi > 0 && sxi > 0 ? dp[syi - 1][sxi - 1] : 0);
    printf("%d\\n", ans);
  }

  return 0;
}

Error: MLE

Fixed submission:

// include
//------------------------------------------
#include <algorithm>
#include <bitset>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

// typedef
//------------------------------------------
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<string> VS;
typedef pair<int, int> PII;
typedef long long LL;

// container util
//------------------------------------------
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define PB push_back
#define MP make_pair
#define SZ(a) int((a).size())
#define EACH(i, c)                                                             \\
  for (typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)
#define EXIST(s, e) ((s).find(e) != (s).end())
#define SORT(c) sort((c).begin(), (c).end())

// repetition
//------------------------------------------
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)

// constant
//--------------------------------------------
const double EPS = 1e-10;
const double PI = acos(-1.0);
const int INF = 1e9 + 10;

int dp[5100][5100];
int exist[5100][5100];
int main() {
  int N, M;
  scanf("%d %d", &N, &M);
  vector<PII> ps(N);
  VI xs(N), ys(N);
  // x??§?¨???¨y??§?¨??????\\??????????????????
  REP(i, N) {
    scanf("%d %d", &ps[i].first, &ps[i].second);
    xs[i] = ps[i].first, ys[i] = ps[i].second;
  }
  xs.PB(-INF), xs.PB(INF);
  ys.PB(-INF), ys.PB(INF);
  SORT(xs);
  SORT(ys);

  // x -> x', 0 <= x' < N+2
  map<int, int> compX, compY;
  VI decompX(N + 2), decompY(N + 2);
  int MX = 0;
  REP(i, N + 2)
  if (!compX.count(xs[i])) {
    compX[xs[i]] = MX++;
    decompX[MX - 1] = xs[i];
  }
  int MY = 0;
  REP(i, N + 2)
  if (!compY.count(ys[i])) {
    compY[ys[i]] = MY++;
    decompY[MY - 1] = ys[i];
  }

  REP(i, N) { ++exist[compY[ps[i].second]][compX[ps[i].first]]; }
  for (int y = 0; y < MY; ++y)
    for (int x = 0; x < MX; ++x)
      dp[y][x] = (y > 0 ? dp[y - 1][x] : 0) + (x > 0 ? dp[y][x - 1] : 0) -
                 (y > 0 && x > 0 ? dp[y - 1][x - 1] : 0) + exist[y][x];

  while (M--) {
    int sx, sy, ex, ey;
    scanf("%d %d %d %d", &sx, &sy, &ex, &ey);
    int sxi = compX.lower_bound(sx)->second;
    int syi = compY.lower_bound(sy)->second;
    auto it = compX.upper_bound(ex);
    --it;
    int exi = it->second;
    it = compY.upper_bound(ey);
    --it;
    int eyi = it->second;

    int ans = dp[eyi][exi] - (sxi > 0 ? dp[eyi][sxi - 1] : 0) -
              (syi > 0 ? dp[syi - 1][exi] : 0) +
              (syi > 0 && sxi > 0 ? dp[syi - 1][sxi - 1] : 0);
    cout << ans << endl;
  }

  return 0;
}