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

#include <bits/stdc++.h>
using namespace std;
typedef pair<int, int> pii;

int main() {
  int n, m, x, y, xx, yy, ans;
  std::cin >> n >> m;
  vector<pii> t(n + 1);
  t[n].first = t[n].second = 1e9 + 7;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &t[i].first, &t[i].second);
  }
  sort(t.begin(), t.end());
  for (int i = 0; i < m; i++) {
    ans = 0;
    scanf("%d %d %d %d", &x, &y, &xx, &yy);
    for (int j = 0; j < n; j++) {
      if (x <= t[j].first and t[j].first <= xx) {
        if (y <= t[j].second and t[j].second <= yy)
          ans++;
      } else if (x < t[j].first) {
        break;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

int main() {
  int n, m, x, y, xx, yy, ans;
  std::cin >> n >> m;
  vector<int> tx(n + 1), ty(n + 1);
  tx[n] = ty[n] = 1e9 + 7;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &tx[i], &ty[i]);
  }
  for (int i = 0; i < m; i++) {
    ans = 0;
    scanf("%d %d %d %d", &x, &y, &xx, &yy);
    for (int j = 0; j < n; j += 2) {
      if (x <= tx[j] and tx[j] <= xx)
        if (y <= ty[j] and ty[j] <= yy)
          ans++;
      if (x <= tx[j + 1] and tx[j + 1] <= xx)
        if (y <= ty[j + 1] and ty[j + 1] <= yy)
          ans++;
    }
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;

  vector<pair<int, int>> v;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }

  sort(v.begin(), v.end());

  rep(i, m) {
    int ans = 0;
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;

    rep(j, n) {
      int X = v[j].first;
      int Y = v[j].second;

      if (X < x1)
        continue;

      REP(k, j, n) {
        int X2 = v[k].first;
        int Y2 = v[k].second;

        if (X2 > x2)
          break;
        if (y1 <= Y2 && Y2 <= y2)
          ans++;
      }
      break;
    }

    cout << ans << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

#define REP(i, k, n) for (int i = k; i < n; i++)
#define rep(i, n) for (int i = 0; i < n; i++)

using namespace std;
typedef long long ll;

int main() {
  int n, m;
  cin >> n >> m;

  vector<pair<int, int>> v;
  rep(i, n) {
    int x, y;
    cin >> x >> y;
    v.push_back(make_pair(x, y));
  }

  rep(i, m) {
    int ans = 0;
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;

    rep(j, n) {
      int X = v[j].first;
      int Y = v[j].second;

      if (x1 <= X && X <= x2 && y1 <= Y && Y <= y2) {
        ans++;
      }
    }

    cout << ans << endl;
  }

  return 0;
}
Problem p01540 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int map[5002][5002];

int main() {
  int n, m;
  cin >> n >> m;
  int x[n], y[n], xx[n + 2], yy[n + 2];
  rep(i, n) cin >> x[i] >> y[i];
  rep(i, n) xx[i] = x[i];
  rep(i, n) yy[i] = y[i];
  xx[n] = 2e9;
  yy[n] = 2e9;
  xx[n + 1] = -2e9;
  yy[n + 1] = -2e9;
  sort(xx, xx + n + 2);
  sort(yy, yy + n + 2);
  rep(i, n + 2) rep(j, n + 2) map[i][j] = 0;
  rep(i, n) {
    int nx, ny;
    rep(j, n + 2) if (x[i] == xx[j]) nx = j;
    rep(j, n + 2) if (y[i] == yy[j]) ny = j;
    ++map[ny][nx];
    --map[n + 1][nx];
    --map[ny][n + 1];
    ++map[n + 1][n + 1];
  }
  rep(i, n + 2) rep(j, n + 1) map[i][j + 1] += map[i][j];
  rep(i, n + 1) rep(j, n + 2) map[i + 1][j] += map[i][j];
  rep(i, m) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int xx1 = lower_bound(xx, xx + n + 2, x1) - xx - 1;
    int yy1 = lower_bound(yy, yy + n + 2, y1) - yy - 1;
    int xx2 = upper_bound(xx, xx + n + 2, x2) - xx - 1;
    int yy2 = upper_bound(yy, yy + n + 2, y2) - yy - 1;
    cout << map[yy2][xx2] - map[yy2][xx1] - map[yy1][xx2] + map[yy1][xx1]
         << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <iostream>

using namespace std;

#define rep(i, n) for (int i = 0; i < int(n); ++i)

int main() {
  int n, m;
  cin >> n >> m;
  int x[n], y[n], xx[n + 2], yy[n + 2];
  rep(i, n) cin >> x[i] >> y[i];
  rep(i, n) xx[i] = x[i];
  rep(i, n) yy[i] = y[i];
  xx[n] = 2e9;
  yy[n] = 2e9;
  xx[n + 1] = -2e9;
  yy[n + 1] = -2e9;
  sort(xx, xx + n + 2);
  sort(yy, yy + n + 2);
  int map[n + 2][n + 2];
  rep(i, n + 2) rep(j, n + 2) map[i][j] = 0;
  rep(i, n) {
    int nx, ny;
    rep(j, n + 2) if (x[i] == xx[j]) nx = j;
    rep(j, n + 2) if (y[i] == yy[j]) ny = j;
    ++map[ny][nx];
    --map[n + 1][nx];
    --map[ny][n + 1];
    ++map[n + 1][n + 1];
  }
  rep(i, n + 2) rep(j, n + 1) map[i][j + 1] += map[i][j];
  rep(i, n + 1) rep(j, n + 2) map[i + 1][j] += map[i][j];
  rep(i, m) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    int xx1 = lower_bound(xx, xx + n + 2, x1) - xx - 1;
    int yy1 = lower_bound(yy, yy + n + 2, y1) - yy - 1;
    int xx2 = upper_bound(xx, xx + n + 2, x2) - xx - 1;
    int yy2 = upper_bound(yy, yy + n + 2, y2) - yy - 1;
    cout << map[yy2][xx2] - map[yy2][xx1] - map[yy1][xx2] + map[yy1][xx1]
         << endl;
  }
  return 0;
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <vector>

using namespace std;

int n, m, x1_, y1_, x2_, y2_;

vector<int> x, y;

int main() {
  scanf("%d", &n);
  scanf("%d", &m);

  x = vector<int>(n);
  y = vector<int>(n);

  for (int i = 0; i < n; i++) {
    scanf("%d", &x[i]);
    scanf("%d", &y[i]);
  }

  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      if (x[i] > x[j]) {
        swap(x[i], x[j]);
        swap(y[i], y[j]);
      }
    }
  }

  for (int i = 0; i < m; i++) {
    scanf("%d", &x1_);
    scanf("%d", &y1_);
    scanf("%d", &x2_);
    scanf("%d", &y2_);

    int s = distance(x.begin(), upper_bound(x.begin(), x.end(), x1_ - 1));
    int t = distance(x.begin(), lower_bound(x.begin(), x.end(), x2_ + 1));

    int ret = 0;

    for (int j = s; j < t; j++) {
      if (y1_ <= y[j] && y[j] <= y2_) {
        ret++;
      }
    }

    printf("%d\\n", ret);
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <vector>

using namespace std;

int n, m, x, y, x1, y1, x2, y2;

vector<pair<int, int>> p;

int main() {
  scanf("%d", &n);
  scanf("%d", &m);

  for (int i = 0; i < n; i++) {
    scanf("%d", &x);
    scanf("%d", &y);

    p.push_back(make_pair(x, y));
  }

  for (int i = 0; i < m; i++) {
    scanf("%d", &x1);
    scanf("%d", &y1);
    scanf("%d", &x2);
    scanf("%d", &y2);

    int ret = 0;

    for (int i = 0; i < n; i++) {
      if (x1 <= p[i].first && p[i].first <= x2 && y1 <= p[i].second &&
          p[i].second <= y2) {
        ret++;
      }
    }

    printf("%d\\n", ret);
  }

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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef pair<int, int> P;

int main() {
  int n, m;
  P data[5000];
  P data2[5000];
  bool ans[5000];
  while (cin >> n >> m) {
    for (int i = 0; i < n; i++) {
      int a, b;
      scanf("%d%d", &a, &b);
      data[i] = P(a, i);
      data2[i] = P(b, i);
    }
    sort(data, data + n);
    sort(data2, data2 + n);
    for (int i = 0; i < m; i++) {
      P a, b;
      scanf("%d%d%d%d", &a.first, &a.second, &b.first, &b.second);
      fill(ans, ans + 5000, false);
      int l = lower_bound(data, data + n, P(a.first, -1)) - data;
      int r = lower_bound(data, data + n, P(b.first + 1, -1)) - data;
      for (int j = l; j < r; j++) {
        ans[data[j].second] = true;
      }

      int l2 = lower_bound(data2, data2 + n, P(a.second, -1)) - data2;
      int r2 = lower_bound(data2, data2 + n, P(b.second + 1, -1)) - data2;
      int cnt = 0;
      for (int j = l2; j < r2; j++) {
        if (ans[data2[j].second])
          cnt++;
      }
      printf("%d\\n", cnt);
    }
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

typedef pair<int, int> S;
typedef pair<int, S> P;

int n, m;
P data[5000];
P data2[5000];
bool ans[5000];

class compare {
public:
  bool operator()(P a, P b) const { return a.first < b.first; }
};

int main() {
  while (cin >> n >> m) {
    for (int i = 0; i < n; i++) {
      int a, b; // x, y
      scanf("%d%d", &a, &b);
      // data.push_back(P(a, b));
      data[i] = P(a, S(b, i));  // x, y
      data2[i] = P(b, S(a, i)); // y, x
    }
    sort(data, data + n, compare());
    sort(data2, data2 + n, compare());
    for (int i = 0; i < m; i++) {
      fill(ans, ans + 5000, false);
      S a, b;
      scanf("%d%d%d%d", &a.second, &a.first, &b.second, &b.first); // x
      int l = lower_bound(data, data + n, P(a.second, S(-1000000001, -1)),
                          compare()) -
              data;
      int r = lower_bound(data, data + n, P(b.second + 1, S(-1000000001, -1)),
                          compare()) -
              data;
      for (int j = l; j < r; j++) {
        ans[data[j].second.second] = true;
      }
      //      cout << l << ' ' << r << endl;
      // y
      int l2 = lower_bound(data2, data2 + n, P(a.first, S(-100000001, -1)),
                           compare()) -
               data2;
      int r2 = lower_bound(data2, data2 + n, P(b.first + 1, S(-100000001, -1)),
                           compare()) -
               data2;
      int cnt = 0;
      for (int j = l2; j < r2; j++) {
        if (ans[data2[j].second.second])
          cnt++;
      }
      //      cout << l2 << ' ' << r2 << endl;
      printf("%d\\n", cnt);
    }
  }
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <stdio.h>
using namespace std;
struct Point {
  int x, y;
  bool operator<(const Point &p) const { return x < p.x; }
};
int main() {
  int n, m;
  cin >> n >> m;
  int i, j, k;
  Point p[n];
  for (i = 0; i < n; i++)
    scanf("%d %d", &p[i].x, &p[i].y);
  sort(p, p + n);
  int sx, sy, tx, ty;
  for (i = 0; i < m; i++) {
    scanf("%d %d %d %d", &sx, &sy, &tx, &ty);
    int ans = 0;
    for (j = 0; j < n; j++) {
      if (sx <= p[j].x && p[j].x <= tx && sy <= p[j].y && p[j].y <= ty) {
        ans++;
      }
      if (tx < p[j].x)
        break;
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <iostream>
#include <stdio.h>
using namespace std;
int main() {
  int n, m;
  cin >> n >> m;
  int i, j, k;
  int x[n], y[n];
  for (i = 0; i < n; i++)
    scanf("%d %d", &x[i], &y[i]);
  int sx, sy, tx, ty;
  for (i = 0; i < m; i++) {
    scanf("%d %d %d %d", &sx, &sy, &tx, &ty);
    int ans = 0;
    for (j = 0; j < n; j++)
      if (sx <= x[j] && x[j] <= tx && sy <= y[j] && y[j] <= ty)
        ans++;
    printf("%d\\n", ans);
  }
  return 0;
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define s second
#define f first
#define r(i, n) for (int i = 0; i < n; i++)
using namespace std;
main() {
  int x, y, q, n, ty, tx;
  pair<int, int> a[5000];
  cin >> n >> q;
  r(i, n) cin >> x >> y, a[i].f = x, a[i].s = y;
  sort(a, a + n);
  r(i, q) {
    cin >> x >> y >> tx >> ty;
    int c = 0;
    r(i, n) {
      if (x <= a[i].f && a[i].f <= tx && y <= a[i].s && a[i].s <= ty)
        c++;
      if (tx < a[i].f)
        break;
    }
    cout << c << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;
class Node {
public:
  int location;
  int p, l, r;
  Node() {}
};
class Point {
public:
  int id, x, y;
  Point() {}
  Point(int id, int x, int y) : id(id), x(x), y(y) {}
  bool operator<(const Point &p) { return id < p.id; }
};
static const int MAX = 1000000;
static const int NIL = -1;
int n;
Point P[MAX];
Node T[MAX];
int np;
bool lessX(const Point &p1, const Point &p2) { return p1.x < p2.x; }
bool lessY(const Point &p1, const Point &p2) { return p1.y < p2.y; }
int makeKDTree(int l, int r, int d) {
  if (!(l < r))
    return NIL;
  int mid = (l + r) / 2;
  int t = np++;
  if (d % 2 == 0)
    sort(P + l, P + r, lessX);
  else
    sort(P + l, P + r, lessY);
  T[t].location = mid;
  T[t].l = makeKDTree(l, mid, d + 1);
  T[t].r = makeKDTree(mid + 1, r, d + 1);
  return t;
}
void find(int v, int sx, int tx, int sy, int ty, int d, vector<Point> &ans) {
  int x = P[T[v].location].x;
  int y = P[T[v].location].y;
  if (sx <= x && x <= tx && sy <= y && y <= ty)
    ans.push_back(P[T[v].location]);
  if (d % 2 == 0) {
    if (T[v].l != NIL)
      if (sx <= x)
        find(T[v].l, sx, tx, sy, ty, d + 1, ans);
    if (T[v].r != NIL)
      if (x <= tx)
        find(T[v].r, sx, tx, sy, ty, d + 1, ans);
  } else {
    if (T[v].l != NIL)
      if (sy <= y)
        find(T[v].l, sx, tx, sy, ty, d + 1, ans);
    if (T[v].r != NIL)
      if (y <= ty)
        find(T[v].r, sx, tx, sy, ty, d + 1, ans);
  }
}
int main() {
  int x, y, q;
  cin >> n >> q;
  rep(i, n) {
    scanf("%d %d", &x, &y);
    P[i] = Point(i, x, y);
    T[i].l = T[i].r = T[i].p = NIL;
  }
  np = 0;
  int root = makeKDTree(0, n, 0);
  int sx, sy, tx, ty;
  vector<Point> ans;
  rep(i, q) {
    scanf("%d %d %d %d", &sx, &sy, &tx, &ty);
    ans.clear();
    find(root, sx, tx, sy, ty, 0, ans);
    cout << ans.size() << endl;
  }
  return 0;
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <iostream>
using namespace std;
#pragma warning(disable : 4996)
int n, m;
pair<int, int> x[10000];
int a, b, c, d, e, f, g, h, sum, l, r, mi;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &x[i].first, &x[i].second);
  }
  sort(x, x + n);
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d %d", &a, &b, &c, &d);
    sum = 0;
    l = 0;
    r = n;
    mi = n / 2;
    for (int j = 0; j < 30; j++) {
      mi = (l + r) / 2;
      if (x[mi].first < a) {
        l = mi;
      }
      if (x[mi].first > a) {
        r = mi;
      }
    }
    e = mi;
    l = 0;
    r = n;
    for (int j = 0; j < 30; j++) {
      mi = (l + r) / 2;
      if (x[mi].first < c) {
        l = mi;
      }
      if (x[mi].first > c) {
        r = mi;
      }
    }
    f = mi;
    g = max(0, e - 5);
    h = min(n - 1, f + 5);
    for (int j = g; j <= h; j++) {
      if (a <= x[j].first) {
        if (x[j].first <= c) {
          if (b <= x[j].second) {
            if (x[j].second <= d) {
              sum++;
            }
          }
        }
      }
    }
    printf("%d\\n", sum);
  }
}

Error: TLE

Fixed submission:

#include <cstdio>
#include <iostream>
using namespace std;
int n, m;
int x[10000], y[10000];
int a, b, c, d, sum;
int main() {
  cin >> n >> m;
  for (int i = 0; i < n; i++) {
    scanf("%d %d", &x[i], &y[i]);
  }
  for (int i = 0; i < m; i++) {
    scanf("%d %d %d %d", &a, &b, &c, &d);
    sum = 0;
    for (int j = 0; j < n; j++) {
      if (a <= x[j]) {
        if (x[j] <= c) {
          if (b <= y[j]) {
            if (y[j] <= d) {
              sum++;
            }
          }
        }
      }
    }
    printf("%d\\n", sum);
  }
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <string>
#include <vector>

using namespace std;

#define INF (1 << 29)
#define EPS (1e-10)
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int, int> pii;

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int main() {
  int n, m;
  cin >> n >> m;
  vector<pii> treasure;
  for (int i = 0; i < n; i++) {
    int x, y;
    // cin >> x >> y;
    scanf("%d %d", &x, &y);
    treasure.pb(mp(x, y));
  }
  sort(treasure.begin(), treasure.end());
  for (int i = 0; i < m; i++) {
    int x1, y1, x2, y2;
    // cin >> x1 >> y1 >> x2 >> y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    // search
    int ans = 0;
    vector<pii>::iterator it =
        lower_bound(treasure.begin(), treasure.end(), mp(x1, y1));
    while (1) {
      if (it == treasure.end() || *it > mp(x2, y2))
        break;
      if ((*it).first > x2)
        it = upper_bound(treasure.begin(), treasure.end(),
                         mp(-INF, (*it).second + 1));
      if (x1 <= (*it).first && (*it).first <= x2 && y1 <= (*it).second &&
          (*it).second <= y2)
        ans++;
      it++;
    }
    // cout << ans << endl;
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define INF (1 << 29)
#define EPS (1e-10)
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vii;
typedef pair<int, int> pii;

int dx[] = {0, 1, 0, -1}, dy[] = {1, 0, -1, 0};

int main() {
  int n, m;
  cin >> n >> m;
  vector<pii> treasure;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    treasure.pb(mp(x, y));
  }
  sort(treasure.begin(), treasure.end());
  for (int i = 0; i < m; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    // search
    int ans = 0;
    vector<pii>::iterator it =
        lower_bound(treasure.begin(), treasure.end(), mp(x1, y1));
    while (1) {
      if (it == treasure.end() || *it > mp(x2, y2))
        break;
      if ((*it).first > x2)
        it = upper_bound(treasure.begin(), treasure.end(),
                         mp(-INF, (*it).second + 1));
      if (x1 <= (*it).first && (*it).first <= x2 && y1 <= (*it).second &&
          (*it).second <= y2)
        ans++;
      it++;
    }
    cout << ans << endl;
  }
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

// iostream のscanf, printf では間に合わなかった
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define INF (1 << 29)
#define mp make_pair
#define pb push_back

typedef pair<int, int> pii;

int main() {
  int n, m;
  cin >> n >> m;
  vector<pii> treasure;
  for (int i = 0; i < n; i++) {
    int x, y;
    // cin >> x >> y;
    scanf("%d %d", &x, &y);
    treasure.pb(mp(x, y));
  }
  sort(treasure.begin(), treasure.end());
  for (int i = 0; i < m; i++) {
    int x1, y1, x2, y2;
    // cin >> x1 >> y1 >> x2 >> y2;
    scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    // search
    int ans = 0;
    vector<pii>::iterator it =
        lower_bound(treasure.begin(), treasure.end(), mp(x1, y1));
    vector<pii>::iterator ite =
        upper_bound(treasure.begin(), treasure.end(), mp(x2, y2));
    while (1) {
      // if( it == treasure.end() || *it > mp(x2,y2) ) break;
      if (it == ite)
        break;
      if (x1 <= (*it).first && (*it).first <= x2 && y1 <= (*it).second &&
          (*it).second <= y2)
        ans++;
      it++;
    }
    // cout << ans << endl;
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

// iostream のscanf, printf では間に合わなかった
#include <algorithm>
#include <cstdio>
#include <iostream>
#include <map>
#include <vector>

using namespace std;

#define INF (1 << 29)
#define mp make_pair
#define pb push_back

typedef pair<int, int> pii;

int main() {
  int n, m;
  cin >> n >> m;
  vector<pii> treasure;
  for (int i = 0; i < n; i++) {
    int x, y;
    cin >> x >> y;
    // scanf("%d %d", &x, &y);
    treasure.pb(mp(x, y));
  }
  sort(treasure.begin(), treasure.end());
  for (int i = 0; i < m; i++) {
    int x1, y1, x2, y2;
    cin >> x1 >> y1 >> x2 >> y2;
    // scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
    //  search
    int ans = 0;
    vector<pii>::iterator it =
        lower_bound(treasure.begin(), treasure.end(), mp(x1, y1));
    while (1) {
      if (it == treasure.end() || *it > mp(x2, y2))
        break;
      if (x1 <= (*it).first && (*it).first <= x2 && y1 <= (*it).second &&
          (*it).second <= y2)
        ans++;
      it++;
    }
    cout << ans << endl;
    // printf("%d\\n", ans);
  }
}
Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
using namespace std;
#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define rep(i, n) FOR((i), 0, (n))
#define N_MAX 5000
#define M_MAX 500000
#define ll long long

//??????????????¨??????
int n, m;
int xy[N_MAX][2];
int square[4];

//??????
int ans;
bool ok;

int main() {
  //??\\???
  cin >> n >> m;
  rep(i, n) rep(j, 2) cin >> xy[i][j];

  // xy??????x???????????????????????????????????????
  bool flag = true;
  for (int i = 0; flag; i++) {
    flag = 0;
    for (int j = n - 1; j >= i + 1; j--) {
      if (xy[j][0] < xy[j - 1][0]) {
        //??£??\\????´??????????
        swap(xy[j], xy[j - 1]);
        flag = 1;
        /*
        temp0 = xy[j][0];
        xy[j][0] = xy[j - 1][0];
        xy[j - 1][0] = temp0;

        temp1 = xy[j][1];
        xy[j][1] = xy[j - 1][1];
        xy[j - 1][1] = temp1;

        rep(k, 2) {
                temp[k]=xy[j][k];
                xy[j][k]=xy[j-1][k];
                xy[j-1][k]=temp[k];
        }
        */
      }
    }
  }

  //?¨????
  rep(roop, m) {
    //???????¨????
    ans = 0;

    //??\\???
    rep(i, 4) cin >> square[i];

    //??????????§??????????x?????´????????¢???(?????????????????????)
    int left = 0, right = n, mid;
    while (left < right) {
      mid = (left + right) / 2;
      if (left == mid)
        break;
      if (xy[mid][0] == square[0])
        break;
      else if (square[0] < xy[mid][0])
        right = mid;
      else
        left = mid;
    }

    // cout<<"left: "<<left<<endl;
    // cout << "ss" << square[1]<<endl;
    // cout<<xy[left][1] << endl;
    while (xy[left][0] <= square[2]) {
      if (square[0] <= xy[left][0])
        if (xy[left][0] <= square[2])
          if (square[1] <= xy[left][1])
            if (xy[left][1] <= square[3])
              ans++;
      left++;
      if (left >= n)
        break;
    }
    cout << ans << endl;
  }
  /*
  //???????????°
  rep(i, n) {
          rep(j,2)cout<<xy[i][j]<<" ";
          cout<<endl;
  }
  */

  // cin>>m;

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <math.h>
#include <queue>
#include <stack>
using namespace std;
#define FOR(k, m, n) for (int(k) = (m); (k) < (n); (k)++)
#define rep(i, n) FOR((i), 0, (n))
#define N_MAX 5000
#define M_MAX 500000
#define ll long long

//??????????????¨??????
int n, m;
int xy[N_MAX][2];
int square[4];

//??????
int ans;
bool ok;

int main() {
  //??\\???
  cin >> n >> m;
  rep(i, n) rep(j, 2) cin >> xy[i][j];

  //?¨????
  rep(roop, m) {
    //???????¨????
    ans = 0;

    //??\\???
    rep(i, 4) cin >> square[i];

    //?¨????
    rep(i, n) {
      if (square[0] <= xy[i][0] && xy[i][0] <= square[2] &&
          square[1] <= xy[i][1] && xy[i][1] <= square[3])
        ans++;
    }
    cout << ans << endl;
  }
  // cin>>m;
  return 0;
}