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

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#ifdef int
const ll INF = (1LL << 60);
#else
const int INF = (1LL << 30);
#endif
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

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

template <typename T> inline void compress(vector<T> &c) {
  sort(ALL(c));
  c.erase(unique(ALL(c)), c.end());
}
template <typename T> inline size_t idx(T i, vector<T> const &c) {
  return lower_bound(ALL(c), i) - c.begin();
}

int xx[5010], yy[5010], sum[5010][5010];

signed main(void) {
  int n, m;
  cin >> n >> m;
  VI x(n), y(n);
  REP(i, n) {
    cin >> xx[i] >> yy[i];
    x[i] = xx[i], y[i] = yy[i];
  }
  compress(x);
  compress(y);
  REP(i, n) {
    int tx = idx(xx[i], x), ty = idx(yy[i], y);
    sum[ty + 1][tx + 1]++;
  }
  REP(i, y.size()) REP(j, x.size()) {
    sum[i + 1][j + 1] += sum[i + 1][j] + sum[i][j + 1] - sum[i][j];
  }

  REP(i, m) {
    int sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    sx = idx(sx, x), sy = idx(sy, y);
    gx = idx(gx + 1, x), gy = idx(gy + 1, y);
    cout << sum[gy][gx] - sum[gy][sx] - sum[sy][gx] + sum[sy][sx] << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

// #define __USE_MINGW_ANSI_STDIO 0
#include <bits/stdc++.h>

using namespace std;
typedef long long ll;
#define int ll
typedef vector<int> VI;
typedef vector<VI> VVI;
typedef vector<ll> VL;
typedef vector<VL> VVL;
typedef pair<int, int> PII;

#define FOR(i, a, n) for (ll i = (ll)a; i < (ll)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define ALL(x) x.begin(), x.end()
#define IN(a, b, x) (a <= x && x < b)
#define MP make_pair
#define PB push_back
#ifdef int
const ll INF = (1LL << 60);
#else
const int INF = (1LL << 30);
#endif
const double PI = 3.14159265359;
const double EPS = 1e-12;
const int MOD = 1000000007;

template <typename T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <typename T> T &chmax(T &a, const T &b) { return a = max(a, b); }

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

// point
typedef complex<double> point;
int ret;

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;
  }
  template <class OUT> void search(const point &ld, const point &ru, OUT out) {
    search(root, 0, ld, ru, out);
  }
  template <class OUT>
  void search(node *t, int d, const point &ld, const point &ru, OUT out) {
    if (t == NULL)
      return;
    const point &p = t->p;
    if (real(ld) <= real(p) && real(p) <= real(ru) && imag(ld) <= imag(p) &&
        imag(p) <= imag(ru))
      ret++;
    if (!compare(d, p, ld))
      search(t->l, !d, ld, ru, out);
    if (!compare(d, ru, p))
      search(t->r, !d, ld, ru, out);
  }
};

kdtree tree;
point *out;
signed main(void) {
  int n, m;
  cin >> n >> m;
  REP(i, n) {
    double x, y;
    cin >> x >> y;
    tree.insert({x, y});
  }
  REP(i, m) {
    double sx, sy, gx, gy;
    cin >> sx >> sy >> gx >> gy;
    ret = 0;
    tree.search({sx, sy}, {gx, gy}, out);
    cout << ret << endl;
  }

  return 0;
}