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

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int MAX_N = 5010;
const int MAX_M = 5 * 100010;
const int MAX_Y = MAX_N + MAX_M * 2;
const int MAX_W = MAX_N + MAX_M * 2;

int H, W, N, M, tsx[MAX_N], tsy[MAX_N];
vector<int> X, Y;

void init_number() {
  set<int> Ys, Xs;
  REP(i, N) {
    Xs.insert(tsx[i]);
    Ys.insert(tsy[i]);
  }
  Ys.insert((1e9 + 10) * -1);
  Ys.insert(1e9 + 10);
  Xs.insert((1e9 + 10) * -1);
  Xs.insert(1e9 + 10);
  X = vector<int>(Xs.begin(), Xs.end());
  Y = vector<int>(Ys.begin(), Ys.end());
  W = X.size();
  H = Y.size();
}

void init(vector<vector<int>> &v, vector<vector<int>> &E) {
  E[0][0] = v[0][0];
  FOR(i, 1, H) E[i][0] = E[i - 1][0] + v[i][0];
  FOR(i, 1, W) E[0][i] = E[0][i - 1] + v[0][i];
  FOR(y, 1, H)
  FOR(x, 1, W)
  E[y][x] = v[y][x] + E[y - 1][x] + E[y][x - 1] - E[y - 1][x - 1];
}

int calc(int Y1, int X1, int Y2, int X2, vector<vector<int>> &E) {
  int ret = E[Y2][X2];
  if (X1 - 1 >= 0)
    ret -= E[Y2][X1 - 1];
  if (Y1 - 1 >= 0)
    ret -= E[Y1 - 1][X2];
  if (X1 - 1 >= 0 && Y1 - 1 >= 0)
    ret += E[Y1 - 1][X1 - 1];
  return ret;
}

int main() {
  cin >> N >> M;
  REP(i, N) cin >> tsx[i] >> tsy[i];
  init_number();
  vector<vector<int>> v(H, vector<int>(W, 0)), E(H, vector<int>(W, 0));
  REP(i, N) {
    int y = lower_bound(Y.begin(), Y.end(), tsy[i]) - Y.begin();
    int x = lower_bound(X.begin(), X.end(), tsx[i]) - X.begin();
    v[y][x] += 1;
  }
  init(v, E);
  REP(i, M) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1 = lower_bound(X.begin(), X.end(), x1) - X.begin();
    y1 = lower_bound(Y.begin(), Y.end(), y1) - Y.begin();
    x2 = upper_bound(X.begin(), X.end(), x2) - X.begin() - 1;
    y2 = upper_bound(Y.begin(), Y.end(), y2) - Y.begin() - 1;
    cout << calc(y1, x1, y2, x2, E) << endl;
  }
  return 0;
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>
using namespace std;
#define REP(i, j) for (int i = 0; i < (int)(j); ++i)
#define FOR(i, j, k) for (int i = (int)(j); i < (int)(k); ++i)
#define SORT(v) sort((v).begin(), (v).end())
#define REVERSE(v) reverse((v).begin(), (v).end())
typedef pair<int, int> P;
const int MAX_N = 5010;
const int MAX_M = 5 * 100010;
const int MAX_Y = MAX_N + MAX_M * 2;
const int MAX_W = MAX_N + MAX_M * 2;

int H, W, tsx[MAX_N], tsy[MAX_N], lsx[MAX_M], lsy[MAX_M], rsx[MAX_M],
    rsy[MAX_M], N, M;
vector<int> X, Y;
map<int, int> Xm, Ym;

void init_number() {
  set<int> Ys, Xs;
  REP(i, N) {
    Xs.insert(tsx[i]);
    Ys.insert(tsy[i]);
  }
  REP(i, M) {
    Xs.insert(lsx[i]);
    Ys.insert(lsy[i]);
  }
  REP(i, M) {
    Xs.insert(rsx[i]);
    Ys.insert(rsy[i]);
  }
  Ys.insert((1e9 + 10) * -1);
  Ys.insert(1e9 + 10);
  Xs.insert((1e9 + 10) * -1);
  Xs.insert(1e9 + 10);
  X = vector<int>(Xs.begin(), Xs.end());
  Y = vector<int>(Ys.begin(), Ys.end());
  W = X.size();
  H = Y.size();
  REP(i, N) {
    int x = tsx[i], y = tsy[i];
    Xm[x] = lower_bound(X.begin(), X.end(), x) - X.begin();
    Ym[y] = lower_bound(Y.begin(), Y.end(), y) - Y.begin();
  }
  REP(i, M) {
    int x = lsx[i], y = lsy[i];
    Xm[x] = lower_bound(X.begin(), X.end(), x) - X.begin();
    Ym[y] = lower_bound(Y.begin(), Y.end(), y) - Y.begin();
    x = rsx[i];
    y = rsy[i];
    Xm[x] = lower_bound(X.begin(), X.end(), x) - X.begin();
    Ym[y] = lower_bound(Y.begin(), Y.end(), y) - Y.begin();
  }
}

void init(vector<vector<int>> &v, vector<vector<int>> &E) {
  E[0][0] = v[0][0];
  FOR(i, 1, H) E[i][0] = E[i - 1][0] + v[i][0];
  FOR(i, 1, W) E[0][i] = E[0][i - 1] + v[0][i];
  FOR(y, 1, H)
  FOR(x, 1, W)
  E[y][x] = v[y][x] + E[y - 1][x] + E[y][x - 1] - E[y - 1][x - 1];
}

int calc(int Y1, int X1, int Y2, int X2, vector<vector<int>> &E) {
  int ret = E[Y2][X2];
  if (X1 - 1 >= 0)
    ret -= E[Y2][X1 - 1];
  if (Y1 - 1 >= 0)
    ret -= E[Y1 - 1][X2];
  if (X1 - 1 >= 0 && Y1 - 1 >= 0)
    ret += E[Y1 - 1][X1 - 1];
  return ret;
}

int main() {
  cin >> N >> M;
  REP(i, N) cin >> tsx[i] >> tsy[i];
  REP(i, M) cin >> lsx[i] >> lsy[i] >> rsx[i] >> rsy[i];
  init_number();
  vector<vector<int>> v(H, vector<int>(W, 0)), E(H, vector<int>(W, 0));
  REP(i, N) v[Ym[tsy[i]]][Xm[tsx[i]]] += 1;
  init(v, E);
  REP(i, M)
      cout << calc(Ym[lsy[i]], Xm[lsx[i]], Ym[rsy[i]], Xm[rsx[i]], E) << endl;
  ;
  return 0;
}