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

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/

void compress(vi &v) {
  sort(ALL(v));
  v.erase(unique(ALL(v)), v.end());
}

int main() {
  int n, m;
  cin >> n >> m;
  vector<pii> v;
  vi vx, vy;
  REP(i, n) {
    ld x, y;
    cin >> x >> y;
    vx.push_back(x);
    vy.push_back(y);
    v.emplace_back(x, y);
  }
  compress(vx);
  compress(vy);
  vector<vi> sum(n + 1, vi(n + 1));
  REP(i, n) {
    int x = v[i].first, y = v[i].second;
    int cx = find(ALL(vx), x) - vx.begin();
    int cy = find(ALL(vy), y) - vy.begin();
    sum[cx + 1][cy + 1]++;
  }
  FOR(i, 1, n + 1) FOR(j, 1, n + 1) {
    sum[i][j] += sum[i - 1][j] + sum[i][j - 1] - sum[i - 1][j - 1];
  }

  REP(i, m) {
    ld lx, ly, rx, ry;
    cin >> lx >> ly >> rx >> ry;
    rx++;
    ry++;
    int clx = lower_bound(ALL(vx), lx) - vx.begin();
    int cly = lower_bound(ALL(vy), ly) - vy.begin();
    int crx = lower_bound(ALL(vx), rx) - vx.begin();
    int cry = lower_bound(ALL(vy), ry) - vy.begin();
    cout << sum[crx][cry] - sum[crx][cly] - sum[clx][cry] + sum[clx][cly]
         << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FOR(i, k, n) for (int i = (k); i < (n); i++)
#define REP(i, n) FOR(i, 0, n)
#define ALL(a) a.begin(), a.end()
#define MS(m, v) memset(m, v, sizeof(m))
#define D10 fixed << setprecision(10)
typedef vector<int> vi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
typedef long double ld;
const int MOD = 1000000007;
const int INF = MOD + 1;
const ld EPS = 1e-10;
template <class T> T &chmin(T &a, const T &b) { return a = min(a, b); }
template <class T> T &chmax(T &a, const T &b) { return a = max(a, b); }

/*--------------------template--------------------*/
typedef complex<double> point;
int ans;
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;
  }
  void search(const point &ld, const point &ru) { search_(root, 0, ld, ru); }
  void search_(node *t, int d, const point &ld, const point &ru) {
    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))
      ans++;
    if (!compare(d, p, ld))
      search_(t->l, !d, ld, ru);
    if (!compare(d, ru, p))
      search_(t->r, !d, ld, ru);
  }
};
int main() {
  int n, m;
  cin >> n >> m;
  vector<point> v;
  REP(i, n) {
    ld x, y;
    cin >> x >> y;
    v.emplace_back(x, y);
  }
  random_shuffle(ALL(v));
  kdtree kd;
  REP(i, n) { kd.insert(v[i]); }
  REP(i, m) {
    ld a, b, c, d;
    cin >> a >> b >> c >> d;
    point l(a, b), r(c, d);
    ans = 0;
    kd.search(l, r);
    cout << ans << endl;
  }
  return 0;
}