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

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define INF 1 << 30
#define DEBUG 0
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.TH";
string NAME;
string itos(int n) {
  stringstream ss;
  ss << n;

  return ss.str();
}
vector<int> x, y;   // 座標
vector<int> xx, yy; // 圧縮した座標

int tp[5005][5005];

void compress(void) {
  x.push_back(-INF);
  y.push_back(-INF);
  sort(ALL(x));
  sort(ALL(y));
  x.erase(unique(ALL(x)), x.end());
  y.erase(unique(ALL(y)), y.end());
  xx.clear();
  yy.clear();
  int sx = x.size();
  int sy = y.size();
  xx.resize(sx, 0);
  yy.resize(sy, 0);
  rep(j, sx) { xx[j] = x[j]; } // end rep
  rep(i, sy) { yy[i] = y[i]; } // end rep
}

int sum(int sx, int sy, int ex, int ey) {
  int ssx = max(sx - 1, 0);
  int ssy = max(sy - 1, 0);

  return (tp[ey][ex] - tp[ssy][ex] - tp[ey][ssx] + tp[ssy][ssx]);
}

int main() {
//	cut here before submit
#if DEBUG
  NAME = FILE_NAME;
  int CNT = 1;
  NAME += itos(CNT);
  while (freopen(NAME.c_str(), "r", stdin) != NULL) {
#endif
    int n, m;
    scanf("%d %d", &n, &m);
    x.clear();
    y.clear();
    vector<int> tx(n, 0), ty(n, 0);
    rep(i, n) {
      scanf("%d %d", &tx[i], &ty[i]);
      x.push_back(tx[i]);
      y.push_back(ty[i]);
    } // end rep

    compress();

    memset(tp, 0, sizeof(tp));
    rep(i, n) {
      int rx = lower_bound(ALL(xx), tx[i]) - xx.begin();
      int ry = lower_bound(ALL(yy), ty[i]) - yy.begin();
      tp[ry][rx]++;
    } // end rep

    int nx = x.size();
    int ny = y.size();
    for (int i = 0; i < ny; i++) {
      for (int j = 1; j < nx; j++) {
        tp[i][j] += tp[i][j - 1];
      } // end for
    }   // end for

    for (int i = 1; i < ny; i++) {
      for (int j = 0; j < nx; j++) {
        tp[i][j] += tp[i - 1][j];
      } // end for
    }   // end for

    rep(j, m) {
      int x1, y1, x2, y2;
      scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
      int xx1 = lower_bound(ALL(xx), x1) - xx.begin();
      int xx2 = upper_bound(ALL(xx), x2) - xx.begin() - 1;
      int yy1 = lower_bound(ALL(yy), y1) - yy.begin();
      int yy2 = upper_bound(ALL(yy), y2) - yy.begin() - 1;
      int res = 0;
      if (xx1 <= xx2 && yy1 <= yy2)
        res = sum(xx1, yy1, xx2, yy2);
      printf("%d\\n", res);
    } // end rep

#if DEBUG
    CNT++; // cut here before submit
    NAME = FILE_NAME;
    NAME += itos(CNT);
  } // end loop; cut here before submit
#endif
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm> // require sort next_permutation count __gcd reverse etc.
#include <cctype>    // require tolower, toupper
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>  // require scanf printf
#include <cstdlib> // require abs exit
#include <cstring> // require memset
#include <ctime>
#include <deque>
#include <fstream> // require freopen
#include <functional>
#include <iomanip> // require setw
#include <iostream>
#include <limits>
#include <map>
#include <numeric> // require accumulate
#include <queue>
#include <set>
#include <sstream> // require stringstream
#include <stack>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define ALL(A) A.begin(), A.end()
#define INF 1 << 30
#define DEBUG 0
using namespace std;

typedef long long ll;
typedef pair<int, int> P;
string FILE_NAME = "testcase.TH";
string NAME;
string itos(int n) {
  stringstream ss;
  ss << n;

  return ss.str();
}
vector<int> x, y;   // 座標
vector<int> xx, yy; // 圧縮した座標

int tp[5005][5005];

void compress(void) {
  x.push_back(-INF);
  y.push_back(-INF);
  sort(ALL(x));
  sort(ALL(y));
  x.erase(unique(ALL(x)), x.end());
  y.erase(unique(ALL(y)), y.end());
  xx.clear();
  yy.clear();
  int sx = x.size();
  int sy = y.size();
  xx.resize(sx, 0);
  yy.resize(sy, 0);
  rep(j, sx) { xx[j] = x[j]; } // end rep
  rep(i, sy) { yy[i] = y[i]; } // end rep
}

int sum(int sx, int sy, int ex, int ey) {
  int ssx = max(sx - 1, 0);
  int ssy = max(sy - 1, 0);

  return (tp[ey][ex] - tp[ssy][ex] - tp[ey][ssx] + tp[ssy][ssx]);
}

int main() {
//	cut here before submit
#if DEBUG
  NAME = FILE_NAME;
  int CNT = 1;
  NAME += itos(CNT);
  while (freopen(NAME.c_str(), "r", stdin) != NULL) {
#endif
    memset(tp, 0, sizeof(tp));
    int n, m;
    scanf("%d %d", &n, &m);
    map<P, int> cnt;
    cnt.clear();
    x.clear();
    y.clear();
    vector<int> tx(n, 0), ty(n, 0);
    rep(i, n) {
      scanf("%d %d", &tx[i], &ty[i]);
      cnt[P(ty[i], tx[i])]++;
      x.push_back(tx[i]);
      y.push_back(ty[i]);
    } // end rep

    compress();
    int nx = x.size();
    int ny = y.size();
    tp[0][0] = cnt[P(yy[0], xx[0])];
    rep(i, ny) rep(j, nx) { tp[i][j] = cnt[P(yy[i], xx[j])]; } // end rep

    for (int i = 0; i < ny; i++) {
      for (int j = 1; j < nx; j++) {
        tp[i][j] += tp[i][j - 1];
      } // end for
    }   // end for

    for (int i = 1; i < ny; i++) {
      for (int j = 0; j < nx; j++) {
        tp[i][j] += tp[i - 1][j];
      } // end for
    }   // end for

    rep(j, m) {
      int x1, y1, x2, y2;
      scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
      int xx1 = lower_bound(ALL(xx), x1) - xx.begin();
      int xx2 = upper_bound(ALL(xx), x2) - xx.begin() - 1;
      int yy1 = lower_bound(ALL(yy), y1) - yy.begin();
      int yy2 = upper_bound(ALL(yy), y2) - yy.begin() - 1;
      int res = 0;
      if (xx1 <= xx2 && yy1 <= yy2)
        res = sum(xx1, yy1, xx2, yy2);
      printf("%d\\n", res);
    } // end rep

#if DEBUG
    CNT++; // cut here before submit
    NAME = FILE_NAME;
    NAME += itos(CNT);
  } // end loop; cut here before submit
#endif
  return 0;
}