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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 28)
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define pow2(a) ((a) * (a))
#define MAX 100010
using namespace std;
struct Point {
  double x, y;
  Point(double x = -inf, double y = -inf) : x(x), y(y) {}
  bool operator<(const Point &a) const {
    return !equals(x, a.x) ? x < a.x : y < a.y;
  }
};
int AN, BN, R;
Point ps[MAX];
int dx[] = {0, 0, 1, 0, -1, 1, +1, -1, -1};
int dy[] = {0, 1, 0, -1, 0, 1, -1, -1, +1};

int main() {

  while (1) {

    scanf("%d %d %d", &AN, &BN, &R);
    if (!(AN | BN))
      break;
    rep(i, AN) scanf("%lf %lf", &ps[i].x, &ps[i].y);
    vector<Point> G[251][251];
    {
      Point p;
      rep(i, BN) {
        scanf("%lf %lf", &p.x, &p.y);
        G[(int)p.y / 40][(int)p.x / 40].push_back(p);
      }
    }
    double r = R;
    int power = 0;
    rep(i, AN) {
      int index_y = (int)ps[i].y / 40;
      int index_x = (int)ps[i].x / 40;
      rep(k, 9) {
        index_x += dx[k];
        index_y += dy[k];
        if (!(0 <= index_x && index_x <= 250 && 0 <= index_y && index_y <= 250))
          goto Skip;
        rep(j, G[index_y][index_x].size()) {
          Point p = G[index_y][index_x][j];
          double dist = sqrt(pow2(ps[i].x - p.x) + pow2(ps[i].y - p.y)) - 2 * r;
          if (equals(dist, 2.0 * r) || dist < 2.0 * r)
            power++;
        }
      Skip:;
        index_x -= dx[k];
        index_y -= dy[k];
      }
    }
    printf("%d\\n", power);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iomanip>
#include <iostream>
#include <vector>
#define REP(i, s, n) for (int i = s; i < n; i++)
#define rep(i, n) REP(i, 0, n)
#define inf (1 << 28)
#define EPS (1e-10)
#define equals(a, b) (fabs((a) - (b)) < EPS)
#define pow2(a) ((a) * (a))
#define MAX 100010
using namespace std;
struct Point {
  double x, y;
  Point(double x = -inf, double y = -inf) : x(x), y(y) {}
  bool operator<(const Point &a) const {
    return !equals(x, a.x) ? x < a.x : y < a.y;
  }
};
int AN, BN, R;
Point ps[MAX];
int dx[] = {0, 0, 1, 0, -1, 1, +1, -1, -1};
int dy[] = {0, 1, 0, -1, 0, 1, -1, -1, +1};
vector<Point> G[251][251];

int main() {

  while (1) {
    rep(i, 251) rep(j, 251) G[i][j].clear();
    scanf("%d %d %d", &AN, &BN, &R);
    if (!(AN | BN | R))
      break;
    rep(i, AN) scanf("%lf %lf", &ps[i].x, &ps[i].y);

    {
      Point p;
      rep(i, BN) {
        scanf("%lf %lf", &p.x, &p.y);
        G[(int)p.y / 40][(int)p.x / 40].push_back(p);
      }
    }
    double r = R;
    int power = 0;
    rep(i, AN) {
      int index_y = (int)ps[i].y / 40;
      int index_x = (int)ps[i].x / 40;
      rep(k, 9) {
        index_x += dx[k];
        index_y += dy[k];
        if (!(0 <= index_x && index_x <= 250 && 0 <= index_y && index_y <= 250))
          goto Skip;
        rep(j, G[index_y][index_x].size()) {
          Point p = G[index_y][index_x][j];
          double dist = sqrt(pow2(ps[i].x - p.x) + pow2(ps[i].y - p.y)) - 2 * r;
          if (equals(dist, r) || dist < r)
            power++;
        }
      Skip:;
        index_x -= dx[k];
        index_y -= dy[k];
      }
    }
    printf("%d\\n", power);
  }
  return 0;
}