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

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;
#define EQ(a, b) (abs((a) - (b)) < EPS)
typedef pair<int, int> P;
const double EPS = 1e-10;

void solve() {
  int AN, BN, R;
  while (cin >> AN >> BN >> R, AN || BN || R) {

    vector<P> VecAN(AN);
    vector<int> VecBN[10001];
    for (int i = 0; i < AN; ++i) {
      cin >> VecAN[i].first >> VecAN[i].second;
    }
    int px, py;
    for (int i = 0; i < BN; ++i) {
      cin >> px >> py;
      VecBN[px].push_back(py);
    }
    for (int i = 0; i < 10001; ++i) {
      sort(VecBN[i].begin(), VecBN[i].end());
    }
    int count = 0;
    for (int i = 0; i < AN; ++i) {
      int minx = max(0, VecAN[i].first - 4 * R);
      int maxx = min(10000, VecAN[i].first + 4 * R);
      for (int j = minx; j <= maxx; ++j) {
        if (VecBN[j].empty()) {
          continue;
        }
        double temp = sqrt((
            double)(-(VecAN[i].first - j) * (VecAN[i].first - j) + 16 * R * R));
        int upperlimit = (int)(temp + VecAN[i].second + EPS);
        double lowerlimit2 = -temp + VecAN[i].second;
        int lowerlimit = (int)(lowerlimit2 + EPS);
        if (!EQ(lowerlimit2, (int)lowerlimit2)) {
          lowerlimit++;
        }
        count += upper_bound(VecBN[j].begin(), VecBN[j].end(), upperlimit) -
                 lower_bound(VecBN[j].begin(), VecBN[j].end(), lowerlimit);
      }
    }
    cout << count << endl;
  }
}

int main() {
  solve();
  return (0);
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <vector>

using namespace std;
#define EQ(a, b) (abs((a) - (b)) < EPS)
typedef pair<int, int> P;
const double EPS = 1e-10;

void solve() {
  int AN, BN, R;
  while (cin >> AN >> BN >> R, AN || BN || R) {

    vector<P> VecAN(AN);
    vector<int> VecBN[10001];
    for (int i = 0; i < AN; ++i) {
      cin >> VecAN[i].first >> VecAN[i].second;
    }
    int px, py;
    for (int i = 0; i < BN; ++i) {
      cin >> px >> py;
      VecBN[px].push_back(py);
    }
    for (int i = 0; i < 10001; ++i) {
      sort(VecBN[i].begin(), VecBN[i].end());
    }
    int count = 0;
    for (int i = 0; i < AN; ++i) {
      int minx = max(0, VecAN[i].first - 4 * R);
      int maxx = min(10000, VecAN[i].second + 4 * R);
      for (int j = minx; j <= maxx; ++j) {
        if (VecBN[j].empty()) {
          continue;
        }
        int upperlimit =
            (int)(sqrt((double)(-(VecAN[i].first - j) * (VecAN[i].first - j) +
                                16 * R * R)) +
                  VecAN[i].second + EPS);
        double lowerlimit2 =
            -sqrt((double)(-(VecAN[i].first - j) * (VecAN[i].first - j) +
                           16 * R * R)) +
            VecAN[i].second;
        int lowerlimit = (int)(lowerlimit2 + EPS);
        if (!EQ(lowerlimit2, (int)lowerlimit2)) {
          lowerlimit++;
        }
        count += upper_bound(VecBN[j].begin(), VecBN[j].end(), upperlimit) -
                 lower_bound(VecBN[j].begin(), VecBN[j].end(), lowerlimit);
      }
    }
    cout << count << endl;
  }
}

int main() {
  solve();
  return (0);
}