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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
typedef pair<int, int> P;
double dist(int x, int y) { return sqrt(x * x + y * y); }
int main() {
  int N, M;
  ll R;
  while (cin >> N >> M >> R && N) {
    R *= 4;
    vector<int> combat[10000];
    REP(i, N) {
      int x, y;
      scanf("%d %d", &x, &y);
      combat[x].push_back(y);
    }
    REP(i, 10000) sort(combat[i].begin(), combat[i].end());
    int ans = 0;
    REP(i, M) {
      int x, y;
      scanf("%d %d", &x, &y);
      for (int xx = max(x - R, 0LL); xx <= min(x + R, 9999LL); xx++) {
        int dx = xx - x;
        vector<int>::iterator b =
            lower_bound(combat[xx].begin(), combat[xx].end(),
                        y - sqrt(R * R - dx * dx) - 1);
        vector<int>::iterator e =
            upper_bound(combat[xx].begin(), combat[xx].end(),
                        y + sqrt(R * R - dx * dx) + 1);
        while (b != e) {
          int yy = *b;
          if (dist(x - xx, y - yy) < (double)R + EPS)
            ans++;
          b++;
        }
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
typedef pair<int, int> P;
int main() {
  int N, M;
  double R;
  while (cin >> N >> M >> R && N) {
    R *= 4;
    vector<P> combat(N);
    REP(i, N) { scanf("%d %d", &combat[i].second, &combat[i].first); }
    vector<double> ball[10000];
    REP(i, M) {
      int x, y;
      scanf("%d %d", &x, &y);
      ;
      ball[x].push_back(y);
    }
    REP(i, 10000) sort(ball[i].begin(), ball[i].end());
    int ans = 0;
    REP(i, N) {
      P p = combat[i];
      for (int x = max(0, (int)(p.first - R));
           x <= min(9999, (int)(p.first + R)); x++) {
        ans += distance(lower_bound(ball[x].begin(), ball[x].end(),
                                    -1.0 * sqrt(R * R - x * x)),
                        upper_bound(ball[x].begin(), ball[x].end(),
                                    +1.0 * sqrt(R * R - x * x)));
      }
    }
    cout << ans << endl;
  }
  return 0;
}