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

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-6;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

double x[200000], y[200000];
double r;

bool Same(int a, int b) {
  return (x[a] - x[b]) * (x[a] - x[b]) + (y[a] - y[b]) * (y[a] - y[b]) <=
         r * r + EPS;
}

void Solve() {
  int n;
  scanf("%d%lf", &n, &r);

  vector<pair<double, int>> es;
  rep(i, 0, n) {
    scanf("%lf%lf", x + i, y + i);
    es.push_back(mp(x[i], i));
    es.push_back(mp(x[i] + r + EPS, i + n));
  }
  sort(es.begin(), es.end());

  set<pair<double, int>> s;
  vector<int> res;
  rep(i, 0, es.size()) {
    int id = es[i].second % n;
    if (es[i].second < n) {
      auto it = s.lower_bound(mp(y[id], id));
      if (it != s.end() && Same(id, it->second))
        continue;
      if (it != s.begin() && Same(id, (--it)->second))
        continue;
      res.push_back(id);
      s.insert(mp(y[id], id));
    } else
      s.erase(mp(y[id], id));
  }

  printf("%d\\n", res.size());
}

int main() {
  Solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

#define rep(i, x, y) for (int i = (x); i < (y); ++i)
#define mp(a, b) make_pair((a), (b))
#define debug(x) #x << "=" << (x)

#ifdef DEBUG
#define _GLIBCXX_DEBUG
#define dump(x) std::cerr << debug(x) << " (L:" << __LINE__ << ")" << std::endl
#else
#define dump(x)
#endif

typedef long long int ll;
typedef pair<int, int> pii;
// template<typename T> using vec=std::vector<T>;

const int INF = 1 << 30;
const long long int INF_ = 1LL << 58;
const double EPS = 1e-6;
const int dx[] = {1, 0, -1, 0}, dy[] = {0, 1, 0, -1};

template <typename T> ostream &operator<<(ostream &os, const vector<T> &vec) {
  os << "[";
  for (const auto &v : vec) {
    os << v << ",";
  }
  os << "]";
  return os;
}

struct UnionFind {
  int sz;
  vector<int> rank, par;

  UnionFind(int n) {
    sz = n;
    rank.assign(sz, 0);
    par.assign(sz, 0);
    rep(i, 0, sz) par[i] = i;
  }

  int Find(int x) {
    if (par[x] == x)
      return x;
    return par[x] = Find(par[x]);
  }

  void Unite(int x, int y) {
    x = Find(x);
    y = Find(y);
    if (x == y)
      return;

    if (rank[x] < rank[y]) {
      par[x] = y;
    } else {
      par[y] = x;
      if (rank[x] == rank[y])
        ++rank[x];
    }
    --sz;
  }
};

void Solve() {
  int n;
  double r;
  cin >> n >> r;

  vector<pair<double, int>> xs(n);
  vector<double> ys(n);

  rep(i, 0, n) {
    double x, y;
    cin >> x >> y;
    xs[i] = mp(x, i);
    ys[i] = y;
  }
  sort(xs.begin(), xs.end());

  vector<bool> done(n);
  UnionFind uf(n);
  rep(i, 0, n) {
    if (done[xs[i].second])
      continue;
    double x1 = xs[i].first, y1 = ys[xs[i].second];
    rep(j, i + 1, n) {
      double x2 = xs[j].first, y2 = ys[xs[j].second];
      if (x2 - x1 - EPS > r)
        continue;
      if ((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1) <= r * r + EPS) {
        uf.Unite(xs[i].second, xs[j].second);
        done[xs[j].second] = true;
      }
    }
  }

  cout << uf.sz << endl;
}

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(0);
  Solve();
  return 0;
}