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

#include <cassert>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <vector>

#define TYPE_A 1
#define TYPE_B 2

using namespace std;

typedef pair<int, int> ipoint;

#define foreach(it, o)                                                         \\
  for (__typeof((o).begin()) it = (o).begin(); it != (o).end(); ++it)

int main() {
  while (true) {
    int ans = 0;
    map<int, set<int>> Xa;
    map<int, set<int>> Xb;

    int AN, BN, R;
    scanf("%d%d%d", &AN, &BN, &R);

    if (AN == 0 && BN == 0)
      break;

    for (int i = 0; i < AN; ++i) {
      int xa, ya;
      scanf("%d%d", &xa, &ya);
      Xa[xa].insert(ya);
    }

    for (int i = 0; i < BN; ++i) {
      int xb, yb;
      scanf("%d%d", &xb, &yb);
      Xb[xb].insert(yb);
    }

    foreach (itxa, Xa) {
      foreach (itxas, itxa->second) {
        int X = itxa->first;
        int Y = *itxas;
        int stx = X - 4 * R - 1;
        int edx = X + 4 * R + 1;
        int sty = Y - 4 * R - 1;
        int edy = Y + 4 * R + 1;

        set<int> tset;
        map<int, set<int>>::iterator itstx = Xb.lower_bound(stx);
        map<int, set<int>>::iterator itedx = Xb.upper_bound(edx);

        for (map<int, set<int>>::iterator itm = itstx; itm != itedx; ++itm) {
          // if( 0 <= itm->first && itm->first < 10000 ){
          int dx = X - itm->first;
          dx *= dx;
          set<int>::iterator itsty = itm->second.lower_bound(sty);
          set<int>::iterator itedy = itm->second.upper_bound(edy);

          for (set<int>::iterator its = itsty; its != itedy; ++its) {
            int dy = Y - *its;
            int d = dx + dy * dy; // - 4*R*R;
            if (d <= 16 * R * R)
              ++ans;
          }
          //}
        }
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <cassert>
#include <complex>
#include <cstdio>
#include <iostream>
#include <map>
#include <set>
#include <vector>

#define TYPE_A 1
#define TYPE_B 2

using namespace std;

typedef pair<int, int> ipoint;

#define foreach(it, o)                                                         \\
  for (__typeof((o).begin()) it = (o).begin(); it != (o).end(); ++it)

int main() {
  while (true) {
    int ans = 0;
    map<int, set<int>> Xa;
    map<int, set<int>> Xb;

    int AN, BN, R, _4R2, _4R;
    scanf("%d%d%d", &AN, &BN, &R);

    if (AN == 0 && BN == 0)
      break;

    for (int i = 0; i < AN; ++i) {
      int xa, ya;
      scanf("%d%d", &xa, &ya);
      Xa[xa].insert(ya);
    }

    for (int i = 0; i < BN; ++i) {
      int xb, yb;
      scanf("%d%d", &xb, &yb);
      Xb[xb].insert(yb);
    }

    foreach (itxa, Xa) {
      foreach (itxas, itxa->second) {
        int X = itxa->first;
        int Y = *itxas;
        int stx = X - 4 * R - 1;
        int edx = X + 4 * R + 1;
        int sty = Y - 4 * R - 1;
        int edy = Y + 4 * R + 1;

        set<int> tset;
        pair<map<int, set<int>>::iterator, bool> itxst =
            Xb.insert(make_pair(stx, tset));
        pair<map<int, set<int>>::iterator, bool> itxed =
            Xb.insert(make_pair(edx, tset));

        for (map<int, set<int>>::iterator itm = itxst.first; itm != itxed.first;
             ++itm) {
          int dx = X - itm->first;
          dx *= dx;
          if (0 <= itm->first && itm->first < 10000) {
            pair<set<int>::iterator, bool> stp = itm->second.insert(sty);
            pair<set<int>::iterator, bool> edp = itm->second.insert(edy);

            for (set<int>::iterator its = stp.first; its != edp.first; ++its) {
              if (*its > edy)
                break;
              int dy = Y - *its;
              int d = dx + dy * dy - 4 * R * R;
              if (0 <= d && d <= 4 * R)
                ++ans;
            }

            if (stp.second) {
              itm->second.erase(itm->second.find(sty));
            }
            if (edp.second) {
              itm->second.erase(itm->second.find(edy));
            }
          }
        }

        if (itxst.second) {
          Xb.erase(Xb.find(stx));
        }
        if (itxed.second) {
          Xb.erase(Xb.find(edx));
        }
      }
    }

    printf("%d\\n", ans);
  }
  return 0;
}