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

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

// 領域を増やすプロ

int main() {

  int n, m;
  cin >> n >> m;
  vector<int> X;
  vector<int> Y;
  vector<int> x(n);
  vector<int> y(n);

  // 点の追加
  for (int i = 0; i < n; i++) {
    int x1, y1;
    cin >> x1 >> y1;
    x[i] = x1;
    y[i] = y1;

    X.push_back(x1);
    Y.push_back(y1);
    // X.push_back(x2);
    // Y.push_back(y2);
  }

  // 座標の始点、終点を追加
  // X.push_back(0);
  // Y.push_back(0);
  // X.push_back(w);
  // Y.push_back(h);

  // 定石
  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  // X = {0, 1, 1, 4, 4, 6, 9, 9, 10, 10, 10, 10}
  // Y = {0, 1, 1, 4, 4, 5, 6, 8, 8, 10, 10, 10}

  X.erase(unique(X.begin(), X.end()), X.end());
  Y.erase(unique(Y.begin(), Y.end()), Y.end());
  // X = {0, 1, 4, 6, 9, 10}
  // Y = {0, 1, 4, 5, 6, 8, 10}

  // cout << endl;
  int h = 0, w = 0;
  // cout << "x1 y2" << endl;
  for (int i = 0; i < n; i++) {

    x[i] = lower_bound(X.begin(), X.end(), x[i]) - X.begin();
    w = max(w, x[i]);
    // int x2 = lower_bound(X.begin(),X.end(),xe[i]) - X.begin();

    y[i] = lower_bound(Y.begin(), Y.end(), y[i]) - Y.begin();
    h = max(h, y[i]);
    // int y2 = lower_bound(Y.begin(),Y.end(),ye[i]) - Y.begin();

    // cout <<"(" << x[i] << " " << y[i] << ")" << endl;
  }

  // cout << h << " " << w << endl;
  vector<vector<int>> imos(h + 2, vector<int>(w + 2, 0));
  for (int i = 0; i < n; i++) {
    // cout << x[i] << "  " << y[i] << endl;
    imos[y[i] + 1][x[i] + 1]++;
  }

  for (int j = 0; j <= w + 1; j++) {
    for (int i = 1; i <= h + 1; i++) {
      imos[i][j] += imos[i - 1][j];
    }
  }

  for (int i = 0; i <= h + 1; i++) {
    for (int j = 1; j <= w + 1; j++) {
      imos[i][j] += imos[i][j - 1];
    }
  }

  /*for(int i = 0; i < X.size(); i++){
      cout << X[i] << endl;
  }*/

  for (int i = 0; i < m; i++) {
    int x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1 = lower_bound(X.begin(), X.end(), x1) - X.begin() + 1;
    x2 = upper_bound(X.begin(), X.end(), x2) - X.begin();

    y1 = lower_bound(Y.begin(), Y.end(), y1) - Y.begin() + 1;
    y2 = upper_bound(Y.begin(), Y.end(), y2) - Y.begin();
    // if(x2 == -1) x2 = 0;
    // if(y2 == -1) y2 = 0;
    // cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl;
    int sum = imos[y2][x2];
    if (x1 > 0)
      sum -= imos[y2][x1 - 1];
    if (y1 > 0)
      sum -= imos[y1 - 1][x2];
    if (x1 > 0 && y1 > 0)
      sum += imos[y1 - 1][x1 - 1];
    cout << sum << endl;
  }
  return 0;
}


Error: 0

Fixed submission:

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

// 領域を増やすプロ

int main() {

  int n, m;
  cin >> n >> m;
  vector<long long> X;
  vector<long long> Y;
  vector<long long> x(n);
  vector<long long> y(n);

  // 点の追加
  for (int i = 0; i < n; i++) {
    long long x1, y1;
    cin >> x1 >> y1;
    x[i] = x1;
    y[i] = y1;

    X.push_back(x1);
    Y.push_back(y1);
    // X.push_back(x2);
    // Y.push_back(y2);
  }

  // 座標の始点、終点を追加
  // X.push_back(0);
  // Y.push_back(0);
  // X.push_back(w);
  // Y.push_back(h);

  // 定石
  sort(X.begin(), X.end());
  sort(Y.begin(), Y.end());
  // X = {0, 1, 1, 4, 4, 6, 9, 9, 10, 10, 10, 10}
  // Y = {0, 1, 1, 4, 4, 5, 6, 8, 8, 10, 10, 10}

  X.erase(unique(X.begin(), X.end()), X.end());
  Y.erase(unique(Y.begin(), Y.end()), Y.end());
  // X = {0, 1, 4, 6, 9, 10}
  // Y = {0, 1, 4, 5, 6, 8, 10}

  // cout << endl;
  long long h = 0, w = 0;
  // cout << "x1 y2" << endl;
  for (int i = 0; i < n; i++) {

    x[i] = lower_bound(X.begin(), X.end(), x[i]) - X.begin();
    w = max(w, x[i]);
    // int x2 = lower_bound(X.begin(),X.end(),xe[i]) - X.begin();

    y[i] = lower_bound(Y.begin(), Y.end(), y[i]) - Y.begin();
    h = max(h, y[i]);
    // int y2 = lower_bound(Y.begin(),Y.end(),ye[i]) - Y.begin();

    // cout <<"(" << x[i] << " " << y[i] << ")" << endl;
  }

  // cout << h << " " << w << endl;
  vector<vector<long long>> imos(h + 1, vector<long long>(w + 1, 0));
  for (int i = 0; i < n; i++) {
    // cout << x[i] << "  " << y[i] << endl;
    imos[y[i]][x[i]]++;
  }

  for (int j = 0; j <= w; j++) {
    for (int i = 1; i <= h; i++) {
      imos[i][j] += imos[i - 1][j];
    }
  }

  for (int i = 0; i <= h; i++) {
    for (int j = 1; j <= w; j++) {
      imos[i][j] += imos[i][j - 1];
    }
  }

  for (int i = 0; i < m; i++) {
    long long x1, x2, y1, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    x1 = lower_bound(X.begin(), X.end(), x1) - X.begin();
    x2 = upper_bound(X.begin(), X.end(), x2) - X.begin() - 1;

    y1 = lower_bound(Y.begin(), Y.end(), y1) - Y.begin();
    y2 = upper_bound(Y.begin(), Y.end(), y2) - Y.begin() - 1;

    // cout << x1 << " " << y1 << " " << x2 << " " << y2 << endl;
    long long sum = imos[y2][x2];
    if (x1 > 0)
      sum -= imos[y2][x1 - 1];
    if (y1 > 0)
      sum -= imos[y1 - 1][x2];
    if (x1 > 0 && y1 > 0)
      sum += imos[y1 - 1][x1 - 1];
    cout << sum << endl;
  }
  return 0;
}