Problem p03858 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define PIE 3.14159265358979
#define NMAX 10005
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long int LL;

LL dist(LL x1, LL y1, LL x2, LL y2) { return abs(x1 - x2) + abs(y1 - y2); }

int main() {
  int N, a, b;
  cin >> N >> a >> b;
  a--, b--;
  vector<LL> x(N), y(N);
  vector<LL> P(N), M(N);
  vector<LL> Ps(N), Ms(N);
  vector<LL> PM[NMAX], MP[NMAX];
  set<pair<LL, int>> PMs[NMAX], MPs[NMAX];
  rep(i, N) {
    cin >> x[i] >> y[i];
    P[i] = x[i] + y[i];
    M[i] = x[i] - y[i];
    Ps[i] = x[i] + y[i];
    Ms[i] = x[i] - y[i];
  }
  sort(Ps.begin(), Ps.end());
  Ps.erase(unique(Ps.begin(), Ps.end()), Ps.end());
  sort(Ms.begin(), Ms.end());
  Ms.erase(unique(Ms.begin(), Ms.end()), Ms.end());
  rep(i, N) {
    int Pnum = (lower_bound(Ps.begin(), Ps.end(), P[i]) - Ps.begin());
    int Mnum = (lower_bound(Ms.begin(), Ms.end(), M[i]) - Ms.begin());
    PM[Pnum].push_back(M[i]);
    MP[Mnum].push_back(P[i]);
    PMs[Pnum].insert(make_pair(M[i], i));
    MPs[Mnum].insert(make_pair(P[i], i));
  }
  rep(i, N) {
    sort(PM[i].begin(), PM[i].end());
    sort(MP[i].begin(), MP[i].end());
  }

  LL d = dist(x[a], y[a], x[b], y[b]);
  LL ans = 0;
  bool used[NMAX];
  rep(i, N) { used[i] = false; }
  queue<int> que;
  que.push(a), que.push(b);
  used[a] = 1, used[b] = 1;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    size_t pos;
    set<pair<LL, int>>::iterator sleft, sright, snow;
    pos = lower_bound(Ps.begin(), Ps.end(), P[now] - d) - Ps.begin();
    if (Ps[pos] == P[now] - d) {
      ans += upper_bound(PM[pos].begin(), PM[pos].end(), M[now] + d) -
             lower_bound(PM[pos].begin(), PM[pos].end(), M[now] - d);
      sleft = PMs[pos].lower_bound(make_pair(M[now] - d, 0));
      sright = PMs[pos].upper_bound(make_pair(M[now] + d, N));
      snow = sleft;
      while (snow != sright) {
        if (!used[snow->second]) {
          que.push(snow->second);
          used[snow->second] = true;
        }
        snow++;
      }
      PMs[pos].erase(sleft, sright);
    }
    pos = lower_bound(Ps.begin(), Ps.end(), P[now] + d) - Ps.begin();
    if (Ps[pos] == P[now] + d) {
      ans += upper_bound(PM[pos].begin(), PM[pos].end(), M[now] + d) -
             lower_bound(PM[pos].begin(), PM[pos].end(), M[now] - d);
      sleft = PMs[pos].lower_bound(make_pair(M[now] - d, 0));
      sright = PMs[pos].upper_bound(make_pair(M[now] + d, N));
      snow = sleft;
      while (snow != sright) {
        if (!used[snow->second]) {
          que.push(snow->second);
          used[snow->second] = true;
        }
        snow++;
      }
      PMs[pos].erase(sleft, sright);
    }
    pos = lower_bound(Ms.begin(), Ms.end(), M[now] - d) - Ms.begin();
    if (Ms[pos] == M[now] - d) {
      ans += lower_bound(MP[pos].begin(), MP[pos].end(), P[now] + d) -
             upper_bound(MP[pos].begin(), MP[pos].end(), P[now] - d);
      sleft = MPs[pos].lower_bound(make_pair(P[now] - d, 0));
      sright = MPs[pos].upper_bound(make_pair(P[now] + d, N));
      snow = sleft;
      while (snow != sright) {
        if (!used[snow->second]) {
          que.push(snow->second);
          used[snow->second] = true;
        }
        snow++;
      }
      MPs[pos].erase(sleft, sright);
    }
    pos = lower_bound(Ms.begin(), Ms.end(), M[now] + d) - Ms.begin();
    if (Ms[pos] == M[now] + d) {
      ans += lower_bound(MP[pos].begin(), MP[pos].end(), P[now] + d) -
             upper_bound(MP[pos].begin(), MP[pos].end(), P[now] - d);
      sleft = MPs[pos].lower_bound(make_pair(P[now] - d, 0));
      sright = MPs[pos].upper_bound(make_pair(P[now] + d, N));
      snow = sleft;
      while (snow != sright) {
        if (!used[snow->second]) {
          que.push(snow->second);
          used[snow->second] = true;
        }
        snow++;
      }
      MPs[pos].erase(sleft, sright);
    }
  }
  ans /= 2;
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <vector>
#define PIE 3.14159265358979
#define NMAX 100005
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long int LL;

LL dist(LL x1, LL y1, LL x2, LL y2) { return abs(x1 - x2) + abs(y1 - y2); }

int main() {
  int N, a, b;
  cin >> N >> a >> b;
  a--, b--;
  vector<LL> x(N), y(N);
  vector<LL> P(N), M(N);
  vector<LL> Ps(N), Ms(N);
  vector<LL> PM[NMAX], MP[NMAX];
  set<pair<LL, int>> PMs[NMAX], MPs[NMAX];
  rep(i, N) {
    cin >> x[i] >> y[i];
    P[i] = x[i] + y[i];
    M[i] = x[i] - y[i];
    Ps[i] = x[i] + y[i];
    Ms[i] = x[i] - y[i];
  }
  sort(Ps.begin(), Ps.end());
  Ps.erase(unique(Ps.begin(), Ps.end()), Ps.end());
  sort(Ms.begin(), Ms.end());
  Ms.erase(unique(Ms.begin(), Ms.end()), Ms.end());
  rep(i, N) {
    int Pnum = (lower_bound(Ps.begin(), Ps.end(), P[i]) - Ps.begin());
    int Mnum = (lower_bound(Ms.begin(), Ms.end(), M[i]) - Ms.begin());
    PM[Pnum].push_back(M[i]);
    MP[Mnum].push_back(P[i]);
    PMs[Pnum].insert(make_pair(M[i], i));
    MPs[Mnum].insert(make_pair(P[i], i));
  }
  rep(i, N) {
    sort(PM[i].begin(), PM[i].end());
    sort(MP[i].begin(), MP[i].end());
  }

  LL d = dist(x[a], y[a], x[b], y[b]);
  LL ans = 0;
  bool used[NMAX];
  rep(i, N) { used[i] = false; }
  queue<int> que;
  que.push(a), que.push(b);
  used[a] = 1, used[b] = 1;
  while (!que.empty()) {
    int now = que.front();
    que.pop();
    size_t pos;
    set<pair<LL, int>>::iterator sleft, sright, snow;
    pos = lower_bound(Ps.begin(), Ps.end(), P[now] - d) - Ps.begin();
    if (Ps[pos] == P[now] - d) {
      ans += upper_bound(PM[pos].begin(), PM[pos].end(), M[now] + d) -
             lower_bound(PM[pos].begin(), PM[pos].end(), M[now] - d);
      sleft = PMs[pos].lower_bound(make_pair(M[now] - d, 0));
      sright = PMs[pos].upper_bound(make_pair(M[now] + d, N));
      snow = sleft;
      while (snow != sright) {
        if (!used[snow->second]) {
          que.push(snow->second);
          used[snow->second] = true;
        }
        snow++;
      }
      PMs[pos].erase(sleft, sright);
    }
    pos = lower_bound(Ps.begin(), Ps.end(), P[now] + d) - Ps.begin();
    if (Ps[pos] == P[now] + d) {
      ans += upper_bound(PM[pos].begin(), PM[pos].end(), M[now] + d) -
             lower_bound(PM[pos].begin(), PM[pos].end(), M[now] - d);
      sleft = PMs[pos].lower_bound(make_pair(M[now] - d, 0));
      sright = PMs[pos].upper_bound(make_pair(M[now] + d, N));
      snow = sleft;
      while (snow != sright) {
        if (!used[snow->second]) {
          que.push(snow->second);
          used[snow->second] = true;
        }
        snow++;
      }
      PMs[pos].erase(sleft, sright);
    }
    pos = lower_bound(Ms.begin(), Ms.end(), M[now] - d) - Ms.begin();
    if (Ms[pos] == M[now] - d) {
      ans += lower_bound(MP[pos].begin(), MP[pos].end(), P[now] + d) -
             upper_bound(MP[pos].begin(), MP[pos].end(), P[now] - d);
      sleft = MPs[pos].lower_bound(make_pair(P[now] - d, 0));
      sright = MPs[pos].upper_bound(make_pair(P[now] + d, N));
      snow = sleft;
      while (snow != sright) {
        if (!used[snow->second]) {
          que.push(snow->second);
          used[snow->second] = true;
        }
        snow++;
      }
      MPs[pos].erase(sleft, sright);
    }
    pos = lower_bound(Ms.begin(), Ms.end(), M[now] + d) - Ms.begin();
    if (Ms[pos] == M[now] + d) {
      ans += lower_bound(MP[pos].begin(), MP[pos].end(), P[now] + d) -
             upper_bound(MP[pos].begin(), MP[pos].end(), P[now] - d);
      sleft = MPs[pos].lower_bound(make_pair(P[now] - d, 0));
      sright = MPs[pos].upper_bound(make_pair(P[now] + d, N));
      snow = sleft;
      while (snow != sright) {
        if (!used[snow->second]) {
          que.push(snow->second);
          used[snow->second] = true;
        }
        snow++;
      }
      MPs[pos].erase(sleft, sright);
    }
  }
  ans /= 2;
  cout << ans << endl;
}