Problem p01526 (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)
const double EPS = 1e-10;
const double PI = acos(-1.0);

const int MAX_N = 200010;
int par[MAX_N];
int myrank[MAX_N];

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    myrank[i] = 0;
  }
}

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (myrank[x] < myrank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (myrank[x] == myrank[y])
      myrank[x]++;
  }
}

bool same(int x, int y) { return find(x) == find(y); }
int dx[] = {0, 1, 1, 0, -1}, dy[] = {0, 0, 1, 1, 1};
int main() {
  int n;
  cin >> n;
  string rs;
  cin >> rs;
  string::iterator r_pos = rs.begin() + rs.find('.', 0);
  rs.replace(r_pos, r_pos + 1, "");
  int r = toInt(rs);
  init(n);

  map<pii, int> pia;
  REP(i, n) {
    string x, y;
    cin >> x >> y;
    string::iterator x_pos = x.begin() + x.find('.', 0);
    x.replace(x_pos, x_pos + 1, "");
    string::iterator y_pos = y.begin() + y.find('.', 0);
    y.replace(y_pos, y_pos + 1, "");
    pii p = make_pair(toInt(x), toInt(y));
    pii index = make_pair(p.first / r, p.second / r);
    if (!EXIST(pia, index)) {
      pia[index] = 0;
    }
    pia[index]++;
  }
  int ans = n;
  vector<pii> ps;
  map<pii, int> index;
  for (map<pii, int>::iterator it = pia.begin(); it != pia.end(); it++) {
    ps.push_back(it->first);
    index[it->first] = ps.size() - 1;
  }
  REP(i, ps.size()) {
    pii key = ps[i];
    REP(d, 5) {
      pii nbr = make_pair(key.first + dx[d], key.second + dy[d]);
      if (d == 0) {
        ans -= pia[nbr] - 1;
      } else {
        if (EXIST(pia, nbr)) {
          if (!same(i, index[nbr])) {
            ans--;
            unite(i, index[nbr]);
          }
        }
      }
    }
  }
  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)
const double EPS = 1e-10;
const double PI = acos(-1.0);

#include <complex>
typedef complex<double> P;

const int MAX_N = 10000;
int par[MAX_N];
int myrank[MAX_N];

void init(int n) {
  for (int i = 0; i < n; i++) {
    par[i] = i;
    myrank[i] = 0;
  }
}

int find(int x) {
  if (par[x] == x) {
    return x;
  } else {
    return par[x] = find(par[x]);
  }
}

void unite(int x, int y) {
  x = find(x);
  y = find(y);
  if (x == y)
    return;

  if (myrank[x] < myrank[y]) {
    par[x] = y;
  } else {
    par[y] = x;
    if (myrank[x] == myrank[y])
      myrank[x]++;
  }
}

bool same(int x, int y) { return find(x) == find(y); }

int dx[] = {0, 1, 1, 0, -1}, dy[] = {0, 0, 1, 1, 1};
int main() {
  int n;
  cin >> n;
  string rs;
  cin >> rs;
  string::iterator r_pos = rs.begin() + rs.find('.', 0);
  rs.replace(r_pos, r_pos + 1, "");
  int r = toInt(rs);
  init(n);

  vector<pii> ps;
  map<pii, vi> pia;
  REP(i, n) {
    string x, y;
    cin >> x >> y;
    string::iterator x_pos = x.begin() + x.find('.', 0);
    x.replace(x_pos, x_pos + 1, "");
    string::iterator y_pos = y.begin() + y.find('.', 0);
    y.replace(y_pos, y_pos + 1, "");
    pii p = make_pair(toInt(x), toInt(y));
    ps.push_back(p);
    pii index = make_pair(p.first / r, p.second / r);
    if (!EXIST(pia, index)) {
      pia[index] = vi();
    }
    pia[index].push_back(i);
  }
  int ans = n;
  for (map<pii, vi>::iterator it = pia.begin(); it != pia.end(); it++) {
    pii key = it->first;
    REP(d, 5) {
      pii nbr = make_pair(key.first + dx[d], key.second + dy[d]);
      if (d == 0) {
        REP(k, pia[nbr].size() - 1) {
          unite(pia[nbr][k], pia[nbr][k + 1]);
          ans--;
        }
      } else {
        if (EXIST(pia, nbr)) {
          unite(pia[key][0], pia[nbr][0]);
          ans--;
        }
      }
    }
  }
  cout << ans << endl;
}