alexjercan/bugnet huggingface dataset:
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> pii(n);

  for (int i = 0; i < n; i++)
    cin >> pii[i].first >> pii[i].second;
  sort(pii.begin(), pii.end());
  for (int i = 0; i < m; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int cnt = 0;
    for (int j = 0; j < n; j++) {
      if (x1 <= pii[j].first && pii[j].first <= x2 && y1 <= pii[j].second &&
          pii[j].second <= y2)
        cnt++;
    }
    printf("%d\\n", cnt);
  }
  // cout<<ans<<endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
#include <utility>
#include <vector>
#define F first
#define S second
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> pii(n);
  for (int i = 0; i < n; i++)
    cin >> pii[i].first >> pii[i].second;
  for (int i = 0; i < m; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int cnt = 0;
    for (int j = 0; j < n; j++) {
      if (x1 <= pii[j].first && pii[j].first <= x2 && y1 <= pii[j].second &&
          pii[j].second <= y2)
        cnt++;
    }
    printf("%d\\n", cnt);
  }
  // cout<<ans<<endl;
  return 0;
}
Problem p01540 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
void compress(vector<int> &c) {
  sort(c.begin(), c.end());
  c.erase(unique(c.begin(), c.end()), c.end());
}
int idx(int i, vector<int> &c) {
  return lower_bound(c.begin(), c.end(), i) - c.begin();
}
int sum[5010][5010];
int main() {
  int xs[5000], ys[5000];
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> x(n), y(n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", xs + i, ys + i);
    x[i] = xs[i];
    y[i] = ys[i];
  }
  compress(x);
  compress(y);
  for (int i = 0; i < n; i++) {
    int fx = idx(xs[i], x);
    int fy = idx(ys[i], y);
    sum[fy + 1][fx + 1]++;
  }
  for (int i = 0; i < y.size(); i++) {
    for (int j = 0; j < x.size(); j++) {
      sum[i + 1][j + 1] += sum[i][j + 1] + sum[i + 1][j] - sum[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    int x1, x2, y1, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    x1 = idx(x1, x);
    y1 = idx(y1, y);
    x2 = idx(x2 + 1, x);
    y2 = idx(y2 + 1, y);
    printf("%d\\n", sum[y2][x2] - sum[y2][x1] - sum[y1][x2] + sum[y1][x1]);
  }

  return 0;
}

Error: 127
/tmp/62e49752-6a49-4558-9d1d-0751cf62e767.out: error while loading shared libraries: libc.so.6: failed to map segment from shared object


Fixed submission:

#include <algorithm>
#include <cstdio>
#include <vector>
using namespace std;
inline void compress(vector<int> &c) {
  sort(c.begin(), c.end());
  c.erase(unique(c.begin(), c.end()), c.end());
}
inline int idx(int i, vector<int> &c) {
  return lower_bound(c.begin(), c.end(), i) - c.begin();
}
int sum[5010][5010];
int main() {
  int xs[5000], ys[1000];
  int n, m;
  scanf("%d%d", &n, &m);
  vector<int> x(n), y(n);
  for (int i = 0; i < n; i++) {
    scanf("%d%d", xs + i, ys + i);
    x[i] = xs[i];
    y[i] = ys[i];
  }
  compress(x);
  compress(y);
  for (int i = 0; i < n; i++) {
    int fx = idx(xs[i], x);
    int fy = idx(ys[i], y);
    sum[fy + 1][fx + 1]++;
  }
  for (int i = 0; i < y.size(); i++) {
    for (int j = 0; j < x.size(); j++) {
      sum[i + 1][j + 1] += sum[i][j + 1] + sum[i + 1][j] - sum[i][j];
    }
  }
  for (int i = 0; i < m; i++) {
    int x1, x2, y1, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    x1 = idx(x1, x);
    y1 = idx(y1, y);
    x2 = idx(x2 + 1, x);
    y2 = idx(y2 + 1, y);
    printf("%d\\n", sum[y2][x2] - sum[y2][x1] - sum[y1][x2] + sum[y1][x1]);
  }

  return 0;
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <utility>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<pair<int, int>> xy(n);
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    xy[i] = make_pair(x, y);
  }
  sort(xy.begin(), xy.end());

  for (int q = 0; q < m; q++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int count = 0;
    vector<pair<int, int>>::iterator it;
    it = lower_bound(xy.begin(), xy.end(), make_pair(x1, (int)-1e9 - 1));
    for (int i = it - xy.begin(); i < n; i++) {
      if (xy[i].first > x2)
        break;
      if (y1 <= xy[i].second && xy[i].second <= y2) {
        count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, m;
  cin >> n >> m;
  vector<int> x(n), y(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
  }
  for (int q = 0; q < m; q++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int count = 0;
    for (int i = 0; i < n; i++) {
      if (x1 <= x[i] && x[i] <= x2 && y1 <= y[i] && y[i] <= y2) {
        count++;
      }
    }
    cout << count << endl;
  }
  return 0;
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define BW(a, x, b) ((a) <= (x) && (x) <= (b))
#define ALL(v) (v).begin(), (v).end()
#define LENGTHOF(x) (sizeof(x) / sizeof(*(x)))
#define AFILL(a, b) fill((int *)a, (int *)(a + LENGTHOF(a)), b)
#define SQ(x) ((x) * (x))
#define Mod(x, mod) (((x)+(mod)%(mod))
#define MP make_pair
#define PB push_back
#define Fi first
#define Se second
#define INF (1 << 30)
#define EPS 1e-10
#define MOD 1000000007

typedef pair<int, int> P;
typedef vector<int> vi;
typedef queue<int> qi;
typedef long long ll;

map<int, int> zipX, zipY;
int unzipX[5005], unzipY[5005];
int cum[5005][5005];
int N, M;
vector<int> X, Y;

int compressX(vector<int> &x) {
  sort(x.begin(), x.end());
  x.erase(unique(x.begin(), x.end()), x.end());
  rep(i, x.size()) {
    zipX[x[i]] = i;
    unzipX[i] = x[i];
  }
  return x.size();
}
int compressY(vector<int> &y) {
  sort(y.begin(), y.end());
  y.erase(unique(y.begin(), y.end()), y.end());
  rep(i, y.size()) {
    zipY[y[i]] = i;
    unzipY[i] = y[i];
  }
  return y.size();
}

int main() {
  scanf("%d%d", &N, &M);
  X.resize(N);
  Y.resize(N);
  rep(i, N) scanf("%d%d", &X[i], &Y[i]);
  vector<int> x, y;
  x = X;
  y = Y;
  int W = compressX(x);
  int H = compressY(y);
  memset(cum, 0, sizeof(cum));
  rep(i, N) cum[zipX[X[i]] + 1][zipY[Y[i]] + 1]++;
  zipX[-INF] = 0;
  zipY[INF] = 0;
  zipX[INF] = W;
  zipY[INF] = H;
  rep(i, W + 1) rep(j, H) cum[i][j + 1] += cum[i][j];
  rep(j, H + 1) rep(i, W) cum[i + 1][j] += cum[i][j];
  /*  rep(i,W+1){
    rep(j,H+1) cout << cum[i][j] << " " ; cout << endl;
    }*/
  rep(i, M) {
    int x1, x2, y1, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    P a = *zipX.lower_bound(x1);
    P b = *zipY.lower_bound(y1);
    P c = *zipX.upper_bound(x2);
    P d = *zipY.upper_bound(y2);
    //    cout << a.second << " " << b.second << " " << c.second << " "
    //    <<d.second << " " << cum[a.second][d.second] << endl;
    printf("%d\\n", cum[a.second][b.second] - cum[c.second][b.second] -
                       cum[a.second][d.second] + cum[c.second][d.second]);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cstdio>

#define rep(i, j) REP((i), 0, (j))
#define REP(i, j, k) for (int i = (j); (i) < (k); ++i)
#define between(x, a, b) ((a) <= (x) && (x) <= (b))
using namespace std;
int grid[5000][2];
int main() {
  int n, m;
  scanf("%d%d", &n, &m);
  rep(i, n) { scanf("%d%d", &grid[i][0], &grid[i][1]); }
  int x1, y1, x2, y2;
  rep(i, m) {
    int res = 0;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    rep(j, n) if (between(grid[j][0], x1, x2) && between(grid[j][1], y1, y2))
        res++;
    printf("%d\\n", res);
  }
  return 0;
}
Problem p01540 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
int A[5010][2];
short S[10005][10005];
int ANS = 0;
int *R;
int Xs[10005], Ys[10005];
map<int, int> zipx, zipy;
int MAX = 1000000001;

int compress(int *X, map<int, int> &zip, int n) {
  sort(X, X + n);
  R = unique(X, X + n);
  int k = R - X;
  for (int i = 0; i < k; i++) {
    zip[X[i]] = i;
  }
  return k;
}

int main() {

  int N, M, x1, y1, x2, y2, xn, yn, ans;
  cin >> N >> M;
  for (int n = 0; n < N; n++) {
    cin >> x1 >> y1;
    A[n][0] = x1;
    A[n][1] = y1;
    Xs[n * 2] = x1;
    Xs[n * 2 + 1] = x1 - 1;
    Ys[n * 2] = y1;
    Ys[n * 2 + 1] = y1 - 1;
  }

  Xs[N * 2] = -1 * MAX;
  Xs[N * 2 + 1] = MAX;
  Ys[N * 2] = -1 * MAX;
  Ys[N * 2 + 1] = MAX;

  xn = compress(Xs, zipx, N * 2 + 2);
  yn = compress(Ys, zipy, N * 2 + 2);

  for (int n = 0; n < N; n++) {
    x1 = zipx[A[n][0]];
    y1 = zipy[A[n][1]];
    S[x1][y1]++;
  }

  for (int x = 1; x <= xn; x++) {
    for (int y = 1; y <= yn; y++) {
      S[x][y] += S[x - 1][y] + S[x][y - 1] - S[x - 1][y - 1];
    }
  }
  for (int m = 0; m < M; m++) {
    cin >> x1 >> y1 >> x2 >> y2;

    x1 = *lower_bound(Xs, Xs + xn, x1 - 1);
    y1 = *lower_bound(Ys, Ys + yn, y1 - 1);
    x2 = *lower_bound(Xs, Xs + xn, x2);
    y2 = *lower_bound(Ys, Ys + yn, y2);

    x1 = zipx[x1];
    x2 = zipx[x2];
    y1 = zipy[y1];
    y2 = zipy[y2];

    ans = S[x2][y2] - S[x1][y2] - S[x2][y1] + S[x1][y1];

    cout << ans << endl;
  }
}


Error: MLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
int A[5010][2];
int S[10005][10005];
int ANS = 0;
int *R;
int Xs[10005], Ys[10005];
map<int, int> zipx, zipy;
int MAX = 1000000001;

int compress(int *X, map<int, int> &zip, int n) {
  sort(X, X + n);
  R = unique(X, X + n);
  int k = R - X;
  for (int i = 0; i < k; i++) {
    zip[X[i]] = i;
  }
  return k;
}

int main() {
  int N, M, x1, y1, x2, y2, xn, yn, ans;
  cin >> N >> M;
  for (int n = 0; n < N; n++) {
    cin >> x1 >> y1;
    A[n][0] = x1;
    A[n][1] = y1;
    Xs[n * 2] = x1;
    Xs[n * 2 + 1] = x1 - 1;
    Ys[n * 2] = y1;
    Ys[n * 2 + 1] = y1 - 1;
  }

  Xs[N * 2] = -1 * MAX;
  Xs[N * 2 + 1] = MAX;
  Ys[N * 2] = -1 * MAX;
  Ys[N * 2 + 1] = MAX;

  xn = compress(Xs, zipx, N * 2 + 2);
  yn = compress(Ys, zipy, N * 2 + 2);

  for (int n = 0; n < N; n++) {
    x1 = zipx[A[n][0]];
    y1 = zipy[A[n][1]];
    S[x1][y1]++;
  }

  for (int x = 1; x <= xn; x++) {
    for (int y = 1; y <= yn; y++) {
      S[x][y] += S[x - 1][y] + S[x][y - 1] - S[x - 1][y - 1];
    }
  }
  for (int m = 0; m < M; m++) {
    cin >> x1 >> y1 >> x2 >> y2;

    x1 = *lower_bound(Xs, Xs + xn, x1 - 1);
    y1 = *lower_bound(Ys, Ys + yn, y1 - 1);
    x2 = *lower_bound(Xs, Xs + xn, x2);
    y2 = *lower_bound(Ys, Ys + yn, y2);

    x1 = zipx[x1];
    x2 = zipx[x2];
    y1 = zipy[y1];
    y2 = zipy[y2];

    ans = S[x2][y2] - S[x1][y2] - S[x2][y1] + S[x1][y1];

    cout << ans << endl;
  }
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
using namespace std;
int x[5000], y[5000];
int main() {
  int n, m, tmp;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];
  for (int i = 0; i < n - 1; i++) {
    for (int j = n - 1; j > i; j--) {
      if (x[j] < x[j - 1]) {
        tmp = x[j], x[j] = x[j - 1], x[j - 1] = tmp;
        tmp = y[j], y[j] = y[j - 1], y[j - 1] = tmp;
      }
    }
  }
  int leftx, rightx, lefty, righty;
  for (int u = 0; u < m; u++) {
    cin >> leftx >> lefty >> rightx >> righty;
    int bef, aft;
    int left = 0, right = n, mid;
    while (true) {
      if (right <= left + 1)
        break;
      mid = (right + left) / 2;
      if (x[mid] >= leftx)
        right = mid;
      else
        left = mid;
    }
    bef = max(0, mid - 1);
    left = 0, right = n;
    while (true) {
      if (right <= left + 1)
        break;
      mid = (right + left) / 2;
      if (x[mid] <= rightx)
        left = mid;
      else
        right = mid;
    }
    aft = min(n - 1, mid + 1);
    int ans = 0;
    for (int i = bef; i <= aft; i++) {
      if (x[i] >= leftx && x[i] <= rightx && y[i] >= lefty && y[i] <= righty)
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
using namespace std;
int x[5000], y[5000];
int main() {
  int n, m, tmp;
  cin >> n >> m;
  for (int i = 0; i < n; i++)
    cin >> x[i] >> y[i];
  for (int i = 0; i < n - 1; i++) {
    for (int j = n - 1; j > i; j--) {
      if (x[j] < x[j - 1]) {
        tmp = x[j], x[j] = x[j - 1], x[j - 1] = tmp;
        tmp = y[j], y[j] = y[j - 1], y[j - 1] = tmp;
      }
    }
  }
  int leftx, rightx, lefty, righty;
  for (int u = 0; u < m; u++) {
    cin >> leftx >> lefty >> rightx >> righty;
    int bef = n, aft = 0;
    for (int i = 0; i < n; i++) {
      if (leftx <= x[i] && bef == n)
        bef = i;
      if (rightx >= x[i])
        aft = i;
    }
    int ans = 0;
    for (int i = bef; i <= aft; i++) {
      if (y[i] >= lefty && y[i] <= righty)
        ans++;
    }
    cout << ans << endl;
  }
  return 0;
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded


#include <algorithm>
#include <climits>
#include <iostream>
#include <vector>
using namespace std;

int main() {
  int n, m;
  int x1, y1, x2, y2;
  vector<int> x, y;
  vector<int> ix, iy;
  vector<vector<int>> dp;

  cin >> n >> m;
  x.resize(n);
  y.resize(n);
  for (int i = 0; i < n; i++) {
    cin >> x[i] >> y[i];
    ix.push_back(x[i]);
    iy.push_back(y[i]);
  }
  ix.push_back(INT_MIN);
  iy.push_back(INT_MIN);
  sort(ix.begin(), ix.end());
  sort(iy.begin(), iy.end());
  ix.erase(unique(ix.begin(), ix.end()), ix.end());
  iy.erase(unique(iy.begin(), iy.end()), iy.end());

  dp.resize(iy.size(), vector<int>(ix.size(), 0));
  for (int i = 0; i < n; i++) {
    int ty = lower_bound(iy.begin(), iy.end(), y[i]) - iy.begin();
    int tx = lower_bound(ix.begin(), ix.end(), x[i]) - ix.begin();
    dp[ty][tx]++;
  }
  for (int i = 1; i < iy.size(); i++) {
    for (int j = 1; j < ix.size(); j++) {
      dp[i][j] += dp[i - 1][j] + dp[i][j - 1] - dp[i - 1][j - 1];
    }
  }

  for (int i = 0; i < m; i++) {
    cin >> x1 >> y1 >> x2 >> y2;
    int ix1, iy1, ix2, iy2;
    iy1 = lower_bound(iy.begin(), iy.end(), y1) - iy.begin();
    ix1 = lower_bound(ix.begin(), ix.end(), x1) - ix.begin();
    iy2 = upper_bound(iy.begin(), iy.end(), y2) - iy.begin() - 1;
    ix2 = upper_bound(ix.begin(), ix.end(), x2) - ix.begin() - 1;
    cout << dp[iy2][ix2] + dp[iy1 - 1][ix1 - 1] - dp[iy1 - 1][ix2] -
                dp[iy2][ix1 - 1]
         << endl;
  }
}

Error: TLE

Fixed submission:


#include <algorithm>
#include <iostream>
#include <set>
using namespace std;
typedef pair<int, int> P;
#define X first
#define Y second
int main() {
  int n, m;
  int x1, y1, x2, y2;
  multiset<P> point;
  multiset<P>::iterator it;
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    cin >> x1 >> y1;
    point.insert(P(x1, y1));
  }
  point.insert(P(1000000001, 1000000001));
  for (int j = 0; j < m; j++) {
    cin >> x1 >> y1 >> x2 >> y2;
    int cnt = 0;
    for (it = lower_bound(point.begin(), point.end(), P(x1, y1)); (*it).X <= x2;
         it++) {
      if ((*it).Y < y1)
        continue;
      if ((*it).Y > y2)
        continue;
      cnt++;
    }
    cout << cnt << endl;
  }
}
Problem p01540 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <vector>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
using namespace std;

int N, M, X[5010], Y[5010];

int compress(int *p, vector<int> &s) {
  REP(i, 0, N) s.push_back(p[i]);
  sort(s.begin(), s.end());
  s.erase(unique(s.begin(), s.end()), s.end());
  REP(i, 0, N) p[i] = find(s.begin(), s.end(), p[i]) - s.begin();
  return s.size();
}

int main(void) {
  cin >> N >> M;
  REP(i, 0, N) cin >> X[i] >> Y[i];
  vector<int> sx, sy;
  int w = compress(X, sx);
  int h = compress(Y, sy);

  int **sum = (int **)malloc((h + 1) * sizeof(int *));
  REP(i, 0, h + 1) sum[i] = (int *)malloc((w + 1) * sizeof(int));
  REP(i, 0, h + 1) REP(j, 0, w + 1) sum[i][j] = 0;
  REP(i, 0, N) sum[Y[i] + 1][X[i] + 1]++;
  REP(i, 0, h) REP(j, 0, w + 1) sum[i + 1][j] += sum[i][j];
  REP(i, 0, h + 1) REP(j, 0, w) sum[i][j + 1] += sum[i][j];

  REP(i, 0, M) {
    int lx, rx, ly, ry;
    cin >> lx >> ly >> rx >> ry;
    int vlx = lower_bound(sx.begin(), sx.end(), lx) - sx.begin();
    int vly = lower_bound(sy.begin(), sy.end(), ly) - sy.begin();
    int vrx = upper_bound(sx.begin(), sx.end(), rx) - sx.begin();
    int vry = upper_bound(sy.begin(), sy.end(), ry) - sy.begin();
    cout << (sum[vry][vrx] - sum[vry][vlx] - sum[vly][vrx] + sum[vly][vlx])
         << endl;
    ;
  }

  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>
#include <vector>
#define REP(i, a, n) for (int i = ((int)a); i < ((int)n); i++)
using namespace std;

int N, M, X[5010], Y[5010];

int compress(int *p, vector<int> &s) {
  REP(i, 0, N) s.push_back(p[i]);
  sort(s.begin(), s.end());
  s.erase(unique(s.begin(), s.end()), s.end());
  REP(i, 0, N) p[i] = find(s.begin(), s.end(), p[i]) - s.begin();
  return s.size();
}

int main(void) {
  cin >> N >> M;
  REP(i, 0, N) cin >> X[i] >> Y[i];
  vector<int> sx, sy;
  int w = compress(X, sx);
  int h = compress(Y, sy);

  int sum[h + 1][w + 1];
  REP(i, 0, h + 1) REP(j, 0, w + 1) sum[i][j] = 0;
  REP(i, 0, N) sum[Y[i] + 1][X[i] + 1]++;
  REP(i, 0, h) REP(j, 0, w + 1) sum[i + 1][j] += sum[i][j];
  REP(i, 0, h + 1) REP(j, 0, w) sum[i][j + 1] += sum[i][j];

  REP(i, 0, M) {
    int lx, rx, ly, ry;
    cin >> lx >> ly >> rx >> ry;
    int vlx = lower_bound(sx.begin(), sx.end(), lx) - sx.begin();
    int vly = lower_bound(sy.begin(), sy.end(), ly) - sy.begin();
    int vrx = upper_bound(sx.begin(), sx.end(), rx) - sx.begin();
    int vry = upper_bound(sy.begin(), sy.end(), ry) - sy.begin();
    cout << (sum[vry][vrx] - sum[vry][vlx] - sum[vly][vrx] + sum[vly][vlx])
         << endl;
    ;
  }

  return 0;
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 1000000001
using namespace std;
typedef pair<int, int> P;

int n, m;
vector<P> v;

int main() {
  scanf("%d%d", &n, &m);
  rep(i, n) {
    int x, y;
    scanf("%d%d", &x, &y);
    v.push_back(P(x, y));
  }
  sort(v.begin(), v.end());
  rep(i, m) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    int sx = 0, ex = n - 1, smid;
    int s, e;
    int cnt = 0;
    while (ex - sx > 1) {
      smid = (sx + ex) / 2;
      if (v[smid].first >= x1)
        ex = smid;
      else
        sx = smid;
    }
    // cout << sx << " " << ex << endl;
    s = sx;
    sx = 0;
    ex = n - 1;
    while (ex - sx > 1) {
      smid = (sx + ex) / 2;
      if (v[smid].first <= x2)
        sx = smid;
      else
        ex = smid;
    }
    // cout << sx << " " << ex << endl;
    e = ex;
    e = ex;
    if (x1 <= v[s].first && v[s].first <= x2 && y1 <= v[s].second &&
        v[s].second <= y2)
      cnt++;
    if (x1 <= v[e].first && v[e].first <= x2 && y1 <= v[e].second &&
        v[e].second <= y2)
      cnt++;

    for (int j = s + 1; j <= e - 1; j++)
      if (y1 <= v[j].second && v[j].second <= y2)
        cnt++;
    cout << cnt << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
typedef pair<int, int> P;

int n, m;
vector<P> v;

int main() {
  scanf("%d%d", &n, &m);
  rep(i, n) {
    int x, y;
    scanf("%d%d", &x, &y);
    v.push_back(P(x, y));
  }
  rep(i, m) {
    int x1, y1, x2, y2;
    scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
    int cnt = 0;
    rep(j, n) {
      if (x1 <= v[j].first && v[j].first <= x2 && y1 <= v[j].second &&
          v[j].second <= y2)
        cnt++;
    }
    cout << cnt << endl;
  }
}
Problem p01540 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;

P p[5000];
vector<int> dat[50000];
void init(int k, int l, int r) {
  if (r - l == 1) {
    dat[k].push_back(p[l].second);
    return;
  }
  int lb = k * 2 + 1, rb = k * 2 + 2;
  init(lb, l, (l + r) / 2);
  init(rb, (l + r) / 2, r);
  dat[k].resize(r - l);
  merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(),
        dat[k].begin());
}
int query(int a, int b, int c, int d, int k, int l, int r) {
  if (b <= l || r <= a)
    return 0;
  if (a <= l && r <= b) {
    int u = upper_bound(dat[k].begin(), dat[k].end(), d) - dat[k].begin();
    int v = upper_bound(dat[k].begin(), dat[k].end(), c - 1) - dat[k].begin();
    return u - v;
  }
  int lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2);
  int rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r);
  return lb + rb;
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  rep(i, n) scanf("%d%d", &p[i].first, &p[i].second);
  sort(p, p + n);
  init(0, 0, n);
  rep(i, q) {
    int sx, tx, sy, ty;
    scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
    int a = lower_bound(p, p + n, P(sx, INT_MIN)) - p;
    int b = lower_bound(p, p + n, P(tx, INT_MAX)) - p;
    printf("%d\\n", query(a, b, sy, ty, 0, 0, n));
  }
}

Error: 0

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef pair<int, int> P;

P p[5000];
vector<int> dat[10000];
void init(int k, int l, int r) {
  if (r - l == 1) {
    dat[k].push_back(p[l].second);
    return;
  }
  int lb = k * 2 + 1, rb = k * 2 + 2;
  init(lb, l, (l + r) / 2);
  init(rb, (l + r) / 2, r);
  dat[k].resize(r - l);
  merge(dat[lb].begin(), dat[lb].end(), dat[rb].begin(), dat[rb].end(),
        dat[k].begin());
}
int query(int a, int b, int c, int d, int k, int l, int r) {
  if (b <= l || r <= a)
    return 0;
  if (a <= l && r <= b) {
    int u = upper_bound(dat[k].begin(), dat[k].end(), d) - dat[k].begin();
    int v = upper_bound(dat[k].begin(), dat[k].end(), c - 1) - dat[k].begin();
    return u - v;
  }
  int lb = query(a, b, c, d, k * 2 + 1, l, (l + r) / 2);
  int rb = query(a, b, c, d, k * 2 + 2, (l + r) / 2, r);
  return lb + rb;
}
int main() {
  int n, q;
  scanf("%d%d", &n, &q);
  rep(i, n) scanf("%d%d", &p[i].first, &p[i].second);
  sort(p, p + n);
  init(0, 0, n);
  rep(i, q) {
    int sx, tx, sy, ty;
    scanf("%d%d%d%d", &sx, &sy, &tx, &ty);
    int a = lower_bound(p, p + n, P(sx, INT_MIN)) - p;
    int b = lower_bound(p, p + n, P(tx, INT_MAX)) - p;
    printf("%d\\n", query(a, b, sy, ty, 0, 0, n));
  }
}