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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#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()
const int INF = 1 << 29;

int ax[100000], ay[100000];
int main() {
  int an, bn, r;
  while (cin >> an >> bn >> r, an) {
    vector<int> X[10000];

    REP(i, an)
    cin >> ax[i] >> ay[i];
    REP(i, bn) {
      int x, y;
      cin >> x >> y;
      X[y].push_back(x);
    }
    REP(y, 10000)
    sort(ALL(X[y]));
    int dx[8 * r + 1];

    REP(i, 8 * r + 1) {
      int dyy = i - 4 * r;
      dx[i] = (int)sqrt(16 * r * r - dyy * dyy);
      //      cout << i << " " <<dx[i] << endl;
    }

    int res = 0;
    REP(i, an) {
      for (int y = max(0, ay[i] - 4 * r); y <= ay[i] + 4 * r; ++y) {
        //        cout << y <<":"<< endl;
        if (y >= 10000)
          break;
        res += upper_bound(ALL(X[y]), ax[i] + dx[y - ay[i] + 4 * r]) -
               lower_bound(ALL(X[y]), ax[i] - dx[y - ay[i] + 4 * r]);
      }
    }
    cout << res << endl;
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
using namespace std;

#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()
const int INF = 1 << 29;

typedef complex<double> P;

struct event {
  double x;
  int type;
  P p;
  bool af;
  event(double x, int type, const P &p, bool af)
      : x(x), type(type), p(p), af(af) {}
};
const bool operator<(const event &a, const event &b) {
  return a.x != b.x ? a.x > b.x : a.type > b.type;
}
struct P2 {
  int x, y;
  bool af;
  P2(int x, int y, bool af) : x(x), y(y), af(af) {}
};
const bool operator<(const P2 &a, const P2 &b) {
  return a.y != b.y ? a.y < b.y : a.x < b.y;
}

int main() {
  int an, bn, r;
  while (cin >> an >> bn >> r, an) {
    priority_queue<event> Q;

    REP(i, an) {
      int x, y;
      cin >> x >> y;
      Q.push(event(x - 2 * r, 0, P(x, y), 1));
      Q.push(event(x + 2 * r, 1, P(x, y), 1));
    }
    REP(i, bn) {
      int x, y;
      cin >> x >> y;
      Q.push(event(x - 2 * r, 0, P(x, y), 0));
      Q.push(event(x + 2 * r, 1, P(x, y), 0));
    }

    set<P2> T;
    int res = 0;
    while (!Q.empty()) {
      event e = Q.top();
      Q.pop();
      //  cout << e.x << " " << e.p << "  a? = " << e.af <<  endl;
      if (e.type == 0) {
        for (set<P2>::iterator it =
                 lower_bound(ALL(T), P2(-INF, e.p.imag() - 2 * r, 0));
             it != upper_bound(ALL(T), P2(INF, e.p.imag() + 2 * r, 0)); ++it) {
          //        FOR(it, T) {
          //  cout << it->x << " " << it->y << "  a? = " << it->af <<  endl;
          if (e.af != it->af && abs(P(it->x, it->y) - e.p) < 4 * r)
            res++;
        }
        T.insert(P2(real(e.p), imag(e.p), e.af));
      } else
        T.erase(P2(real(e.p), imag(e.p), e.af));
    }
    cout << res << endl;
  }
}