Problem p01540 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int mp[5005][5005], n, m;
vector<int> x, y;
// O(n * log n ) n=v.size()
vector<int> compress(vector<int> v) {
  // v = {4,6,7,1,1,6}
  //   -> res={1,2,3,0,0,2}

  vector<int> copy = v;

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

  vector<int> res;
  for (int i = 0; i < (int)copy.size(); i++) {
    int num = lower_bound(v.begin(), v.end(), copy[i]) - v.begin();
    res.push_back(num);
  }
  return res;
}

int sum(int sx, int sy, int gx, int gy) {
  sx--, sy--;
  int res = mp[gx][gy];

  if (sx >= 0)
    res -= mp[sx][gy];

  if (sy >= 0)
    res -= mp[gx][sy];

  if (sx >= 0 && sy >= 0)
    res += mp[sx][sy];

  return res;
}

int main() {
  cin >> n >> m;
  x.resize(n), y.resize(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];
  vector<int> X, Y;
  X = compress(x), Y = compress(y);
  for (int i = 0; i < n; i++)
    mp[X[i]][Y[i]]++;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      mp[i][j + 1] += mp[i][j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      mp[j + 1][i] += mp[j][i];
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());
  while (m--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int sx = lower_bound(x.begin(), x.end(), a) - x.begin();
    int sy = lower_bound(y.begin(), y.end(), b) - y.begin();
    int gx = upper_bound(x.begin(), x.end(), c) - x.begin() - 1;
    int gy = upper_bound(y.begin(), y.end(), d) - y.begin() - 1;
    cout << sum(sx, sy, gx, gy) << endl;
  }
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int mp[5005][5005], n, m;
vector<int> x, y;
// O(n * log n ) n=v.size()
vector<int> compress(vector<int> v) {
  // v = {4,6,7,1,1,6}
  //   -> res={1,2,3,0,0,2}

  vector<int> copy = v;

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

  vector<int> res;
  for (int i = 0; i < (int)copy.size(); i++) {
    int num = lower_bound(v.begin(), v.end(), copy[i]) - v.begin();
    res.push_back(num);
  }
  return res;
}

int sum(int sx, int sy, int gx, int gy) {
  sx--, sy--;
  if (gx < 0 || gy < 0)
    return 0;
  int res = mp[gx][gy];

  if (sx >= 0)
    res -= mp[sx][gy];

  if (sy >= 0)
    res -= mp[gx][sy];

  if (sx >= 0 && sy >= 0)
    res += mp[sx][sy];

  return res;
}

int main() {
  cin >> n >> m;
  x.resize(n), y.resize(n);
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];
  vector<int> X, Y;
  X = compress(x), Y = compress(y);
  for (int i = 0; i < n; i++)
    mp[X[i]][Y[i]]++;
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      mp[i][j + 1] += mp[i][j];
  for (int i = 0; i < n; i++)
    for (int j = 0; j < n; j++)
      mp[j + 1][i] += mp[j][i];
  sort(x.begin(), x.end());
  sort(y.begin(), y.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  y.erase(unique(y.begin(), y.end()), y.end());
  while (m--) {
    int a, b, c, d;
    cin >> a >> b >> c >> d;
    int sx = lower_bound(x.begin(), x.end(), a) - x.begin();
    int sy = lower_bound(y.begin(), y.end(), b) - y.begin();
    int gx = upper_bound(x.begin(), x.end(), c) - x.begin() - 1;
    int gy = upper_bound(y.begin(), y.end(), d) - y.begin() - 1;
    cout << sum(sx, sy, gx, gy) << endl;
  }
  return 0;
}