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

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

struct Point {
  ll x;
  ll y;
  int type;
  Point() { ; }
  Point(double x, double y, int type) : x(x), y(y), type(type) { ; }
  bool operator<(const Point &rhs) const {
    if (fabs(y - rhs.y) > EPS) {
      return y < rhs.y;
    }
    if (fabs(x - rhs.x) > EPS) {
      return x < rhs.x;
    }
    return false;
  }
};

struct Event {
  int x;
  Point p;
  int in;
  Event() { ; }
  Event(int x, Point p, int in) : x(x), p(p), in(in) { ; }
  bool operator<(const Event &rhs) const {
    if (x != rhs.x) {
      return x < rhs.x;
    }
    return in > rhs.in;
  }
};

inline ll square(ll x) { return x * x; }

inline ll dist2(const Point &a, const Point &b) {
  return square(a.x - b.x) + square(a.y - b.y);
}

int an, bn, n;
ll r;
Event event[500000];

int PlaneSweep() {
  int ans = 0;
  set<Point> open;
  REP(iter, n) {
    Event &e = event[iter];
    Point &p = e.p;
    if (e.in) {
      set<Point>::iterator lower =
          open.lower_bound(Point(0, p.y - 4.0 * r - 1, 0));
      set<Point>::iterator upper =
          open.upper_bound(Point(0, p.y + 4.0 * r + 1, 0));
      for (set<Point>::iterator it = lower; it != upper; it++) {
        if (dist2(*it, p) <= 16.0 * r * r) {
          ans += p.type ^ it->type;
        }
      }
      open.insert(p);
    } else {
      open.erase(open.find(p));
    }
  }
  return ans;
}

int main() {
  while (scanf("%d %d %lld", &an, &bn, &r), an | bn) {
    n = (an + bn) * 2;
    REP(i, an) {
      ll x, y;
      scanf("%lld %lld", &x, &y);
      event[2 * i + 0] = Event(x - 4 * r, Point(x, y, 0), 1);
      event[2 * i + 1] = Event(x, Point(x, y, 0), 0);
    }
    REP(i, bn) {
      ll x, y;
      scanf("%lld %lld", &x, &y);
      event[2 * an + 2 * i + 0] = Event(x - 4 * r, Point(x, y, 1), 1);
      event[2 * an + 2 * i + 1] = Event(x, Point(x, y, 1), 0);
    }
    sort(event, event + n);
    int ans = PlaneSweep();
    printf("%d\\n", ans);
  }
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <iostream>
#include <math.h>
#include <set>
#include <stdio.h>
#include <string.h>
#include <vector>

using namespace std;
typedef long long ll;
static const double EPS = 1e-9;
static const double PI = acos(-1.0);

#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define FOR(i, s, n) for (int i = (s); i < (int)(n); i++)
#define FOREQ(i, s, n) for (int i = (s); i <= (int)(n); i++)
#define FORIT(it, c)                                                           \\
  for (__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
#define MEMSET(v, h) memset((v), h, sizeof(v))

struct Point {
  ll x;
  ll y;
  int type;
  Point() { ; }
  Point(double x, double y, int type) : x(x), y(y), type(type) { ; }
  bool operator<(const Point &rhs) const {
    if (fabs(y - rhs.y) > EPS) {
      return y < rhs.y;
    }
    if (fabs(x - rhs.x) > EPS) {
      return x < rhs.x;
    }
    return false;
  }
};

struct Event {
  int x;
  Point p;
  int in;
  Event() { ; }
  Event(int x, Point p, int in) : x(x), p(p), in(in) { ; }
  bool operator<(const Event &rhs) const { return x < rhs.x; }
};

inline ll square(ll x) { return x * x; }

inline ll dist2(const Point &a, const Point &b) {
  return square(a.x - b.x) + square(a.y - b.y);
}

int an, bn, n;
ll r;
Event event[500000];

int PlaneSweep() {
  int ans = 0;
  set<Point> open;
  REP(iter, n) {
    Event e = event[iter];
    Point p = e.p;
    set<Point>::iterator lower =
        open.lower_bound(Point(0, p.y - 4.0 * r - 1, 0));
    set<Point>::iterator upper =
        open.upper_bound(Point(0, p.y + 4.0 * r + 1, 0));
    for (set<Point>::iterator it = lower; it != upper; it++) {
      if (dist2(*it, p) <= 16.0 * r * r) {
        ans += p.type ^ it->type;
      }
    }
    if (e.in) {
      open.insert(p);
    } else {
      open.erase(open.find(p));
    }
  }
  return ans / 2;
}

int main() {
  while (scanf("%d %d %lld", &an, &bn, &r), an | bn) {
    n = (an + bn) * 2;
    REP(i, an) {
      ll x, y;
      scanf("%lld %lld", &x, &y);
      event[2 * i + 0] = Event(x - 2 * r - 1, Point(x, y, 0), 1);
      event[2 * i + 1] = Event(x + 2 * r + 1, Point(x, y, 0), 0);
    }
    REP(i, bn) {
      ll x, y;
      scanf("%lld %lld", &x, &y);
      event[2 * an + 2 * i + 0] = Event(x - 2 * r - 1, Point(x, y, 1), 1);
      event[2 * an + 2 * i + 1] = Event(x + 2 * r + 1, Point(x, y, 1), 0);
    }
    sort(event, event + n);
    int ans = PlaneSweep();
    printf("%d\\n", ans);
  }
}