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

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

#define EPS (1e-10)
#define EQ(a, b) (abs((a) - (b)) < EPS)

using namespace std;

// int binSearch(vector<int> &v,int cy,int dx,int R){
//	int ub=v.size()-1;
//	int lb=-1;
//	while(ub-lb>1){
//		int mid=ub+lb;
//		mid/=2;
//		if(R-dx>=(cy-v[mid])*(cy-v[mid])){
//			ub=mid;
//		}
//		else{
//			lb=mid;
//		}
//	}
//	return ub;
// }

int main() {
  int an, bn, r;
  int flyX[100001];
  int flyY[100001];
  int px, py;
  vector<int> energy[10001];
  int sum;
  while (cin >> an >> bn >> r && !(an == 0 && bn == 0 && r == 0)) {
    for (int i = 0; i < 10001; i++) {
      energy[i].clear();
    }
    for (int i = 0; i < an; i++) {
      cin >> flyX[i] >> flyY[i];
    }
    int infoMinX = 10000000;
    int infoMaxX = -1;

    for (int i = 0; i < bn; i++) {
      cin >> px >> py;
      energy[px].push_back(py);
    }
    for (int i = 0; i < 10001; i++) {
      sort(energy[i].begin(), energy[i].end());
    }
    sum = 0;
    for (int i = 0; i < an; i++) {
      int minx = max(0, flyX[i] - 4 * r);
      int maxx = min(9999, flyX[i] + 4 * r);
      for (int j = minx; j <= maxx; j++) {
        if (energy[j].empty())
          continue;
        int ovlimit = (int)(sqrt((double)(-((flyX[i] - j) * (flyX[i] - j)) +
                                          16 * r * r)) +
                            flyY[i] + EPS);
        double lolimit2 =
            (-sqrt((double)(-((flyX[i] - j) * (flyX[i] - j)) + 16 * r * r)) +
             flyY[i]);
        int lolimit = (int)(lolimit2 + EPS);
        if (!EQ(lolimit2, (int)lolimit2)) {
          lolimit++;
        }
        sum += (upper_bound(energy[j].begin(), energy[j].end(), ovlimit) -
                lower_bound(energy[j].begin(), energy[j].end(), lolimit));
      }
    }
    cout << sum << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

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

#define EPS (1e-10)

using namespace std;

int main() {

  int an, bn, r;
  vector<pair<int, int>> flys;
  int flyX[100001];
  int flyY[100001];
  int px, py;
  vector<int> energy[10001];
  int sum;
  while (cin >> an >> bn >> r && !(an == 0 && bn == 0 && r == 0)) {
    for (int i = 0; i < 10001; i++) {
      energy[i].clear();
    }
    for (int i = 0; i < an; i++) {
      cin >> flyX[i] >> flyY[i];
    }
    for (int i = 0; i < bn; i++) {
      cin >> px >> py;
      energy[px].push_back(py);
    }
    for (int i = 0; i < 10001; i++) {
      sort(energy[i].begin(), energy[i].end());
    }
    sum = 0;
    for (int i = 0; i < an; i++) {
      int minx = max(0, flyX[i] - 4 * r);
      int maxx = min(9999, flyX[i] + 4 * r);
      for (int j = minx; j <= maxx; j++) {
        int ovlimit =
            (int)(sqrt((double)(-(flyX[i] - j) * (flyX[i] - j) + 16 * r * r) +
                       flyY[i]) +
                  EPS);
        int lolimit =
            (int)(-sqrt((double)(-(flyX[i] - j) * (flyX[i] - j) + 16 * r * r) +
                        flyY[i]) +
                  EPS);
        sum += (upper_bound(energy[j].begin(), energy[j].end(), ovlimit + 1) -
                lower_bound(energy[j].begin(), energy[j].end(), lolimit));
      }
    }
    cout << sum << endl;
  }

  return 0;
}