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

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define REP(i, n) for (integer i = 0; i < (integer)(n); i++)

typedef long long int integer;
typedef pair<integer, integer> P;
const int MAX = 5004;

int acc[MAX + 8][MAX + 8];
int trecd[MAX + 8][MAX + 8];

int main() {
  integer n, m;
  cin >> n >> m;
  vector<integer> xs, ys;
  vector<P> treasures(n);
  for (P &p : treasures) {
    cin >> p.first >> p.second;
    xs.push_back(p.first);
    ys.push_back(p.second);
  }
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  sort(ys.begin(), ys.end());
  ys.erase(unique(ys.begin(), ys.end()), ys.end());
  vector<integer> rxs = xs, rys = ys;
  reverse(rxs.begin(), rxs.end());
  for (integer &rx : rxs)
    rx = -rx;
  reverse(rys.begin(), rys.end());
  for (integer &ry : rys)
    ry = -ry;

  for (P p : treasures) {
    integer x = find(xs.begin(), xs.end(), p.first) - xs.begin() + 1;
    integer y = find(ys.begin(), ys.end(), p.second) - ys.begin() + 1;
    trecd[x][y]++;
  }

  for (integer y = 1; y < MAX; y++)
    for (integer x = 1; x < MAX; x++) {
      acc[x][y] =
          acc[x][y - 1] + acc[x - 1][y] - acc[x - 1][y - 1] + trecd[x][y];
    }

  REP(i, m) {
    integer xb, yb, xe, ye;
    cin >> xb >> yb >> xe >> ye;
    xb = lower_bound(xs.begin(), xs.end(), xb) - xs.begin() + 1;
    yb = lower_bound(ys.begin(), ys.end(), yb) - ys.begin() + 1;
    xe = rxs.end() - lower_bound(rxs.begin(), rxs.end(), -xe);
    ye = rys.end() - lower_bound(rys.begin(), rys.end(), -ye);
    integer ans =
        acc[xe][ye] - acc[xb - 1][ye] - acc[xe][yb - 1] + acc[xb - 1][yb - 1];
    cout << ans << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
#include <vector>
using namespace std;
#define REP(i, n) for (integer i = 0; i < (integer)(n); i++)

typedef long long int integer;
typedef pair<integer, integer> P;
const integer MAX = 5000;

integer acc[MAX + 8][MAX + 8];
integer trecd[MAX + 8][MAX + 8];

int main() {
  integer n, m;
  cin >> n >> m;
  vector<integer> xs, ys;
  vector<P> treasures(n);
  for (P &p : treasures) {
    cin >> p.first >> p.second;
    xs.push_back(p.first);
    ys.push_back(p.second);
  }
  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  sort(ys.begin(), ys.end());
  ys.erase(unique(ys.begin(), ys.end()), ys.end());

  /*  for (integer x : xs) cout << x << " "; cout << endl;
    for (integer y : ys) cout << y << " "; cout << endl;*/

  for (P p : treasures) {
    integer x = find(xs.begin(), xs.end(), p.first) - xs.begin() + 1;
    integer y = find(ys.begin(), ys.end(), p.second) - ys.begin() + 1;
    trecd[x][y]++;
  }

  for (integer y = 1; y < MAX; y++)
    for (integer x = 1; x < MAX; x++) {
      acc[x][y] =
          acc[x][y - 1] + acc[x - 1][y] - acc[x - 1][y - 1] + trecd[x][y];
    }

  /*  REP(y, 10) {
      REP(x, 10) {
        cout << acc[x][y] << " ";
      }
      cout << endl;
    }*/

  REP(i, m) {
    integer xb, yb, xe, ye;
    cin >> xb >> yb >> xe >> ye;
    xb = upper_bound(xs.begin(), xs.end(), xb - 1) - xs.begin() + 1;
    yb = upper_bound(ys.begin(), ys.end(), yb - 1) - ys.begin() + 1;
    xe = *(lower_bound(xs.begin(), xs.end(), xe + 1) - 1);
    xe = lower_bound(xs.begin(), xs.end(), xe) - xs.begin() + 1;
    xe = max(xb, xe);
    ye = *(lower_bound(ys.begin(), ys.end(), ye + 1) - 1);
    ye = lower_bound(ys.begin(), ys.end(), ye) - ys.begin() + 1;
    ye = max(yb, ye);
    //    cout << "(" << xb << " " << yb << " " << xe << " " << ye << ")"<<
    //    endl;
    integer ans =
        acc[xe][ye] - acc[xb - 1][ye] - acc[xe][yb - 1] + acc[xb - 1][yb - 1];
    cout << ans << endl;
  }
}