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

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

map<int, int> mx, my;
vector<int> vx, vy;
int N, M, x[5010], y[5010];
int board[5010][5010];

signed main() {
  cin >> N >> M;
  rep(i, 0, N) {
    cin >> x[i] >> y[i];
    if (!mx.count(x[i]))
      vx.push_back(x[i]);
    if (!my.count(y[i]))
      vy.push_back(y[i]);
    mx[x[i]];
    my[y[i]];
  }
  sort(vx.begin(), vx.end());
  sort(vy.begin(), vy.end());
  vx.push_back(INF);
  vy.push_back(INF);

  // compression
  int k = 1;
  for (auto &e : mx) {
    e.second = k++;
    // printf("mx: %lld -> %lld\\n", e.first, e.second);
  }
  k = 1;
  for (auto &e : my) {
    e.second = k++;
    // printf("my: %lld -> %lld\\n", e.first, e.second);
  }

  // accumulate
  rep(i, 0, N) {
    // printf("treasure exist: (%lld, %lld)\\n", mx[x[i]], my[y[i]]);
    board[mx[x[i]]][my[y[i]]]++;
  }
  rep(i, 0, N + 1) rep(j, 0, N + 1) board[i + 1][j + 1] += board[i + 1][j];
  rep(j, 0, N + 1) rep(i, 0, N + 1) board[i + 1][j + 1] += board[i][j + 1];

  // solve
  rep(i, 0, M) {
    int xl, yl, xr, yr;
    cin >> xl >> yl >> xr >> yr;

    // 1-indexed
    xl = lower_bound(vx.begin(), vx.end(), xl) - vx.begin();
    yl = lower_bound(vy.begin(), vy.end(), yl) - vy.begin();
    xr = upper_bound(vx.begin(), vx.end(), xr) - vx.begin();
    yr = upper_bound(vy.begin(), vy.end(), yr) - vy.begin();

    // printf("xl = %lld, yl = %lld, xr = %lld, yr = %lld\\n", xl, yl, xr, yr);

    int ans = board[xr][yr] - board[xr][yl] - board[xl][yr] + board[xl][yl];
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cfloat>
#include <climits>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, a, n) for (int(i) = (a); (i) < (n); (i)++)
#define repq(i, a, n) for (int(i) = (a); (i) <= (n); (i)++)
#define repr(i, a, n) for (int(i) = (a); (i) >= (n); (i)--)
#define int long long int

template <typename T> void chmax(T &a, T b) { a = max(a, b); }
template <typename T> void chmin(T &a, T b) { a = min(a, b); }
template <typename T> void chadd(T &a, T b) { a = a + b; }

typedef pair<int, int> pii;
typedef long long ll;

int dx[] = {0, 0, 1, -1};
int dy[] = {1, -1, 0, 0};
constexpr ll INF = 1001001001001001LL;
constexpr ll MOD = 1000000007LL;

map<int, int> mx, my;
vector<int> vx, vy;
int N, M, x[1010], y[1010];
int board[1010][1010];

signed main() {
  cin >> N >> M;
  rep(i, 0, N) {
    cin >> x[i] >> y[i];
    if (!mx.count(x[i]))
      vx.push_back(x[i]);
    if (!my.count(y[i]))
      vy.push_back(y[i]);
    mx[x[i]];
    my[y[i]];
  }
  sort(vx.begin(), vx.end());
  sort(vy.begin(), vy.end());
  vx.push_back(INF);
  vy.push_back(INF);

  // compression
  int k = 1;
  for (auto &e : mx) {
    e.second = k++;
    // printf("mx: %lld -> %lld\\n", e.first, e.second);
  }
  k = 1;
  for (auto &e : my) {
    e.second = k++;
    // printf("my: %lld -> %lld\\n", e.first, e.second);
  }

  // accumulate
  rep(i, 0, N) {
    // printf("treasure exist: (%lld, %lld)\\n", mx[x[i]], my[y[i]]);
    board[mx[x[i]]][my[y[i]]]++;
  }
  rep(i, 0, N + 1) rep(j, 0, N + 1) board[i + 1][j + 1] += board[i + 1][j];
  rep(j, 0, N + 1) rep(i, 0, N + 1) board[i + 1][j + 1] += board[i][j + 1];

  // solve
  rep(i, 0, M) {
    int xl, yl, xr, yr;
    cin >> xl >> yl >> xr >> yr;

    // 1-indexed
    xl = lower_bound(vx.begin(), vx.end(), xl) - vx.begin();
    yl = lower_bound(vy.begin(), vy.end(), yl) - vy.begin();
    xr = upper_bound(vx.begin(), vx.end(), xr) - vx.begin();
    yr = upper_bound(vy.begin(), vy.end(), yr) - vy.begin();

    // printf("xl = %lld, yl = %lld, xr = %lld, yr = %lld\\n", xl, yl, xr, yr);

    int ans = board[xr][yr] - board[xr][yl] - board[xl][yr] + board[xl][yl];
    cout << ans << endl;
  }
  return 0;
}