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

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--)
#define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
template <class T, class S> inline pair<T, S> mp(const T x, const S y) {
  return make_pair(x, y);
}
template <class T, class S> inline bool minup(T &m, const S x) {
  return m > (T)x ? (m = (T)x, true) : false;
}
template <class T, class S> inline bool maxup(T &m, const S x) {
  return m < (T)x ? (m = (T)x, true) : false;
}

static const int INF = 2000000000;
static const ll MOD = 1000000007LL;
static const double EPS = 1E-10;

int n, m;
int sx, sy;
int tx, ty;
int vx[5555];
int vy[5555];
int maze[5555][5555];

int main() {
  cin >> n >> m;
  rep(i, n) cin >> vx[i] >> vy[i];

  vi x, y;
  rep(i, n) {
    x.pb(vx[i]);
    y.pb(vy[i]);
  }
  sort(ALL(x));
  x.erase(unique(ALL(x)), x.end());
  sort(ALL(y));
  y.erase(unique(ALL(y)), y.end());
  rep(i, n) {
    vx[i] = lower_bound(ALL(x), vx[i]) - x.begin();
    vy[i] = lower_bound(ALL(y), vy[i]) - y.begin();
  }
  rep(i, n) maze[vx[i] + 1][vy[i] + 1] += 1;
  rep(i, n + 1) rep(j, n) maze[i][j + 1] += maze[i][j];
  rep(i, n) rep(j, n + 1) maze[i + 1][j] += maze[i][j];

  rep(i, m) {
    cin >> sx >> sy >> tx >> ty;
    tx += 1;
    ty += 1;
    ll ret = 0;
    int lx = lower_bound(ALL(x), tx) - x.begin();
    int ly = lower_bound(ALL(y), ty) - y.begin();
    int rx = lower_bound(ALL(x), sx) - x.begin();
    int ry = lower_bound(ALL(y), sy) - y.begin();

    ret += maze[lx][ly];
    ret += maze[rx][ry];
    ret -= maze[rx][ly];
    ret -= maze[lx][ry];

    cout << ret << endl;
  }

  return 0;
}

Error: -11

Fixed submission:

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

typedef long long int ll;
typedef pair<int, int> pii;
typedef vector<int> vi;
typedef vector<pair<int, int>> vii;
#define rrep(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define erep(i, m, n) for (int(i) = (m); (i) <= (n); (i)++)
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define rrev(i, m, n) for (int(i) = (n)-1; (i) >= (m); (i)--)
#define erev(i, m, n) for (int(i) = (n); (i) >= (m); (i)--)
#define rev(i, n) for (int(i) = (n)-1; (i) >= 0; (i)--)
#define vrep(i, c)                                                             \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(v) (v).begin(), (v).end()
#define pb push_back
template <class T, class S> inline pair<T, S> mp(const T x, const S y) {
  return make_pair(x, y);
}
template <class T, class S> inline bool minup(T &m, const S x) {
  return m > (T)x ? (m = (T)x, true) : false;
}
template <class T, class S> inline bool maxup(T &m, const S x) {
  return m < (T)x ? (m = (T)x, true) : false;
}

static const int INF = 2000000000;
static const ll MOD = 1000000007LL;
static const double EPS = 1E-10;

int n, m;
int sx[55555], sy[55555];
int tx[55555], ty[55555];
int vx[5555];
int vy[5555];
int maze[5555][5555];

int main() {
  cin >> n >> m;
  rep(i, n) cin >> vx[i] >> vy[i];
  rep(i, m) cin >> sx[i] >> sy[i] >> tx[i] >> ty[i];

  vi x, y;
  rep(i, n) {
    x.pb(vx[i]);
    y.pb(vy[i]);
  }
  x.pb(INF);
  y.pb(INF);
  sort(ALL(x));
  x.erase(unique(ALL(x)), x.end());
  sort(ALL(y));
  y.erase(unique(ALL(y)), y.end());
  rep(i, n) {
    vx[i] = lower_bound(ALL(x), vx[i]) - x.begin();
    vy[i] = lower_bound(ALL(y), vy[i]) - y.begin();
  }
  rep(i, n) maze[vx[i]][vy[i]] += 1;
  rep(i, n) rep(j, n - 1) maze[i][j + 1] += maze[i][j];
  rep(i, n - 1) rep(j, n) maze[i + 1][j] += maze[i][j];

  rep(i, m) {
    int ret = 0;
    int lx = upper_bound(ALL(x), tx[i]) - x.begin() - 1;
    int ly = upper_bound(ALL(y), ty[i]) - y.begin() - 1;
    int rx = lower_bound(ALL(x), sx[i]) - x.begin();
    int ry = lower_bound(ALL(y), sy[i]) - y.begin();

    ret += maze[lx][ly];
    ret += maze[rx - 1][ry - 1];
    ret -= maze[rx - 1][ly];
    ret -= maze[lx][ry - 1];

    cout << ret << endl;
  }

  return 0;
}