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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>

using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INFTY = 1 << 29;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

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

struct Event {
  Point pos;
  int io;   // 0:in,1:out
  int type; // 0:戦闘機,1:エネルギー弾
  int index;
  Event() {}
  Event(Point p, int io, int t, int i) : pos(p), io(io), type(t), index(i) {}
  bool operator<(const Event &e) const {
    if (pos.x != e.pos.x)
      return pos.x < e.pos.x;
    if (io != e.io)
      return io < e.io;
    return false;
  }
};
ostream &operator<<(ostream &os, const Event &e) {
  printf("((%d,%d),%s,%s[%d])", e.pos.x, e.pos.y, e.io ? "out" : "in",
         e.type ? "bs" : "as", e.index);
  return os;
}

int main() {
  // cin.tie(0);
  // ios_base::sync_with_stdio(false);

  for (int m, n, r; cin >> m >> n >> r, m | n;) {
    vector<Event> es;
    {
      vector<Point> as(m), bs(n);
      rep(i, m) cin >> as[i].x >> as[i].y;
      rep(i, n) cin >> bs[i].x >> bs[i].y;

      rep(i, m) {
        es.emplace_back(Point(as[i].x - 2 * r, as[i].y), 0, 0, i);
        es.emplace_back(Point(as[i].x + 2 * r, as[i].y), 1, 0, i);
      }
      rep(i, n) {
        es.emplace_back(Point(bs[i].x - 2 * r, bs[i].y), 0, 1, i);
        es.emplace_back(Point(bs[i].x + 2 * r, bs[i].y), 1, 1, i);
      }
      sort(all(es));
    }

    set<Point> as, bs;
    int res = 0;
    for (Event &e : es) {
      if (e.io == 0) {
        e.pos.x += 2 * r;
        if (e.type == 0) {
          auto i = bs.lower_bound(Point(-INFTY, e.pos.y - 4 * r));
          auto j = bs.upper_bound(Point(+INFTY, e.pos.y + 4 * r));
          for (; i != j; ++i) {
            int dx = abs(i->x - e.pos.x), dy = abs(i->y - e.pos.y);
            if (dx * dx + dy * dy <= 4 * r * 4 * r)
              res++;
          }
          as.insert(e.pos);
        } else {
          auto i = as.lower_bound(Point(-INFTY, e.pos.y - 4 * r));
          auto j = as.upper_bound(Point(+INFTY, e.pos.y + 4 * r));
          for (; i != j; ++i) {
            int dx = abs(i->x - e.pos.x), dy = abs(i->y - e.pos.y);
            if (dx * dx + dy * dy <= 4 * r * 4 * r)
              res++;
          }
          bs.insert(e.pos);
        }
      } else {
        e.pos.x -= 2 * r;
        if (e.type == 0)
          as.erase(e.pos);
        else
          bs.erase(e.pos);
      }
    }
    cout << res << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <climits>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>

using namespace std;

#define dump(n) cout << "# " << #n << '=' << (n) << endl
#define repi(i, a, b) for (int i = int(a); i < int(b); i++)
#define peri(i, a, b) for (int i = int(b); i-- > int(a);)
#define rep(i, n) repi(i, 0, n)
#define per(i, n) peri(i, 0, n)
#define iter(c) __typeof__((c).begin())
#define foreach(i, c) for (iter(c) i = (c).begin(); i != (c).end(); ++i)
#define all(c) (c).begin(), (c).end()
#define mp make_pair

typedef unsigned int uint;
typedef long long ll;
typedef unsigned long long ull;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<double> vd;
typedef vector<vd> vvd;
typedef vector<string> vs;
typedef pair<int, int> pii;

const int INFTY = 1 << 29;
const double EPS = 1e-9;

template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << '(' << p.first << ',' << p.second << ')';
}
template <typename T> ostream &operator<<(ostream &os, const vector<T> &a) {
  os << '[';
  rep(i, a.size()) os << (i ? " " : "") << a[i];
  return os << ']';
}

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

struct Event {
  Point pos;
  int io;   // 0:in,1:out
  int type; // 0:戦闘機,1:エネルギー弾
  int index;
  Event() {}
  Event(Point p, int io, int t, int i) : pos(p), io(io), type(t), index(i) {}
  bool operator<(const Event &e) const {
    if (pos.x != e.pos.x)
      return pos.x < e.pos.x;
    if (io != e.io)
      return io < e.io;
    return false;
  }
};
ostream &operator<<(ostream &os, const Event &e) {
  printf("((%d,%d),%s,%s[%d])", e.pos.x, e.pos.y, e.io ? "out" : "in",
         e.type ? "bs" : "as", e.index);
  return os;
}

int main() {
  // cin.tie(0);
  // ios_base::sync_with_stdio(false);

  for (int m, n, r; cin >> m >> n >> r, m | n | r;) {
    vector<Event> es;
    {
      vector<Point> as(m), bs(n);
      rep(i, m) cin >> as[i].x >> as[i].y;
      rep(i, n) cin >> bs[i].x >> bs[i].y;

      rep(i, m) {
        es.emplace_back(Point(as[i].x - r, as[i].y), 0, 0, i);
        es.emplace_back(Point(as[i].x + r, as[i].y), 1, 0, i);
      }
      rep(i, n) {
        es.emplace_back(Point(bs[i].x - r, bs[i].y), 0, 1, i);
        es.emplace_back(Point(bs[i].x + r, bs[i].y), 1, 1, i);
      }
      sort(all(es));
    }

    set<Point> as, bs;
    int res = 0;
    for (Event &e : es) {
      if (e.io == 0) {
        if (e.type == 0) {
          auto i = bs.lower_bound(Point(e.pos.x, -INFTY));
          auto j = bs.lower_bound(Point(e.pos.x, INFTY));
          for (; i != j; ++i) {
            int dx = abs(i->x - e.pos.x), dy = abs(i->y - e.pos.y);
            if (dx * dx + dy * dy <= 4 * r * 4 * r)
              res++;
          }
          e.pos.x += r;
          as.insert(e.pos);
        } else {
          auto i = as.lower_bound(Point(e.pos.x, -INFTY));
          auto j = as.lower_bound(Point(e.pos.x, INFTY));
          for (; i != j; ++i) {
            int dx = abs(i->x - e.pos.x), dy = abs(i->y - e.pos.y);
            if (dx * dx + dy * dy <= 4 * r * 4 * r)
              res++;
          }
          e.pos.x += r;
          bs.insert(e.pos);
        }
      } else {
        if (e.type == 0)
          as.erase(e.pos);
        else
          bs.erase(e.pos);
      }
    }
    cout << res << endl;
  }

  return 0;
}