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

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const ll INF = 1LL << 50;
const double EPS = 1E-10;

#define rep(i, n) for (int i = 0; i < n; i++)

const int MAX = 10010;
P ACM[100010];
vector<int> ICPC[MAX];
int dis[100];

int main() {
  int AN, BN, R;
  int x, y, d;

  while (cin >> AN >> BN >> R && (AN || BN)) {

    for (int i = 0; i <= 4 * R; i++) {
      int d2 = (16 * R * R - i * i);
      int d = ((sqrt((double)d2)) + 0.5);
      if (d * d > d2)
        d--;
      dis[i] = d;
    }
    rep(i, AN) { cin >> ACM[i].first >> ACM[i].second; }
    int cnt = 0;
    rep(i, BN) {
      cin >> x >> y;
      ICPC[x].push_back(y);
    }

    rep(i, MAX) sort(ICPC[i].begin(), ICPC[i].end());

    rep(i, AN) {
      x = ACM[i].first;
      y = ACM[i].second;
      for (int j = x - 4 * R; j <= x + 4 * R; j++) {
        if (j < 0 || j >= 10000)
          continue;
        d = dis[abs(j - x)];
        cnt += upper_bound(ICPC[j].begin(), ICPC[j].end(), y + d) -
               lower_bound(ICPC[j].begin(), ICPC[j].end(), y - d);
      }
    }
    cout << cnt << endl;
    rep(i, MAX) { ICPC[i].clear(); }
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <algorithm>
#include <cassert>
#include <cctype>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <fstream>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <math.h>
#include <memory.h>
#include <memory>
#include <numeric>
#include <queue>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;

typedef complex<double> xy_t;
typedef pair<xy_t, xy_t> line;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<double, double> Pd;
typedef pair<int, P> PP;
typedef pair<int, PP> PPP;
const ll INF = 1LL << 50;
const double EPS = 1E-10;

#define rep(i, n) for (int i = 0; i < n; i++)

const int MAX = 10010;
P ACM[100010];
P pos[100010];
bool ICPC[MAX][MAX];
int dis[100];

int main() {
  int AN, BN, R;
  int x, y, d;
  memset(ICPC, false, sizeof(ICPC));

  while (cin >> AN >> BN >> R && (AN || BN)) {

    for (int i = 0; i <= 4 * R; i++) {
      int d2 = (16 * R * R - i * i);
      int d = ((sqrt((double)d2)) + 0.5);
      if (d * d > d2)
        d--;
      dis[i] = d;
    }
    rep(i, AN) { cin >> ACM[i].first >> ACM[i].second; }
    int cnt = 0;
    rep(i, BN) {
      cin >> x >> y;
      pos[i] = P(x, y);
      ICPC[x][y] = true;
    }

    rep(i, AN) {
      x = ACM[i].first;
      y = ACM[i].second;
      for (int j = x - 4 * R; j <= x + 4 * R; j++) {
        if (j < 0 || j >= 10000)
          continue;
        d = dis[abs(j - x)];
        for (int k = y - d; k <= y + d; k++) {
          if (k < 0 || k >= 10000)
            continue;
          if (ICPC[j][k])
            cnt++;
        }
      }
    }
    cout << cnt << endl;
    rep(i, BN) { ICPC[pos[i].first][pos[i].second] = false; }
  }
  return 0;
}