Problem p03858 (C++)

Buggy submission (status: Runtime Error):
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//                       _oo0oo_                         //
//                      o8888888o                        //
//                      88" . "88      ------ hzt1       //
//                      (| -_- |)                        //
//                      0\  =  /0                        //
//                    ___/`---'\___                      //
//                  .' \|     |// '.                     //
//                 / \|||  :  |||// \                    //
//                / _||||| -:- |||||- \                  //
//               |   | \  -  /// |     |                 //
//               | \_|  ''\---/''  |_/ |                 //
//               \  .-\__  '-'  ___/-. /                 //
//             ___'. .'  /--.--\  `. .'___               //
//          ."" '<  `.___\_<|>_/___.' >' "".             //
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |           //
//         \  \ `_.   \_ __\ /__ _/   .-` /  /           //
//     =====`-.____`.___ \_____/___.-`___.-'=====        //
//                       `=---='                         //
//                                                       //
//                                                       //
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       //
//                                                       //
//                 God-He Bless All.                     //
//           This Code Will Never Explode.               //
//                                                       //
//                                                       //
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#define rep(i, a, b) for (int i = (a), i##_end_ = (b); i <= i##_end_; i++)
#define dwn(i, a, b) for (int i = (a), i##_end_ = (b); i >= i##_end_; i--)
using namespace std;
const int Size = 1 << 16;
char buffer[Size], *head, *tail;
inline char Getchar() {
  if (head == tail) {
    int l = fread(buffer, 1, Size, stdin);
    tail = (head = buffer) + l;
  }
  if (head == tail)
    return -1;
  return *head++;
}
inline int read() {
  int x = 0, f = 1;
  char c = Getchar();
  for (; !isdigit(c); c = Getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = Getchar())
    x = x * 10 + c - '0';
  return x * f;
}
typedef pair<int, int> pr;
typedef long long ll;
#define mp make_pair
#define xx first
#define yy second
const int maxn = 100010;
struct Node {
  Node *ch[2];
  int r, s, v;
  void maintain() { s = ch[0]->s + ch[1]->s + 1; }
} nodes[maxn], *null = &nodes[0];
int ToT;
Node *newnode(int v) {
  Node *o = &nodes[++ToT];
  o->ch[0] = o->ch[1] = null;
  o->s = 1;
  o->v = v;
  o->r = rand();
  return o;
}
Node *root[2 * maxn];
map<int, int> M;
int TAT;
void init() {
  rep(i, 1, TAT) root[i] = null;
  TAT = ToT = 0;
  M.clear();
}
int id(int x) {
  if (!M.count(x))
    M[x] = ++TAT;
  return M[x];
}
void rotate(Node *&o, int d) {
  Node *k = o->ch[d ^ 1];
  o->ch[d ^ 1] = k->ch[d];
  k->ch[d] = o;
  o->maintain();
  k->maintain();
  o = k;
}
void insert(Node *&o, int v) {
  if (o == null)
    o = newnode(v);
  else {
    int d = v > o->v;
    insert(o->ch[d], v);
    if (o->ch[d]->r > o->r)
      rotate(o, d ^ 1);
    else
      o->maintain();
  }
}
int query(Node *&o, int v) {
  if (o == null)
    return 0;
  if (v < o->v)
    return query(o->ch[0], v);
  return query(o->ch[1], v) + o->ch[0]->s + 1;
}
void print(Node *&o) {
  if (o == null)
    return;
  print(o->ch[0]);
  printf("%d ", o->v);
  print(o->ch[1]);
}
pr A[maxn], B[maxn];
int n, Q[maxn];
set<pr> T1[maxn * 2], T2[maxn * 2];
void del(int x) {
  T1[id(A[x].xx - A[x].yy)].erase(mp(A[x].xx + A[x].yy, x));
  T2[id(A[x].xx + A[x].yy)].erase(mp(A[x].xx - A[x].yy, x));
}
void pre(int x, int y, int d) {
  swap(A[1], A[x]);
  swap(A[2], A[y]);
  Q[1] = 1;
  Q[2] = 2;
  int m = 2;
  rep(i, 3, n) {
    T1[id(A[i].xx - A[i].yy)].insert(mp(A[i].xx + A[i].yy, i));
    T2[id(A[i].xx + A[i].yy)].insert(mp(A[i].xx - A[i].yy, i));
  }
  for (int i = 1; i <= m; i++) {
    int x = A[Q[i]].xx - A[Q[i]].yy, y = A[Q[i]].xx + A[Q[i]].yy;
    for (set<pr>::iterator it = T1[id(x - d)].lower_bound(mp(y - d, 0));
         it != T1[id(x - d)].end();) {
      if ((*it).xx <= y + d)
        Q[++m] = (*it).yy, it++, del(Q[m]);
      else
        break;
    }
    for (set<pr>::iterator it = T1[id(x + d)].lower_bound(mp(y - d, 0));
         it != T1[id(x + d)].end();) {
      if ((*it).xx <= y + d)
        Q[++m] = (*it).yy, it++, del(Q[m]);
      else
        break;
    }
    for (set<pr>::iterator it = T2[id(y - d)].lower_bound(mp(x - d, 0));
         it != T2[id(y - d)].end();) {
      if ((*it).xx <= x + d)
        Q[++m] = (*it).yy, it++, del(Q[m]);
      else
        break;
    }
    for (set<pr>::iterator it = T2[id(y + d)].lower_bound(mp(x - d, 0));
         it != T2[id(y + d)].end();) {
      if ((*it).xx <= x + d)
        Q[++m] = (*it).yy, it++, del(Q[m]);
      else
        break;
    }
  }
  rep(i, 1, m) B[i] = A[Q[i]];
  rep(i, 1, m) A[i] = B[i];
  n = m;
  TAT = 0;
  M.clear();
}
ll ans;
void solve(int D) {
  init();
  sort(A + 1, A + n + 1);
  rep(i, 1, n) {
    int p = i;
    while (p <= n && A[p].xx == A[i].xx)
      p++;
    p--;
    rep(j, i, p) {
      int tar = A[j].xx + A[j].yy - D;
      ans += query(root[id(tar)], A[j].yy - 1);
    }
    rep(j, i, p) insert(root[id(A[j].xx + A[j].yy)], A[j].yy);
    i = p;
  }
}
map<pr, int> S;
int main() {
  srand(52501);
  n = read();
  int x = read(), y = read();
  rep(i, 1, 2 * n) root[i] = null;
  rep(i, 1, n) A[i].xx = read(), A[i].yy = read();
  int d = abs(A[x].xx - A[y].xx) + abs(A[x].yy - A[y].yy);
  pre(x, y, d);
  solve(d);
  rep(i, 1, n) A[i].yy = -A[i].yy;
  solve(d);
  rep(i, 1, n) A[i].xx = -A[i].xx;
  solve(d);
  rep(i, 1, n) A[i].yy = -A[i].yy;
  solve(d);
  ans >>= 1;
  rep(i, 1, n) {
    ans += S[mp(A[i].xx - d, A[i].yy)];
    ans += S[mp(A[i].xx + d, A[i].yy)];
    ans += S[mp(A[i].xx, A[i].yy - d)];
    ans += S[mp(A[i].xx, A[i].yy + d)];
    S[A[i]]++;
  }
  printf("%lld\n", ans);
  return 0;
}

Fixed submission:
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
//                       _oo0oo_                         //
//                      o8888888o                        //
//                      88" . "88      ------ hzt1       //
//                      (| -_- |)                        //
//                      0\  =  /0                        //
//                    ___/`---'\___                      //
//                  .' \|     |// '.                     //
//                 / \|||  :  |||// \                    //
//                / _||||| -:- |||||- \                  //
//               |   | \  -  /// |     |                 //
//               | \_|  ''\---/''  |_/ |                 //
//               \  .-\__  '-'  ___/-. /                 //
//             ___'. .'  /--.--\  `. .'___               //
//          ."" '<  `.___\_<|>_/___.' >' "".             //
//         | | :  `- \`.;`\ _ /`;.`/ - ` : | |           //
//         \  \ `_.   \_ __\ /__ _/   .-` /  /           //
//     =====`-.____`.___ \_____/___.-`___.-'=====        //
//                       `=---='                         //
//                                                       //
//                                                       //
//     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~       //
//                                                       //
//                 God-He Bless All.                     //
//           This Code Will Never Explode.               //
//                                                       //
//                                                       //
///////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////
#include <algorithm>
#include <cctype>
#include <cstdio>
#include <cstring>
#include <map>
#include <set>
#define rep(i, a, b) for (int i = (a), i##_end_ = (b); i <= i##_end_; i++)
#define dwn(i, a, b) for (int i = (a), i##_end_ = (b); i >= i##_end_; i--)
using namespace std;
const int Size = 1 << 16;
char buffer[Size], *head, *tail;
inline char Getchar() {
  if (head == tail) {
    int l = fread(buffer, 1, Size, stdin);
    tail = (head = buffer) + l;
  }
  if (head == tail)
    return -1;
  return *head++;
}
inline int read() {
  int x = 0, f = 1;
  char c = Getchar();
  for (; !isdigit(c); c = Getchar())
    if (c == '-')
      f = -1;
  for (; isdigit(c); c = Getchar())
    x = x * 10 + c - '0';
  return x * f;
}
typedef pair<int, int> pr;
typedef long long ll;
#define mp make_pair
#define xx first
#define yy second
const int maxn = 500010;
struct Node {
  Node *ch[2];
  int r, s, v;
  void maintain() { s = ch[0]->s + ch[1]->s + 1; }
} nodes[maxn], *null = &nodes[0];
int ToT;
Node *newnode(int v) {
  Node *o = &nodes[++ToT];
  o->ch[0] = o->ch[1] = null;
  o->s = 1;
  o->v = v;
  o->r = rand();
  return o;
}
Node *root[2 * maxn];
map<int, int> M;
int TAT;
void init() {
  rep(i, 1, TAT) root[i] = null;
  TAT = ToT = 0;
  M.clear();
}
int id(int x) {
  if (!M.count(x))
    M[x] = ++TAT;
  return M[x];
}
void rotate(Node *&o, int d) {
  Node *k = o->ch[d ^ 1];
  o->ch[d ^ 1] = k->ch[d];
  k->ch[d] = o;
  o->maintain();
  k->maintain();
  o = k;
}
void insert(Node *&o, int v) {
  if (o == null)
    o = newnode(v);
  else {
    int d = v > o->v;
    insert(o->ch[d], v);
    if (o->ch[d]->r > o->r)
      rotate(o, d ^ 1);
    else
      o->maintain();
  }
}
int query(Node *&o, int v) {
  if (o == null)
    return 0;
  if (v < o->v)
    return query(o->ch[0], v);
  return query(o->ch[1], v) + o->ch[0]->s + 1;
}
void print(Node *&o) {
  if (o == null)
    return;
  print(o->ch[0]);
  printf("%d ", o->v);
  print(o->ch[1]);
}
pr A[maxn], B[maxn];
int n, Q[maxn];
set<pr> T1[maxn * 2], T2[maxn * 2];
void del(int x) {
  T1[id(A[x].xx - A[x].yy)].erase(mp(A[x].xx + A[x].yy, x));
  T2[id(A[x].xx + A[x].yy)].erase(mp(A[x].xx - A[x].yy, x));
}
void pre(int x, int y, int d) {
  swap(A[1], A[x]);
  swap(A[2], A[y]);
  Q[1] = 1;
  Q[2] = 2;
  int m = 2;
  rep(i, 3, n) {
    T1[id(A[i].xx - A[i].yy)].insert(mp(A[i].xx + A[i].yy, i));
    T2[id(A[i].xx + A[i].yy)].insert(mp(A[i].xx - A[i].yy, i));
  }
  for (int i = 1; i <= m; i++) {
    int x = A[Q[i]].xx - A[Q[i]].yy, y = A[Q[i]].xx + A[Q[i]].yy;
    for (set<pr>::iterator it = T1[id(x - d)].lower_bound(mp(y - d, 0));
         it != T1[id(x - d)].end();) {
      if ((*it).xx <= y + d)
        Q[++m] = (*it).yy, it++, del(Q[m]);
      else
        break;
    }
    for (set<pr>::iterator it = T1[id(x + d)].lower_bound(mp(y - d, 0));
         it != T1[id(x + d)].end();) {
      if ((*it).xx <= y + d)
        Q[++m] = (*it).yy, it++, del(Q[m]);
      else
        break;
    }
    for (set<pr>::iterator it = T2[id(y - d)].lower_bound(mp(x - d, 0));
         it != T2[id(y - d)].end();) {
      if ((*it).xx <= x + d)
        Q[++m] = (*it).yy, it++, del(Q[m]);
      else
        break;
    }
    for (set<pr>::iterator it = T2[id(y + d)].lower_bound(mp(x - d, 0));
         it != T2[id(y + d)].end();) {
      if ((*it).xx <= x + d)
        Q[++m] = (*it).yy, it++, del(Q[m]);
      else
        break;
    }
  }
  rep(i, 1, m) B[i] = A[Q[i]];
  rep(i, 1, m) A[i] = B[i];
  n = m;
  TAT = 0;
  M.clear();
}
ll ans;
void solve(int D) {
  init();
  sort(A + 1, A + n + 1);
  rep(i, 1, n) {
    int p = i;
    while (p <= n && A[p].xx == A[i].xx)
      p++;
    p--;
    rep(j, i, p) {
      int tar = A[j].xx + A[j].yy - D;
      ans += query(root[id(tar)], A[j].yy - 1);
    }
    rep(j, i, p) insert(root[id(A[j].xx + A[j].yy)], A[j].yy);
    i = p;
  }
}
map<pr, int> S;
int main() {
  srand(52501);
  n = read();
  int x = read(), y = read();
  rep(i, 1, 2 * n) root[i] = null;
  rep(i, 1, n) A[i].xx = read(), A[i].yy = read();
  int d = abs(A[x].xx - A[y].xx) + abs(A[x].yy - A[y].yy);
  pre(x, y, d);
  solve(d);
  rep(i, 1, n) A[i].yy = -A[i].yy;
  solve(d);
  rep(i, 1, n) A[i].xx = -A[i].xx;
  solve(d);
  rep(i, 1, n) A[i].yy = -A[i].yy;
  solve(d);
  ans >>= 1;
  rep(i, 1, n) {
    ans += S[mp(A[i].xx - d, A[i].yy)];
    ans += S[mp(A[i].xx + d, A[i].yy)];
    ans += S[mp(A[i].xx, A[i].yy - d)];
    ans += S[mp(A[i].xx, A[i].yy + d)];
    S[A[i]]++;
  }
  printf("%lld\n", ans);
  return 0;
}