Problem p01540 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#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 fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
  os << "(" << p.fst << ", " << p.scd << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  rep(i, v.size()) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os;
}
template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;

int n, m;

int x[5005], y[5005];
vi compressed_x, compressed_y;

int a[5005][5005];

int main() {
  cin >> n >> m;

  rep(i, n) {
    cin >> x[i] >> y[i];
    compressed_x.PB(x[i]);
    compressed_y.PB(y[i]);
  }

  sort(all(compressed_x));
  sort(all(compressed_y));

  compressed_x.erase(unique(all(compressed_x)), end(compressed_x));
  compressed_y.erase(unique(all(compressed_y)), end(compressed_y));

  rep(i, n) {
    int xidx = lower_bound(all(compressed_x), x[i]) - begin(compressed_x);
    int yidx = lower_bound(all(compressed_y), y[i]) - begin(compressed_y);

    a[yidx][xidx]++;
  }

  for (int i = 0; i < compressed_y.size() + 1; ++i) {
    for (int j = 0; j < compressed_x.size() + 1; ++j) {
      if (i - 1 >= 0)
        a[i][j] += a[i - 1][j];
    }
  }

  for (int i = 0; i < compressed_x.size() + 1; ++i) {
    for (int j = 0; j < compressed_y.size() + 1; ++j) {
      if (i - 1 >= 0)
        a[j][i] += a[j][i - 1];
    }
  }

  compressed_x.PB(inf);
  compressed_y.PB(inf);

  rep(i, m) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;

    x1 = lower_bound(all(compressed_x), x1) - begin(compressed_x);
    x2 = upper_bound(all(compressed_x), x2) - begin(compressed_x) - 1;
    y1 = lower_bound(all(compressed_y), y1) - begin(compressed_y);
    y2 = upper_bound(all(compressed_y), y2) - begin(compressed_y) - 1;

    int ans = 0;
    ans += a[y2][x2];
    if (y1 - 1 >= 0)
      ans -= a[y1 - 1][x2];
    if (x1 - 1 >= 0)
      ans -= a[y2][x1 - 1];
    if (y1 - 1 >= 0 && x1 - 1 >= 0)
      ans += a[y1 - 1][x1 - 1];

    cout << ans << endl;
  }
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#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 fst first
#define scd second
#define PB push_back
#define MP make_pair
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define omajinai                                                               \
  ios::sync_with_stdio(false);                                                 \
  cin.tie(0)
#define rep(i, x) for (int i = 0; i < (int)(x); ++i)
#define rep1(i, x) for (int i = 1; i <= (int)(x); ++i)

using ll = long long;
using ld = long double;
using vi = vector<int>;
using vvi = vector<vi>;
using pii = pair<int, int>;
using vpii = vector<pii>;

template <class T, class U>
ostream &operator<<(ostream &os, const pair<T, U> p) {
  os << "(" << p.fst << ", " << p.scd << ")";
  return os;
}
template <class T> ostream &operator<<(ostream &os, const vector<T> v) {
  rep(i, v.size()) {
    if (i)
      os << ", ";
    os << v[i];
  }
  return os;
}
template <typename T> T &max(T &a, T &b) {
  if (a >= b)
    return a;
  return b;
}
template <typename T> T &min(T &a, T &b) {
  if (a < b)
    return a;
  return b;
}
template <typename T> bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <typename T> bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr int inf = 1e9;
constexpr ll linf = 3e18;
constexpr double eps = 1e-9;

int n, m;

int x[5005], y[5005];
vi compressed_x, compressed_y;

int a[5005][5005];

int main() {
  cin >> n >> m;

  rep(i, n) {
    cin >> x[i] >> y[i];
    compressed_x.PB(x[i]);
    compressed_y.PB(y[i]);
  }

  sort(all(compressed_x));
  sort(all(compressed_y));

  compressed_x.erase(unique(all(compressed_x)), end(compressed_x));
  compressed_y.erase(unique(all(compressed_y)), end(compressed_y));

  rep(i, n) {
    int xidx = lower_bound(all(compressed_x), x[i]) - begin(compressed_x);
    int yidx = lower_bound(all(compressed_y), y[i]) - begin(compressed_y);

    a[yidx][xidx]++;
  }

  for (int i = 0; i < compressed_y.size() + 1; ++i) {
    for (int j = 0; j < compressed_x.size() + 1; ++j) {
      if (i - 1 >= 0)
        a[i][j] += a[i - 1][j];
    }
  }

  for (int i = 0; i < compressed_x.size() + 1; ++i) {
    for (int j = 0; j < compressed_y.size() + 1; ++j) {
      if (i - 1 >= 0)
        a[j][i] += a[j][i - 1];
    }
  }

  compressed_x.PB(inf);
  compressed_y.PB(inf);

  rep(i, m) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;

    x1 = lower_bound(all(compressed_x), x1) - begin(compressed_x);
    x2 = upper_bound(all(compressed_x), x2) - begin(compressed_x) - 1;
    y1 = lower_bound(all(compressed_y), y1) - begin(compressed_y);
    y2 = upper_bound(all(compressed_y), y2) - begin(compressed_y) - 1;

    if (x2 < 0 || y2 < 0) {
      cout << 0 << endl;
      continue;
    }

    int ans = 0;
    ans += a[y2][x2];
    if (y1 - 1 >= 0)
      ans -= a[y1 - 1][x2];
    if (x1 - 1 >= 0)
      ans -= a[y2][x1 - 1];
    if (y1 - 1 >= 0 && x1 - 1 >= 0)
      ans += a[y1 - 1][x1 - 1];

    cout << ans << endl;
  }
}