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

#include <bits/stdc++.h>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second

typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;

map<int, int> mx, my;

int s[5010][5010];

int get(int x1, int y1, int x2, int y2) {
  return s[y2][x2] - s[y1][x2] - s[y2][x1] + s[y1][x1];
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(n), y(n);
  rep(i, n) {
    cin >> x[i] >> y[i];
    mx[x[i]]++;
    my[y[i]]++;
  }
  int cnt_x = 0, cnt_y = 0;
  for (auto &p : mx)
    p.se = ++cnt_x;
  for (auto &p : my)
    p.se = ++cnt_y;
  for (int i = 0; i < n; i++)
    s[my[y[i]]][mx[x[i]]]++;
  for (int i = 0; i < 5010; i++)
    for (int j = 1; j < 5010; j++)
      s[i][j] += s[i][j - 1];
  for (int i = 1; i < 5010; i++)
    for (int j = 0; j < 5010; j++)
      s[i][j] += s[i - 1][j];

  rep(i, m) {
    int qx1, qy1, qx2, qy2;
    cin >> qx1 >> qy1 >> qx2 >> qy2;
    int x1, x2, y1, y2;
    if (mx.lower_bound(qx1) == mx.end() || my.lower_bound(qy1) == my.end()) {
      cout << 0 << endl;
      continue;
    }
    x1 = (mx.lower_bound(qx1))->se - 1;
    y1 = (my.lower_bound(qy1))->se - 1;
    x2 = (--mx.upper_bound(qx2))->se;
    y2 = (--my.upper_bound(qy2))->se;
    cout << get(x1, y1, x2, y2) << endl;
  }
}

Error: 127
/tmp/a155d434-1f87-4604-8781-29c9fa79978d.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object


Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

#define rep(i, n) REP(i, 0, n)
#define REP(i, s, e) for (int i = (s); i < (int)(e); i++)
#define pb push_back
#define mp make_pair
#define all(r) (r).begin(), (r).end()
#define rall(r) (r).rbegin(), (r).rend()
#define fi first
#define se second
#define println(X) cout << X << endl;
#define DBG(X) cout << #X << " : " << X << endl;

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef vector<ll> vl;
typedef vector<vl> vll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;

const int INF = 1e9;
const ll LINF = 1e18;
const ll MOD = 1e9 + 7;
double EPS = 1e-8;
const double PI = acos(-1);

int d[5010][5010];

int f(int x1, int y1, int x2, int y2) {
  return d[x2][y2] - d[x1][y2] - d[x2][y1] + d[x1][y1];
}

int main() {
  int n, m;
  cin >> n >> m;
  map<int, int> mx, my;
  vector<pii> v(n), s(m), t(m);
  rep(i, n) {
    cin >> v[i].fi >> v[i].se;
    mx[v[i].fi]++;
    my[v[i].se]++;
  }
  rep(i, m) {
    cin >> s[i].fi >> s[i].se >> t[i].fi >> t[i].se;
    mx[s[i].fi]++;
    my[s[i].se]++;
    mx[t[i].fi]++;
    my[t[i].se]++;
  }
  int cnt = 0;
  for (auto &p : mx)
    p.se = ++cnt;
  cnt = 0;
  for (auto &p : my)
    p.se = ++cnt;

  rep(i, n) d[mx[v[i].fi]][my[v[i].se]]++;

  // rep(i, 20){
  // 	rep(j, 20) printf("%3d", d[i][j]);
  // 	printf("\\n");
  // }

  for (int i = 0; i < 5010; i++) {
    for (int j = 0; j < 5009; j++) {
      d[i][j + 1] += d[i][j];
    }
  }
  for (int i = 0; i < 5009; i++) {
    for (int j = 0; j < 5010; j++) {
      d[i + 1][j] += d[i][j];
    }
  }
  rep(i, m) {
    cout << f(mx[s[i].fi] - 1, my[s[i].se] - 1, mx[t[i].fi], my[t[i].se])
         << endl;
  }
}