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

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> T;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
#define SQ(X) ((X) * (X))
int AN, BN, R, ans;
int AX[100100], AY[100100], BX[100100], BY[100100];
vector<T> event;
set<P> fighter, energy;
bool ok(int a, int b) {
  return (SQ(AX[a] - BX[b]) + SQ(AY[a] - BY[b]) <= 16 * R * R);
}
int main() {
  while (1) {
    scanf("%d %d %d", &AN, &BN, &R);
    if (AN == 0 && BN == 0)
      break;
    ans = 0;
    event.clear();
    fighter.clear();
    energy.clear();
    for (int i = 0; i < AN; i++) {
      scanf("%d %d", &AX[i], &AY[i]);
      event.pb(T(AX[i] - 2 * R, P(-1, i)));
      event.pb(T(AX[i] + 2 * R, P(1, i)));
    }
    for (int i = 0; i < BN; i++) {
      scanf("%d %d", &BX[i], &BY[i]);
      event.pb(T(BX[i] - 2 * R, P(-2, i)));
      event.pb(T(BX[i] + 2 * R, P(2, i)));
    }
    sort(event.begin(), event.end());
    for (int i = 0; i < event.size(); i++) {
      T cur = event[i];
      int k = cur.sec.sec;
      int type = cur.sec.fi;
      set<P>::iterator it, it1, it2;
      if (abs(type) == 1) {
        if (type < 0) {
          it1 = energy.lower_bound(P(AY[k] - 4 * R, -1));
          it2 = energy.upper_bound(P(AY[k] + 4 * R, INF));
          for (it = it1; it != it2; it++)
            if (ok(k, it->sec))
              ans++;
          fighter.insert(P(AY[k], k));
        } else
          fighter.erase(P(AY[k], k));
      } else {
        if (type < 0) {
          it1 = fighter.lower_bound(P(BY[k] - 4 * R, -1));
          it2 = fighter.upper_bound(P(BY[k] + 4 * R, INF));
          for (it = it1; it != it2; it++)
            if (ok(it->sec, k))
              ans++;
          energy.insert(P(BY[k], k));
        } else
          energy.erase(P(BY[k], k));
      }
    }
    cout << ans << endl;
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
typedef pair<int, int> P;
typedef pair<int, P> T;
typedef long long ll;
typedef vector<int> vi;
typedef vector<ll> vll;
#define pu push
#define pb push_back
#define mp make_pair
#define eps 1e-9
#define INF 2000000000
#define sz(x) ((int)(x).size())
#define fi first
#define sec second
#define SORT(x) sort((x).begin(), (x).end())
#define all(x) (x).begin(), (x).end()
#define EQ(a, b) (abs((a) - (b)) < eps)
#define SQ(X) ((X) * (X))
int AN, BN, R, ans;
int AX[100100], AY[100100], BX[100100], BY[100100];
vector<T> event;
set<P> fighter, energy;
bool comp(T a, T b) {
  if (a.sec.fi != b.sec.fi)
    return a.sec.fi < b.sec.fi;
  if (b.fi == 0 && b.sec.sec < BN)
    return false;
  return true;
}
bool ok(int a, int b) {
  return (SQ(AX[a] - BX[b]) + SQ(AY[a] - BY[b]) <= 16 * R * R);
}
int main() {
  while (1) {
    scanf("%d %d %d", &AN, &BN, &R);
    if (AN == 0 && BN == 0)
      break;
    ans = 0;
    event.clear();
    fighter.clear();
    energy.clear();
    for (int i = 0; i < AN; i++) {
      scanf("%d %d", &AX[i], &AY[i]);
      event.pb(T(0, P(AX[i] - 2 * R, i)));
      event.pb(T(0, P(AX[i] + 2 * R, i + AN)));
    }
    for (int i = 0; i < BN; i++) {
      scanf("%d %d", &BX[i], &BY[i]);
      event.pb(T(1, P(BX[i] - 2 * R, i)));
      event.pb(T(1, P(BX[i] + 2 * R, i + BN)));
    }
    sort(event.begin(), event.end(), comp);
    for (int i = 0; i < event.size(); i++) {
      T cur = event[i];
      int k = cur.sec.sec;
      set<P>::iterator it, it1, it2;
      if (cur.fi == 0) {
        if (k < AN) {
          it1 = energy.lower_bound(P(AY[k] - 4 * R, -1));
          it2 = energy.upper_bound(P(AY[k] + 4 * R, INF));
          for (it = it1; it != it2; it++)
            if (ok(k, it->sec))
              ans++;
          fighter.insert(P(AY[k], k));
        } else
          fighter.erase(P(AY[k - AN], k - AN));
      } else {
        if (k < BN) {
          it1 = fighter.lower_bound(P(BY[k] - 4 * R, -1));
          it2 = fighter.upper_bound(P(BY[k] + 4 * R, INF));
          for (it = it1; it != it2; it++)
            if (ok(it->sec, k))
              ans++;
          energy.insert(P(BY[k], k));
        } else
          energy.erase(P(BY[k - BN], k - BN));
      }
    }
    cout << ans << endl;
  }
  return 0;
}