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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j;
  int n, m, r;
  while (cin >> n >> m >> r, n || m || r) {
    r *= 4;
    int x[100000], y[100000];
    for (i = 0; i < n; ++i)
      cin >> x[i] >> y[i];
    vector<int> a[10000];
    for (i = 0; i < m; ++i) {
      int p, q;
      cin >> p >> q;
      a[q].push_back(p);
    }
    for (i = 0; i < 10000; ++i)
      sort(a[i].begin(), a[i].end());
    int b[90], c[90];
    for (i = -r; i <= r; ++i) {
      for (j = -40; i * i + j * j > r * r; ++j)
        ;
      b[i + 40] = j;
      for (; i * i + j * j <= r * r; ++j)
        ;
      c[i + 40] = j;
    }
    int sm = 0;
    for (i = 0; i < n; ++i) {
      for (j = y[i] - r; j <= y[i] + r; ++j) {
        if (0 <= j && j < 10000)
          sm += lower_bound(a[j].begin(), a[j].end(), x[i] + c[j - y[i] + 40]) -
                lower_bound(a[j].begin(), a[j].end(), x[i] + b[j - y[i] + 40]);
      }
    }
    cout << sm << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <iostream>
#include <list>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
int main() {
  int i, j;
  int m, n, r;
  while (cin >> m >> n >> r, m || n) {
    r *= 4;
    int a[100000], b[100000];
    for (i = 0; i < m; i++)
      cin >> a[i] >> b[i];
    vector<int> c[10001];
    for (i = 0; i < n; i++) {
      int p, q;
      cin >> p >> q;
      c[p].push_back(q);
    }
    for (i = 0; i < 10001; i++) {
      if (c[i].empty() == 0)
        sort(c[i].begin(), c[i].end());
    }
    int sm = 0;
    for (i = 0; i < n; i++) {
      for (j = max(0, a[i] - r); j <= min(10000, a[i] + r); j++) {
        if (c[j].empty() == 0) {
          double p = sqrt(r * r - (a[i] - j) * (a[i] - j));
          int vmn = ceil(b[i] - p);
          int vmx = floor(b[i] + p);
          int nmn = 1, nmx = c[j].size() - 1;
          if (0) {
          } else if (c[j][0] >= vmn) {
            nmn = nmx = 0;
          } else if (c[j][nmx] < vmn) {
            nmn = nmx = c[j].size();
          } else {
            for (;;) {
              if (0) {
              } else if (c[j][(nmn + nmx) / 2 - 1] >= vmn) {
                nmn = (nmn + nmx) / 2 + 1;
              } else if (c[j][(nmn + nmx) / 2] < vmn) {
                nmx = (nmn + nmx) / 2 - 1;
              } else {
                break;
              }
            }
          }
          int xmn = 0, xmx = c[j].size() - 2;
          if (0) {
          } else if (c[j][0] > vmx) {
            xmn = xmx = -1;
          } else if (c[j][c[j].size() - 1] <= vmx) {
            xmn = xmx = c[j].size() - 1;
          } else {
            for (;;) {
              if (0) {
              } else if (c[j][(xmn + xmx) / 2 + 1] <= vmx) {
                xmn = (xmn + xmx) / 2 + 1;
              } else if (c[j][(xmn + xmx) / 2] > vmx) {
                xmx = (xmn + xmx) / 2 - 1;
              } else {
                break;
              }
            }
          }
          sm += (xmn + xmx) / 2 - (nmn + nmx) / 2 + 1;
        }
      }
    }
    cout << sm << endl;
  }
  return 0;
}