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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int lb(vector<int> &Z, int z) {
  return lower_bound(Z.begin(), Z.end(), z) - Z.begin();
}

int ub(vector<int> &Z, int z) {
  return upper_bound(Z.begin(), Z.end(), z) - Z.begin();
}

int compress(vector<int> &z, vector<int> &z1, vector<int> &z2) {
  int n = z.size(), m = z1.size();
  vector<int> Z;
  for (int i = 0; i < n; i++)
    Z.push_back(z[i]);
  sort(Z.begin(), Z.end());
  Z.erase(unique(Z.begin(), Z.end()), Z.end());
  for (int i = 0; i < n; i++)
    z[i] = lb(Z, z[i]);
  for (int j = 0; j < m; j++) {
    z1[j] = lb(Z, z1[j]);
    z2[j] = ub(Z, z2[j]) - 1;
  }
  return Z.size();
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(n), y(n);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &x[i], &y[i]);
  vector<int> x1(m), y1(m), x2(m), y2(m);
  for (int j = 0; j < m; j++)
    scanf("%d%d%d%d", &x1[j], &y1[j], &x2[j], &y2[j]);
  int w = compress(x, x1, x2), h = compress(y, y1, y2);
  vector<vector<int>> sum(h + 1, vector<int>(w + 1));
  for (int i = 0; i < n; i++)
    sum[y[i] + 1][x[i] + 1]++;
  for (int y = 1; y <= h; y++)
    for (int x = 1; x <= w; x++)
      sum[y][x] += sum[y][x - 1] + sum[y - 1][x] - sum[y - 1][x - 1];
  for (int j = 0; j < m; j++) {
    int l = x1[j], u = y1[j], r = x2[j] + 1, d = y2[j] + 1;
    printf("%d\\n", sum[d][r] - sum[d][l] - sum[u][r] + sum[u][l]);
  }
}

Error: 0

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cfloat>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <time.h>
#include <vector>
using namespace std;

typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> i_i;
typedef pair<ll, int> ll_i;
typedef pair<double, int> d_i;
typedef pair<ll, ll> ll_ll;
typedef pair<double, double> d_d;
struct edge {
  int u, v;
  ll w;
};

ll MOD = 1000000007;
ll _MOD = 1000000009;
double EPS = 1e-10;

int foo(vector<int> &Z, int z) {
  return lower_bound(Z.begin(), Z.end(), z) - Z.begin();
}

int compress(vector<int> &z, vector<int> &z1, vector<int> &z2) {
  int n = z.size(), m = z1.size();
  vector<int> Z;
  for (int i = 0; i < n; i++)
    Z.push_back(z[i]);
  for (int j = 0; j < m; j++) {
    Z.push_back(z1[j]);
    Z.push_back(z2[j]);
  }
  sort(Z.begin(), Z.end());
  Z.erase(unique(Z.begin(), Z.end()), Z.end());
  for (int i = 0; i < n; i++)
    z[i] = foo(Z, z[i]);
  for (int j = 0; j < m; j++) {
    z1[j] = foo(Z, z1[j]);
    z2[j] = foo(Z, z2[j]);
  }
  return Z.size();
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(n), y(n);
  for (int i = 0; i < n; i++)
    scanf("%d%d", &x[i], &y[i]);
  vector<int> x1(m), y1(m), x2(m), y2(m);
  for (int j = 0; j < m; j++)
    scanf("%d%d%d%d", &x1[j], &y1[j], &x2[j], &y2[j]);
  int w = compress(x, x1, x2), h = compress(y, y1, y2);
  vector<vector<int>> sum(h + 1, vector<int>(w + 1));
  for (int i = 0; i < n; i++)
    sum[y[i] + 1][x[i] + 1]++;
  for (int y = 1; y <= h; y++)
    for (int x = 1; x <= w; x++)
      sum[y][x] += sum[y][x - 1] + sum[y - 1][x] - sum[y - 1][x - 1];
  for (int j = 0; j < m; j++) {
    int l = x1[j], u = y1[j], r = x2[j] + 1, d = y2[j] + 1;
    printf("%d\\n", sum[d][r] - sum[d][l] - sum[u][r] + sum[u][l]);
  }
}