Problem p01540 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define dump(o)                                                                \
  cerr << #o;                                                                  \
  for (auto e : (o))                                                           \
    cerr << " " << e;                                                          \
  cerr << endl;
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

// pair?????????
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

//??§?¨???§???
// v: ??§???????????§?¨??????? ????????????
template <typename T> void compress(vector<T> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}

//??§?¨???§???(map)
// v: ??§???????????§?¨??????? ????????????
//?????????: zip[??§???????????§?¨?]:??§???????????§?¨?
//(zip.size()<=10^5?????????????????????)
template <typename T> map<T, T> compressMap(vector<T> &v) {
  map<T, T> zip;
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (T i = 0; i < v.size(); i++)
    zip[v[i]] = i;
  return zip;
}

//??§???????????§?¨?
// index(??§???????????§?¨?, ??§???????????§?¨??????????(??????) )
template <typename T> int index(T i, vector<T> v) {
  return lower_bound(v.begin(), v.end(), i) - v.begin();
}

#define MAX 5001

// Sample2
// https://gyazo.com/39c67d1abe7a6a686acb52285f8a0bc9

int main() {
  // 2?¬?????´???????
  static int cumsum[MAX][MAX] = {};
  int xs[MAX], ys[MAX];
  int n, m;
  cin >> n >> m;
  vector<int> x(n + 1), y(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> xs[i] >> ys[i];
    x[i] = xs[i];
    y[i] = ys[i];
  }
  compress(x);
  compress(y);

  // map<int, int> zipx(compressMap(x));
  // map<int, int> zipy(compressMap(y));
  // dump(x);
  // dump(zipx);
  // dump(y);
  // dump(zipy);

  for (int i = 0; i < n; i++) {
    int vx = index(xs[i], x);
    int vy = index(ys[i], y);
    cumsum[vx + 1][vy + 1]++;
  }
  for (int vy = 0; vy < y.size(); vy++)
    for (int vx = 0; vx < x.size(); vx++)
      cumsum[vx + 1][vy + 1] +=
          cumsum[vx + 1][vy] + cumsum[vx][vy + 1] - cumsum[vx][vy];

  for (int i = 0; i < m; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1 = index(x1, x);
    x2 = index(x2 + 1, x);
    y1 = index(y1, y);
    y2 = index(y2 + 1, y);
    cout << cumsum[x2][y2] - cumsum[x2][y1] - cumsum[x1][y2] + cumsum[x1][y1]
         << endl;
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <limits>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define dump(o)                                                                \
  cerr << #o;                                                                  \
  for (auto e : (o))                                                           \
    cerr << " " << e;                                                          \
  cerr << endl;
#define MOD 1000000007
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL

// pair?????????
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  os << "(" << p.first << "," << p.second << ")";
  return os;
}

//??§?¨???§???
// v: ??§???????????§?¨??????? ????????????
template <typename T> void compress(vector<T> &v) {
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
}

//??§?¨???§???(map)
// v: ??§???????????§?¨??????? ????????????
//?????????: zip[??§???????????§?¨?]:??§???????????§?¨?
//(zip.size()<=10^5?????????????????????)
template <typename T> map<T, T> compressMap(vector<T> &v) {
  map<T, T> zip;
  sort(v.begin(), v.end());
  v.erase(unique(v.begin(), v.end()), v.end());
  for (T i = 0; i < v.size(); i++)
    zip[v[i]] = i;
  return zip;
}

//??§???????????§?¨?
// index(??§???????????§?¨?, ??§???????????§?¨??????????(??????) )
template <typename T> int index(T i, vector<T> v) {
  return lower_bound(v.begin(), v.end(), i) - v.begin();
}

#define MAX 5100

// Sample2
// https://gyazo.com/39c67d1abe7a6a686acb52285f8a0bc9

int main() {
  // 2?¬?????´???????
  static int cumsum[MAX][MAX] = {};
  int xs[MAX], ys[MAX];
  int n, m;
  cin >> n >> m;
  vector<int> x(n + 1), y(n + 1);
  for (int i = 0; i < n; i++) {
    cin >> xs[i] >> ys[i];
    x[i] = xs[i];
    y[i] = ys[i];
  }
  compress(x);
  compress(y);

  // map<int, int> zipx(compressMap(x));
  // map<int, int> zipy(compressMap(y));
  // dump(x);
  // dump(zipx);
  // dump(y);
  // dump(zipy);

  for (int i = 0; i < n; i++) {
    int vx = index(xs[i], x);
    int vy = index(ys[i], y);
    cumsum[vx + 1][vy + 1]++;
  }
  for (int vy = 0; vy < y.size(); vy++)
    for (int vx = 0; vx < x.size(); vx++)
      cumsum[vx + 1][vy + 1] +=
          cumsum[vx + 1][vy] + cumsum[vx][vy + 1] - cumsum[vx][vy];

  for (int i = 0; i < m; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1 = index(x1, x);
    x2 = index(x2 + 1, x);
    y1 = index(y1, y);
    y2 = index(y2 + 1, y);
    cout << cumsum[x2][y2] - cumsum[x2][y1] - cumsum[x1][y2] + cumsum[x1][y1]
         << endl;
  }
  return 0;
}