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

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
int dp[5000][5000] = {};
int tx[5000];
int ty[5000];
int s[500000][4];
int main() {
  int n, m;
  cin >> n >> m;
  vector<int> xs;
  vector<int> ys;
  rep(i, n) {
    cin >> tx[i] >> ty[i];
    xs.push_back(tx[i]);
    ys.push_back(ty[i]);
  }
  sort(xs.begin(), xs.end());
  sort(ys.begin(), ys.end());
  rep(i, m) {
    rep(j, 4) { cin >> s[i][j]; }
    s[i][0] = lower_bound(xs.begin(), xs.end(), s[i][0]) - xs.begin();
    s[i][1] = lower_bound(ys.begin(), ys.end(), s[i][1]) - ys.begin();
    s[i][2] = upper_bound(xs.begin(), xs.end(), s[i][2]) - xs.begin();
    s[i][2]--;
    s[i][3] = upper_bound(ys.begin(), ys.end(), s[i][3]) - ys.begin();
    s[i][3]--;
  }
  rep(i, n) {
    tx[i] = find(xs.begin(), xs.end(), tx[i]) - xs.begin();
    ty[i] = find(ys.begin(), ys.end(), ty[i]) - ys.begin();
  }
  rep(i, n) { dp[tx[i]][ty[i]]++; }
  rep(i, n) {
    rep(j, n) {
      if (i == 0 && j == 0) {
        dp[0][0] = dp[0][0];
      } else if (i == 0) {
        dp[0][j] += dp[0][j - 1];
      } else if (j == 0) {
        dp[i][0] += dp[i - 1][0];
      } else {
        dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
      }
    }
  }
  rep(i, m) {
    int sx = s[i][0];
    int sy = s[i][1];
    int gx = s[i][2];
    int gy = s[i][3];
    int cnt;
    if (sx >= n || sy >= n || gx < 0 || gy < 0) {
      cnt = 0;
    } else if (sx > 0 && sy > 0) {
      cnt = dp[gx][gy] - dp[sx - 1][gy] - dp[gx][sy - 1] + dp[sx - 1][sy - 1];
    } else if (sx > 0) {
      cnt = dp[gx][gy] - dp[sx - 1][gy];
    } else if (sy > 0) {
      cnt = dp[gx][gy] - dp[gx][sy - 1];
    } else {
      cnt = dp[gx][gy];
    }
    cout << cnt << endl;
  }
  return 0;
}


Error: -11

Fixed submission:

#include <algorithm>
#include <ciso646>
#include <cmath>
#include <complex>
#include <cstdio>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <queue>
#include <random>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef long long ll;
const ll MOD = 1000000007;
const ll INF = (ll)1000000007 * 1000000007;
const double EPS = 1e-9;
typedef pair<int, int> P;
typedef unsigned int ui;
#define stop                                                                   \\
  char nyaa;                                                                   \\
  cin >> nyaa;
#define rep(i, n) for (int i = 0; i < n; i++)
#define per(i, n) for (int i = n - 1; i >= 0; i--)
#define Rep(i, sta, n) for (int i = sta; i < n; i++)
#define rep1(i, n) for (int i = 1; i <= n; i++)
#define per1(i, n) for (int i = n; i >= 1; i--)
#define Rep1(i, sta, n) for (int i = sta; i <= n; i++)
typedef long double ld;
typedef complex<ld> Point;
const ld eps = 1e-11;
const ld pi = acos(-1.0);
int main() {
  int n, m;
  cin >> n >> m;
  int tx[5000];
  int ty[5000];
  int s[500000][4];
  vector<int> xs;
  vector<int> ys;
  rep(i, n) {
    cin >> tx[i] >> ty[i];
    xs.push_back(tx[i]);
    ys.push_back(ty[i]);
  }
  sort(xs.begin(), xs.end());
  sort(ys.begin(), ys.end());
  rep(i, m) {
    rep(j, 4) { cin >> s[i][j]; }
    s[i][0] = lower_bound(xs.begin(), xs.end(), s[i][0]) - xs.begin();
    s[i][1] = lower_bound(ys.begin(), ys.end(), s[i][1]) - ys.begin();
    s[i][2] = upper_bound(xs.begin(), xs.end(), s[i][2]) - xs.begin();
    s[i][2]--;
    s[i][3] = upper_bound(ys.begin(), ys.end(), s[i][3]) - ys.begin();
    s[i][3]--;
  }
  rep(i, n) {
    tx[i] = find(xs.begin(), xs.end(), tx[i]) - xs.begin();
    ty[i] = find(ys.begin(), ys.end(), ty[i]) - ys.begin();
  }
  int dp[500][500] = {};
  rep(i, n) { dp[tx[i]][ty[i]]++; }
  rep(i, n) {
    rep(j, n) {
      if (i == 0 && j == 0) {
        dp[0][0] = dp[0][0];
      } else if (i == 0) {
        dp[0][j] += dp[0][j - 1];
      } else if (j == 0) {
        dp[i][0] += dp[i - 1][0];
      } else {
        dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
      }
    }
  }
  rep(i, m) {
    int sx = s[i][0];
    int sy = s[i][1];
    int gx = s[i][2];
    int gy = s[i][3];
    int cnt;
    if (sx >= n || sy >= n || gx < 0 || gy < 0) {
      cnt = 0;
    } else if (sx > 0 && sy > 0) {
      cnt = dp[gx][gy] - dp[sx - 1][gy] - dp[gx][sy - 1] + dp[sx - 1][sy - 1];
    } else if (sx > 0) {
      cnt = dp[gx][gy] - dp[sx - 1][gy];
    } else if (sy > 0) {
      cnt = dp[gx][gy] - dp[gx][sy - 1];
    } else {
      cnt = dp[gx][gy];
    }
    cout << cnt << endl;
  }
  return 0;
}