Problem p00609 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int MAX = 10000;
int main() {
  int an, bn, r;
  while (cin >> an >> bn >> r, an && bn) {
    vvi a(MAX, vi());
    REP(i, an) {
      int x, y;
      cin >> x >> y;
      a[x].push_back(y);
    }
    REP(i, MAX) { sort(ALL(a[i])); }
    ll ans = 0;
    REP(i, bn) {
      int xb, yb;
      cin >> xb >> yb;
      FOR(dx, -4 * r, 4 * r + 1) {
        int x = xb + dx;
        if (x >= 0 && x < MAX) {
          vi::iterator it = lower_bound(ALL(a[x]), yb - 4 * r);
          for (; it != a[x].end() && *it - yb <= 4 * r; it++) {
            int dy = *it - yb;
            if (sqrt((double)(dx * dx + dy * dy)) < 4 * r + EPS) {
              ans++;
            }
          }
        }
      }
    }
    cout << ans << endl;
  }
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <utility>
#include <vector>
using namespace std;
inline int toInt(string s) {
  int v;
  istringstream sin(s);
  sin >> v;
  return v;
}
template <class T> inline string toString(T x) {
  ostringstream sout;
  sout << x;
  return sout.str();
}
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<string> vs;
typedef pair<int, int> pii;
typedef long long ll;
#define ALL(a) (a).begin(), (a).end()
#define RALL(a) (a).rbegin(), (a).rend()
#define EXIST(s, e) ((s).find(e) != (s).end())
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define REP(i, n) FOR(i, 0, n)
#define EACH(t, i, c) for (t::iterator i = (c).begin(); i != (c).end(); ++i)
const double EPS = 1e-10;
const double PI = acos(-1.0);

int main() {
  int an, bn, r;
  while (cin >> an >> bn >> r, an && bn) {
    static vvi a(10000, vi());
    REP(i, an) {
      int x, y;
      cin >> x >> y;
      a[x].push_back(y);
    }
    REP(i, 10000) { sort(ALL(a[i])); }
    int ans = 0;
    REP(i, bn) {
      int xb, yb;
      cin >> xb >> yb;
      FOR(dx, -2 * r, 2 * r + 1) {
        int x = xb + dx;
        vi::iterator it = lower_bound(ALL(a[x]), yb - 2 * r);
        for (; it != a[x].end() && *it - yb <= 2 * r; it++) {
          int dy = *it - yb;
          if (sqrt((double)(dx * dx + dy * dy)) < 4 * r + EPS) {
            ans++;
          }
        }
      }
    }
    cout << ans << endl;
  }
}