Problem p01540 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;

const pii Dir[8] = { //?????????
    {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

signed main() {
  int n, m;
  scanf("%d %d", &n, &m);
  vector<pii> px(n);
  vector<pii> py(n);
  vector<int> pxynum(n);
  REP(i, n) {
    scanf("%d %d", &px[i].first, &px[i].second);
    py[i] = {px[i].second, px[i].first};
  }
  sort(ALL(px));
  sort(ALL(py));
  int cou = 0;
  for (auto cur : px) {
    auto itr = lower_bound(ALL(py), make_pair(cur.second, cur.first));
    pxynum[cou] = itr - py.begin();
    cou++;
  }

  REP(i, m) {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);

    auto s = lower_bound(ALL(px), make_pair(a, -INT_MAX));
    auto f = upper_bound(s, px.end(), make_pair(c, INT_MAX));
    int ys = lower_bound(ALL(py), make_pair(b, -INT_MAX)) - py.begin();
    int yf = upper_bound(py.begin() + ys, py.end(), make_pair(d, INT_MAX)) -
             py.begin();
    int ans = 0;
    for (int i = s - px.begin(); i < f - px.begin(); i++) {
      if (RANGE(pxynum[i], ys, yf - 1)) {
        ans++;
      }
    }
    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <functional>
#include <iostream>
#include <limits.h>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <vector>
#define rep(i, s, n) for (int i = (s); (n) > i; i++)
#define REP(i, n) rep(i, 0, n)
#define RANGE(x, a, b) ((a) <= (x) && (x) <= (b))
#define DUPLE(a, b, c, d)                                                      \\
  (RANGE(a, c, d) || RANGE(b, c, d) || RANGE(c, a, b) || RANGE(d, a, b))
#define INCLU(a, b, c, d) (RANGE(a, c, d) && (b, c, d))
#define PW(x) ((x) * (x))
#define ALL(x) (x).begin(), (x).end()
#define MODU 1000000007
#define bitcheck(a, b) ((a >> b) & 1)
#define bitset(a, b) (a |= (1 << b))
#define bitunset(a, b) (a &= ~(1 << b))
using namespace std;
typedef pair<int, int> pii;
typedef long long ll;

const pii Dir[8] = { //?????????
    {0, 1}, {0, -1}, {1, 0}, {-1, 0}, {1, 1}, {1, -1}, {-1, -1}, {-1, 1}};
template <typename A, size_t N, typename T>
void Fill(A (&array)[N], const T &val) {
  std::fill((T *)array, (T *)(array + N), val);
}

typedef complex<int> point;
struct kdtree {
  struct node {
    point p;
    node *l, *r;
    node(const point &p) : p(p), l(NULL), r(NULL) {}
  } *root;
  kdtree() : root(NULL) {}

#define compare(d, p, q) (d ? real(p) < real(q) : imag(p) < imag(q))
  void insert(const point &p) { root = insert(root, 0, p); }
  node *insert(node *t, int d, const point &p) {
    if (t == NULL)
      return new node(p);
    if (compare(d, p, t->p))
      t->l = insert(t->l, !d, p);
    else
      t->r = insert(t->r, !d, p);
    return t;
  }
  int search(const point &ld, const point &ru) {
    return search(root, 0, ld, ru);
  }

  int search(node *t, int d, const point &ld, const point &ru) {
    int cou = 0;

    if (t == NULL)
      return 0;
    const point &p = t->p;
    if (real(ld) <= real(p) && real(p) <= real(ru) && imag(ld) <= imag(p) &&
        imag(p) <= imag(ru))
      cou++;
    if (!compare(d, p, ld))
      cou += search(t->l, !d, ld, ru);
    if (!compare(d, ru, p))
      cou += search(t->r, !d, ld, ru);
    return cou;
  }
};

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

  REP(i, n) {
    int x, y;
    scanf("%d %d", &x, &y);

    kd.insert({x, y});
  }
  REP(i, m) {
    int a, b, c, d;
    scanf("%d %d %d %d", &a, &b, &c, &d);

    printf("%d\\n", kd.search({a, b}, {c, d}));
  }
  return 0;
}