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

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

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;

typedef pair<double, pair<int, int>> pp;

int main() {
  int an, bn, r;
  while (scanf("%d%d%d", &an, &bn, &r), an + bn + r) {
    int ans = 0;

    vector<pp> p(an);

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

      p[i].f = sqrt((double)(x * x + y * y));
      p[i].s.f = x;
      p[i].s.s = y;
    }

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

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

      double rmin = sqrt((double)(x * x + y * y)) - r * 4 - 0.1;
      double rmax = sqrt((double)(x * x + y * y)) + r * 4 + 0.1;
      vector<pp>::iterator now =
          lower_bound(p.begin(), p.end(), pp(rmin, mp(INT_MIN, INT_MIN)));
      vector<pp>::iterator end =
          upper_bound(p.begin(), p.end(), pp(rmax, mp(INT_MAX, INT_MAX)));
      while (now != end) {
        int xx = now->s.f, yy = now->s.s;
        if ((x - xx) * (x - xx) + (y - yy) * (y - yy) <= 4 * r * 4 * r)
          ans++;
        now++;
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

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

#define f first
#define s second
#define mp make_pair

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, c)                                                              \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)
#define ALL(c) (c).begin(), (c).end()

using namespace std;

typedef unsigned int uint;
typedef long long ll;
typedef complex<int> P; /* ツ点 */

/*
 * 2Dtree
 */
class Rect {
public:
  P lu;
  P rd;
  Rect(P a, P b) {
    int lux, luy, rdx, rdy;
    lux = std::min(a.real(), b.real());
    luy = std::min(a.imag(), b.imag());
    rdx = std::max(a.real(), b.real());
    rdy = std::max(a.imag(), b.imag());
    lu = P(lux, luy);
    rd = P(rdx, rdy);
  }
};

bool insiderect(P p, const Rect &range) {
  int r = (range.rd.real() - range.lu.real()) / 2;
  int x = range.lu.real() + r;
  int y = range.lu.imag() + r;
  return (p.real() - x) * (p.real() - x) + (p.imag() - y) * (p.imag() - y) <=
         r * r;
}

class Tree2D {
  struct node {
    P p;
    node *l;
    node *r;
  };

  node *pool;
  int idx;
  node *z, *head;
  P dammy;

  int searchr(node *t, Rect range, int d) {
    int t1, t2, tx1, tx2, ty1, ty2, count = 0;
    if (t == z)
      return 0;
    tx1 = range.lu.real() < t->p.real();
    tx2 = t->p.real() <= range.rd.real();
    ty1 = range.lu.imag() < t->p.imag();
    ty2 = t->p.imag() <= range.rd.imag();
    t1 = d ? tx1 : ty1;
    t2 = d ? tx2 : ty2;
    if (t1)
      count += searchr(t->l, range, !d);
    if (insiderect(t->p, range))
      count++;
    if (t2)
      count += searchr(t->r, range, !d);
    return count;
  }
  node *getNode() { return &pool[idx++]; }

public:
  Tree2D(int n) {
    pool = new node[n + 10];
    idx = 0;
    head = getNode();
    head->p = P(INT_MIN, INT_MIN);
    head->l = head->r = z;
  }
  ~Tree2D() { delete[] pool; }
  void insert(P p) {
    node *f, *t;
    int d, td;
    for (d = 0, t = head; t != z; d != d) {
      td = d ? (p.real() < t->p.real()) : (p.imag() < t->p.imag());
      f = t;
      t = td ? t->l : t->r;
    }
    t = getNode();
    t->p = p;
    t->l = z;
    t->r = z;
    if (td)
      f->l = t;
    else
      f->r = t;
  }
  /*
   * rangeツづ可甘慊づ慊づェツづゥツ点ツづ個青板づーツ青板つヲツづゥ
   */
  int search(Rect range) {
    if (!head)
      return 0;
    return searchr(head->r, range, 1);
  }
};

int main() {
  int an, bn, r;
  while (scanf("%d%d%d", &an, &bn, &r), an + bn + r) {
    Tree2D t(an);
    int ans = 0;
    REP(i, an) {
      int x, y;
      scanf("%d%d", &x, &y);
      t.insert(P(x, y));
    }

    REP(i, bn) {
      int x, y;
      scanf("%d%d", &x, &y);
      ans += t.search(Rect(P(x - 4 * r, y - 4 * r), P(x + 4 * r, y + 4 * r)));
    }

    printf("%d", ans);
  }
  return 0;
}