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

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

int N, M;
int fie[5005][5005];
int rui[5005][5005];
ll xy[2][5001];
ll X[5001], Y[5001];

int W, H;

int compress(int id, ll *x) {
  vector<ll> xs;
  for (int i = 0; i < N; i++) {
    xs.push_back(xy[id][i]);
  }

  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());

  for (int i = 0; i < N; i++) {
    ll a = find(xs.begin(), xs.end(), xy[id][i]) - xs.begin();
    a++;
    x[a] = xy[id][i];
    xy[id][i] = a;
  }

  x[0] = -10000000000;

  return (int)xs.size();
}

int nibun(ll a, ll *b, int wh, bool f) {

  /*
  cout << a << " :: wh = " << wh << endl;
  for(int i=0;i<=W;i++)
    cout << b[i] << " ";
  cout << endl;
  */
  int res = lower_bound(b, b + wh + 1, a) - b;

  //  cout << res << endl;

  if (b[res] > a && !f)
    res--;

  return res;
}

void view() {
  for (int y = 0; y <= H + 1; y++) {
    for (int x = 0; x <= W + 1; x++) {
      cout << rui[x][y] << " ";
    }
    cout << endl;
  }
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> xy[0][i] >> xy[1][i];
  }

  W = compress(0, X);
  H = compress(1, Y);
  /*
  cout << "W = " << W << endl;
  for(int i=1;i<=W;i++)
    cout << X[i] << " ";
  cout << endl;
  cout << "H = " << H << endl;
  for(int i=1;i<=H;i++)
    cout << Y[i] << " ";
  cout << endl;
  */

  for (int i = 0; i < N; i++) {
    fie[xy[0][i]][xy[1][i]]++;
  }

  for (int y = 1; y <= H + 1; y++) {
    for (int x = 1; x <= W + 1; x++) {
      rui[x][y] = rui[x - 1][y] + fie[x][y];
    }
  }
  for (int y = 1; y <= H + 1; y++) {
    for (int x = 1; x <= W + 1; x++) {
      rui[x][y] += rui[x][y - 1];
    }
  }

  for (int i = 0; i < M; i++) {
    ll x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1 = nibun(x1, X, W, true);
    y1 = nibun(y1, Y, H, true);
    x2 = nibun(x2, X, W, false);
    y2 = nibun(y2, Y, H, false);
    /*
    cout << x1 << " "  << y1 << " " << x2 << " " << y2 << endl;
    cout << rui[x2][y2] << " - " << rui[x2][y1-1] << " - " << rui[x1-1][y2] << "
    + " << rui[x1][y1] << endl;
    */
    int res =
        rui[x2][y2] - rui[x2][y1 - 1] - rui[x1 - 1][y2] + rui[x1 - 1][y1 - 1];
    cout << res << endl;
  }
}

Error: MLE

Fixed submission:

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

typedef long long ll;

int N, M;
ll fie[5111][5111];
ll rui[5111][5111];
ll xy[2][5001];
ll X[5001], Y[5001];

int W, H;

int compress(int id, ll *x) {
  vector<ll> xs;
  for (int i = 0; i < N; i++) {
    xs.push_back(xy[id][i]);
  }

  sort(xs.begin(), xs.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());

  for (int i = 0; i < N; i++) {
    ll a = find(xs.begin(), xs.end(), xy[id][i]) - xs.begin();
    a++;
    x[a] = xy[id][i];
    xy[id][i] = a;
  }

  x[0] = -10000000000;

  return (int)xs.size();
}

int nibun(ll a, ll *b, int wh, bool f) {

  /*
  cout << a << " :: wh = " << wh << endl;
  for(int i=0;i<=W;i++)
    cout << b[i] << " ";
  cout << endl;
  */
  int res = lower_bound(b, b + wh + 1, a) - b;

  //  cout << res << endl;

  if (b[res] > a && !f)
    res--;

  return res;
}

void view() {
  for (int y = 0; y <= H + 1; y++) {
    for (int x = 0; x <= W + 1; x++) {
      cout << rui[x][y] << " ";
    }
    cout << endl;
  }
}

int main() {
  cin >> N >> M;
  for (int i = 0; i < N; i++) {
    cin >> xy[0][i] >> xy[1][i];
  }

  W = compress(0, X);
  H = compress(1, Y);
  /*
  cout << "W = " << W << endl;
  for(int i=1;i<=W;i++)
    cout << X[i] << " ";
  cout << endl;
  cout << "H = " << H << endl;
  for(int i=1;i<=H;i++)
    cout << Y[i] << " ";
  cout << endl;
  */

  for (int i = 0; i < N; i++) {
    fie[xy[0][i]][xy[1][i]]++;
  }

  for (int y = 1; y <= H + 1; y++) {
    for (int x = 1; x <= W + 1; x++) {
      rui[x][y] = rui[x - 1][y] + fie[x][y];
    }
  }
  for (int y = 1; y <= H + 1; y++) {
    for (int x = 1; x <= W + 1; x++) {
      rui[x][y] += rui[x][y - 1];
    }
  }

  for (int i = 0; i < M; i++) {
    ll x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1 = nibun(x1, X, W, true);
    y1 = nibun(y1, Y, H, true);
    x2 = nibun(x2, X, W, false);
    y2 = nibun(y2, Y, H, false);
    /*
    cout << x1 << " "  << y1 << " " << x2 << " " << y2 << endl;
    cout << rui[x2][y2] << " - " << rui[x2][y1-1] << " - " << rui[x1-1][y2] << "
    + " << rui[x1][y1] << endl;
    */
    int res =
        rui[x2][y2] - rui[x2][y1 - 1] - rui[x1 - 1][y2] + rui[x1 - 1][y1 - 1];
    cout << res << endl;
  }
}