Problem p01540 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
// #define int long long
using namespace std;
using pii = pair<int, int>;
#define REPF(i, a, n) for (int i = (a); i < (int)(n); ++i)
#define REP(i, n) REPF(i, 0, n)
#define ALL(c) begin(c), end(c)

int sum[5100][5100];
signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(n), y(n);
  vector<pii> p(n);
  REP(i, n) {
    cin >> p[i].first >> p[i].second;
    x[i] = p[i].first;
    y[i] = p[i].second;
  }
  sort(ALL(x));
  x.erase(unique(ALL(x)), end(x));
  sort(ALL(y));
  y.erase(unique(ALL(y)), end(y));

  auto get_x = [&](int i) {
    return distance(begin(x), lower_bound(ALL(x), i));
  };
  auto get_y = [&](int i) {
    return distance(begin(y), lower_bound(ALL(y), i));
  };

  REP(i, n) {
    int xx = get_x(p[i].first);
    int yy = get_y(p[i].second);
    sum[xx][yy]++;
  }
  REP(i, 5100) REPF(j, 1, 5100) sum[i][j] += sum[i][j - 1];
  REP(j, 5100) REPF(i, 1, 5100) sum[i][j] += sum[i - 1][j];

  REP(i, m) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int xx1 = get_x(x1);
    int yy1 = get_y(y1);
    int xx2 = get_x(x2);
    int yy2 = get_y(y2);
    if (find(ALL(x), x2) == end(x))
      xx2--;
    if (find(ALL(y), y2) == end(y))
      yy2--;

    if (xx2 < xx1 || yy2 < yy1) {
      cout << 0 << endl;
      continue;
    }

    int ans = sum[xx2][yy2];
    if (xx1 > 0) {
      ans -= sum[xx1 - 1][yy2];
    }
    if (yy1 > 0) {
      ans -= sum[xx2][yy1 - 1];
    }
    if (xx1 > 0 && yy1 > 0) {
      ans += sum[xx1 - 1][yy1 - 1];
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 127
/tmp/3bf3957e-6125-4fe3-bd70-cc95d876aa36.out: error while loading shared libraries: libstdc++.so.6: failed to map segment from shared object


Fixed submission:

#include <bits/stdc++.h>
// #define int long long
using namespace std;
using pii = pair<int, int>;
#define REPF(i, a, n) for (int i = (a); i < (int)(n); ++i)
#define REP(i, n) REPF(i, 0, n)
#define ALL(c) begin(c), end(c)

int sum[5100][5100];
signed main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(n), y(n);
  vector<pii> p(n);
  REP(i, n) {
    cin >> p[i].first >> p[i].second;
    x[i] = p[i].first;
    y[i] = p[i].second;
  }
  sort(ALL(x));
  x.erase(unique(ALL(x)), end(x));
  sort(ALL(y));
  y.erase(unique(ALL(y)), end(y));

  auto get_x = [&](int i) {
    return distance(begin(x), lower_bound(ALL(x), i));
  };
  auto get_y = [&](int i) {
    return distance(begin(y), lower_bound(ALL(y), i));
  };

  REP(i, n) {
    int xx = get_x(p[i].first);
    int yy = get_y(p[i].second);
    sum[xx][yy]++;
  }
  REP(i, 5100) REPF(j, 1, 5100) sum[i][j] += sum[i][j - 1];
  REP(j, 5100) REPF(i, 1, 5100) sum[i][j] += sum[i - 1][j];

  REP(i, m) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int xx1 = get_x(x1);
    int yy1 = get_y(y1);
    int xx2 = get_x(x2);
    int yy2 = get_y(y2);
    if (find(ALL(x), x2) == end(x))
      xx2--;
    if (find(ALL(y), y2) == end(y))
      yy2--;

    int ans = sum[xx2][yy2];
    if (xx1 > 0) {
      ans -= sum[xx1 - 1][yy2];
    }
    if (yy1 > 0) {
      ans -= sum[xx2][yy1 - 1];
    }
    if (xx1 > 0 && yy1 > 0) {
      ans += sum[xx1 - 1][yy1 - 1];
    }
    cout << ans << endl;
  }
  return 0;
}