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

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;

int const Max = 10010;
set<int> ys[Max];

constexpr int sq(int x) { return x * x; }

int main() {
  for (int N, M, R; cin >> N >> M >> R && N;) {
    rep(i, Max) ys[i].clear();

    rep(i, N) {
      int x, y;
      scanf("%d%d", &x, &y);
      ys[x].insert(y);
    }

    int ans = 0;
    for (; M--;) {
      int x, y;
      scanf("%d%d", &x, &y);
      REP(px, max(0, x - 4 * R), min(Max, x + 4 * R + 1)) {
        const int py = y - 4 * R;
        auto iter = ys[px].lower_bound(py);
        while (iter != ys[px].end() && *iter <= y + 4 * R) {
          ans += sq(px - x) + sq(*iter - y) <= 16 * R * R;
          iter++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstring>
#include <deque>
#include <fstream>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <unordered_map>
#include <unordered_set>
#include <vector>

using namespace std;

#define REP(i, a, b) for (int i = a; i < (int)b; i++)
#define rep(i, n) REP(i, 0, n)
#define all(c) (c).begin(), (c).end()
#define zero(a) memset(a, 0, sizeof a)
#define minus(a) memset(a, -1, sizeof a)
#define watch(a)                                                               \\
  { cout << #a << " = " << a << endl; }
template <class T1, class T2> inline bool minimize(T1 &a, T2 b) {
  return b < a && (a = b, 1);
}
template <class T1, class T2> inline bool maximize(T1 &a, T2 b) {
  return a < b && (a = b, 1);
}

typedef long long ll;
int const inf = 1 << 29;
#define sq(x) ((x) * (x))

struct Circle {
  int x, y, r;
};

bool operator<(const Circle &a, const Circle &b) {
  return a.x != b.x ? a.x < b.x : a.y < b.y;
}

pair<int, int> as[100001], bs[100001];

const int BSize = 320;

vector<int> B1[BSize][BSize];
vector<int> B2[BSize][BSize];

int main() {

  for (int AN, BN, RR; cin >> AN >> BN >> RR && (AN | BN | RR);) {
    rep(i, BSize) rep(j, BSize) B1[i][j].clear(), B2[i][j].clear();
    rep(i, AN) {
      int x, y;
      scanf("%d%d", &x, &y);
      as[i] = {x, y};
      B1[y / BSize][x / BSize].push_back(i);
    }

    rep(i, BN) {
      int x, y;
      scanf("%d%d", &x, &y);
      bs[i] = {x, y};
      B2[y / BSize][x / BSize].push_back(i);
    }

    int res = 0;

    rep(i, BSize) rep(j, BSize) {
      for (auto aidx : B1[i][j]) {
        REP(i2, max(0, i - 1), min(BSize, i + 2))
        REP(j2, max(0, j - 1), min(BSize, j + 2)) {
          for (auto bidx : B2[i2][j2]) {
            if (sq(as[aidx].first - bs[bidx].first) +
                    sq(as[aidx].second - bs[bidx].second) <=
                16 * RR * RR) {
              res++;
            }
          }
        }
      }
    }

    cout << res << endl;
  }

  return 0;
}