Problem p00609 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

const int N = 100002, kUnit = 250, kB = 10000 / kUnit;
int AN, BN, R;
int xa[N], ya[N], xb[N], yb[N];

typedef pair<int, int> P;
vector<P> sep[kB + 2][kB + 2];

bool input() {
  scanf("%d%d%d", &AN, &BN, &R);
  if (AN == 0)
    return false;
  rep(i, AN) scanf("%d%d", xa + i, ya + i);
  rep(i, BN) scanf("%d%d", xb + i, yb + i);
  return true;
}

int solve() {
  // バケット初期化
  rep(i, kB) rep(j, kB) sep[i][j].clear();

  // 弾をバケットに放り込む
  rep(i, BN) {
    int x = xb[i] / kUnit, y = yb[i] / kUnit;
    sep[y][x].push_back(P(xb[i], yb[i]));
  }

  int ans = 0;
  rep(i, AN) {
    int x = xa[i], y = ya[i];
    int ix = x / kUnit, iy = y / kUnit;
    for (int dy = -1; dy <= 1; ++dy) {
      for (int dx = -1; dx <= 1; ++dx) {
        int tix = ix + dx, tiy = iy + dy;
        if (0 > tix || 0 > tiy || tix >= kB || tiy >= kB)
          continue;
        // 同じバケット内の弾と比較
        rep(j, sep[tiy][tix].size()) {
          int dist_x = sep[tiy][tix][j].first - x,
              dist_y = sep[tiy][tix][j].second - y;
          int dist_pow = (dist_x * dist_x) + (dist_y * dist_y);
          if (16 * R * R >= dist_pow)
            ++ans;
        }
      }
    }
  }
  return ans;
}

int main() {
  while (input()) {
    printf("%d\n", solve());
  }
  return 0;
}

Fixed submission:
#include <algorithm>
#include <cctype>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;

#define rep(i, n) for (int(i) = 0; (i) < (int)(n); ++(i))
#define foreach(c, i)                                                          \
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); i++)

const int N = 100002, kUnit = 50, kB = 10000 / kUnit;
int AN, BN, R;
int xa[N], ya[N], xb[N], yb[N];

typedef pair<int, int> P;
vector<P> sep[kB + 2][kB + 2];

bool input() {
  scanf("%d%d%d", &AN, &BN, &R);
  if (AN == 0)
    return false;
  rep(i, AN) scanf("%d%d", xa + i, ya + i);
  rep(i, BN) scanf("%d%d", xb + i, yb + i);
  return true;
}

int solve() {
  // バケット初期化
  rep(i, kB) rep(j, kB) sep[i][j].clear();

  // 弾をバケットに放り込む
  rep(i, BN) {
    int x = xb[i] / kUnit, y = yb[i] / kUnit;
    sep[y][x].push_back(P(xb[i], yb[i]));
  }

  int ans = 0;
  rep(i, AN) {
    int x = xa[i], y = ya[i];
    int ix = x / kUnit, iy = y / kUnit;
    for (int dy = -1; dy <= 1; ++dy) {
      for (int dx = -1; dx <= 1; ++dx) {
        int tix = ix + dx, tiy = iy + dy;
        if (0 > tix || 0 > tiy || tix >= kB || tiy >= kB)
          continue;
        // 同じバケット内の弾と比較
        rep(j, sep[tiy][tix].size()) {
          int dist_x = sep[tiy][tix][j].first - x,
              dist_y = sep[tiy][tix][j].second - y;
          int dist_pow = (dist_x * dist_x) + (dist_y * dist_y);
          if (16 * R * R >= dist_pow)
            ++ans;
        }
      }
    }
  }
  return ans;
}

int main() {
  while (input()) {
    printf("%d\n", solve());
  }
  return 0;
}