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

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
int main() {
  int n, m;
  cin >> n >> m;

  vector<int> A(n * n, 0);
  vector<int> x(n);
  vector<int> y(n);
  vector<int> xb(m);
  vector<int> yb(m);
  vector<int> xe(m);
  vector<int> ye(m);
  vector<int> xs;
  vector<int> ys;

  for (int j = 0; j < n; j++)
    cin >> x[j] >> y[j];

  for (int i = 0; i < m; i++)
    cin >> xb[i] >> yb[i] >> xe[i] >> ye[i];

  xs = x;
  ys = y;
  sort(xs.begin(), xs.end());
  sort(ys.begin(), ys.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  ys.erase(unique(ys.begin(), ys.end()), ys.end());

  for (int j = 0; j < n; j++) {
    x[j] = find(xs.begin(), xs.end(), x[j]) - xs.begin();
    y[j] = find(ys.begin(), ys.end(), y[j]) - ys.begin();
  }

  for (int i = 0; i < m; i++) {
    xb[i] = lower_bound(xs.begin(), xs.end(), xb[i]) - xs.begin();
    yb[i] = lower_bound(ys.begin(), ys.end(), yb[i]) - ys.begin();
    xe[i] = upper_bound(xs.begin(), xs.end(), xe[i]) - xs.begin() - 1;
    ye[i] = upper_bound(ys.begin(), ys.end(), ye[i]) - ys.begin() - 1;
  }

  for (int j = 0; j < n; j++) {
    A[n * y[j] + x[j]] += 1;
  }

  for (int j = 0; j < n; j++) {
    for (int k = 1; k < n; k++) {
      A[n * j + k] += A[n * j + k - 1];
    }
  }
  for (int k = 0; k < n; k++) {
    for (int j = 1; j < n; j++) {
      A[n * j + k] += A[n * (j - 1) + k];
    }
  }

  for (int i = 0; i < m; i++) {
    if (xe[i] < 0 || ye[i] < 0 || xb[i] == (int)xs.size() ||
        yb[i] == (int)ys.size()) {
      cout << 0 << endl;
      continue;
    }
    int a, b, c;
    if (xb[i] > 0 && yb[i] > 0)
      c = A[n * (yb[i] - 1) + (xb[i] - 1)];
    else
      c = 0;
    if (xb[i] > 0)
      a = A[n * ye[i] + (xb[i] - 1)];
    else
      a = 0;
    if (yb[i] > 0)
      b = A[n * (yb[i] - 1) + xe[i]];
    else
      b = 0;
    cout << A[n * ye[i] + xe[i]] - a - b + c << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>

using namespace std;
int main() {
  int n, m;
  cin >> n >> m;

  vector<int> A(n * n, 0);
  vector<int> x(n);
  vector<int> y(n);
  vector<int> xb(m);
  vector<int> yb(m);
  vector<int> xe(m);
  vector<int> ye(m);
  vector<int> xs;
  vector<int> ys;

  for (int j = 0; j < n; j++)
    cin >> x[j] >> y[j];

  for (int i = 0; i < m; i++)
    cin >> xb[i] >> yb[i] >> xe[i] >> ye[i];

  xs = x;
  ys = y;
  sort(xs.begin(), xs.end());
  sort(ys.begin(), ys.end());
  xs.erase(unique(xs.begin(), xs.end()), xs.end());
  ys.erase(unique(ys.begin(), ys.end()), ys.end());

  for (int j = 0; j < n; j++) {
    x[j] = find(xs.begin(), xs.end(), x[j]) - xs.begin();
    y[j] = find(ys.begin(), ys.end(), y[j]) - ys.begin();
  }

  for (int i = 0; i < m; i++) {
    xb[i] = lower_bound(xs.begin(), xs.end(), xb[i]) - xs.begin();
    yb[i] = lower_bound(ys.begin(), ys.end(), yb[i]) - ys.begin();
    xe[i] = upper_bound(xs.begin(), xs.end(), xe[i]) - xs.begin() - 1;
    ye[i] = upper_bound(ys.begin(), ys.end(), ye[i]) - ys.begin() - 1;
  }

  for (int j = 0; j < n; j++) {
    A[n * y[j] + x[j]] += 1;
  }

  for (int j = 0; j < n; j++) {
    for (int k = 1; k < n; k++) {
      A[n * j + k] += A[n * j + k - 1];
    }
  }
  for (int k = 0; k < n; k++) {
    for (int j = 1; j < n; j++) {
      A[n * j + k] += A[n * (j - 1) + k];
    }
  }

  for (int i = 0; i < m; i++) {
    int a, b, c;
    c = A[n * (yb[i] - 1) + (xb[i] - 1)];
    if (xb[i] > 0)
      a = A[n * ye[i] + (xb[i] - 1)];
    else
      c = a = 0;
    if (yb[i] > 0)
      b = A[n * (yb[i] - 1) + xe[i]];
    else
      c = b = 0;
    cout << A[n * ye[i] + xe[i]] - a - b + c << endl;
  }
}