Problem p03858 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define plus 0
#define minus 1
using namespace std;
const int N = 1e5 + 10;
struct Dot {
  int x, y, id;
  Dot() {}
  Dot(int x1, int y1, int id1) {
    x = x1;
    y = y1;
    id = id1;
  }
  friend bool operator<(Dot a, Dot b) {
    if (a.x != b.x)
      return a.x < b.x;
    if (a.y != b.y)
      return a.y < b.y;
    return a.id < b.id;
  }
} a[N];
int vis[N];
map<int, vector<int>> rec2[2];
map<int, set<Dot>> rec1[2];
set<Dot>::iterator it, it2;
queue<Dot> q;
ll ans;
int n, A, B, D;
int Abs(int x) { return x < 0 ? -x : x; }
void prework() {
  while (!q.empty())
    q.pop();
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; ++i) {
    if (i == A)
      q.push(a[i]), vis[i] = true;
    else {
      rec1[plus][a[i].x + a[i].y].insert(a[i]);
      rec1[minus][a[i].x - a[i].y].insert(a[i]);
    }
  }
}
int Cnt = 0;
void get_point(int which, ll val, int lx, int rx) {
  Dot ttmp;
  if (rec1[which].find(val) == rec1[which].end())
    return;
  set<Dot> &tmp = rec1[which][val];
  it = lower_bound(tmp.begin(), tmp.end(), Dot(lx, 0, 0));
  ttmp = *it;
  for (it2 = it; ttmp.x <= rx && it != tmp.end(); it = it2, ttmp = *it2) {
    ++it2;
    q.push(ttmp), vis[ttmp.id] = true;
    rec1[plus][ttmp.x + ttmp.y].erase(ttmp);
    rec1[minus][ttmp.x - ttmp.y].erase(ttmp);
  }
}
void bfs() {
  Dot u, v;
  while (!q.empty()) {
    v = q.front();
    q.pop();
    if (1LL * v.x + v.y + D <= 2e9)
      get_point(plus, 1LL * v.x + v.y + D, v.x, v.x + D);
    get_point(plus, 1LL * v.x + v.y - D, v.x - D, v.x);

    get_point(minus, 1LL * v.x - v.y + D, v.x, v.x + D);
    get_point(minus, 1LL * v.x - v.y - D, v.x - D, v.x);

    // printf("%d\n",(int)q.size());
  }
  // printf("%d\n",debug);
}
int calc(int which, int val, int lx, int rx) {
  if (rec2[which].find(val) == rec2[which].end())
    return 0;
  vector<int> &tmp = rec2[which][val];
  return lower_bound(tmp.begin(), tmp.end(), rx + 1) -
         lower_bound(tmp.begin(), tmp.end(), lx);
}

void solve() {
  sort(a + 1, a + 1 + n);
  ans = 0;
  Dot v;
  for (int i = 1; i <= n; ++i) {
    v = a[i];
    // printf("%d %d %d\n",v.x,v.y,v.id);
    if (!vis[v.id])
      continue;
    if (1LL * v.x + v.y + D <= 2e9)
      ans += calc(plus, v.x + v.y + D, v.x, v.x + D);
    ans += calc(plus, v.x + v.y - D, v.x - D, v.x);

    ans += calc(minus, v.x - v.y + D, v.x + 1, v.x + D - 1);
    ans += calc(minus, v.x - v.y - D, v.x - D + 1, v.x - 1);

    rec2[plus][v.x + v.y].push_back(v.x);
    rec2[minus][v.x - v.y].push_back(v.x);
    // printf("%lld\n",ans);
  }
  printf("%lld\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
  // freopen("a.in","r",stdin);
#endif
  scanf("%d%d%d", &n, &A, &B);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &a[i].x, &a[i].y), a[i].id = i;
  D = Abs(a[A].x - a[B].x) + Abs(a[A].y - a[B].y);
  prework();
  bfs();
  solve();
  // printf("%d\n",Cnt);
}

Fixed submission:
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <vector>
#define ll long long
#define plus 0
#define minus 1
using namespace std;
const int N = 1e5 + 10;
struct Dot {
  int x, y, id;
  Dot() {}
  Dot(int x1, int y1, int id1) {
    x = x1;
    y = y1;
    id = id1;
  }
  friend bool operator<(Dot a, Dot b) {
    if (a.x != b.x)
      return a.x < b.x;
    if (a.y != b.y)
      return a.y < b.y;
    return a.id < b.id;
  }
} a[N];
int vis[N];
map<int, vector<int>> rec2[2];
map<int, set<Dot>> rec1[2];
set<Dot>::iterator it, it2;
queue<Dot> q;
ll ans;
int n, A, B, D;
int Abs(int x) { return x < 0 ? -x : x; }
void prework() {
  while (!q.empty())
    q.pop();
  memset(vis, false, sizeof(vis));
  for (int i = 1; i <= n; ++i) {
    if (i == A)
      q.push(a[i]), vis[i] = true;
    else {
      rec1[plus][a[i].x + a[i].y].insert(a[i]);
      rec1[minus][a[i].x - a[i].y].insert(a[i]);
    }
  }
}
int Cnt = 0;
void get_point(int which, ll val, int lx, int rx) {
  Dot ttmp;
  if (rec1[which].find(val) == rec1[which].end())
    return;
  set<Dot> &tmp = rec1[which][val];
  it = tmp.lower_bound(Dot(lx, 0, 0));
  ttmp = *it;
  for (it2 = it; ttmp.x <= rx && it != tmp.end(); it = it2, ttmp = *it2) {
    ++it2;
    q.push(ttmp), vis[ttmp.id] = true;
    rec1[plus][ttmp.x + ttmp.y].erase(ttmp);
    rec1[minus][ttmp.x - ttmp.y].erase(ttmp);
  }
}
void bfs() {
  Dot u, v;
  while (!q.empty()) {
    v = q.front();
    q.pop();
    if (1LL * v.x + v.y + D <= 2e9)
      get_point(plus, 1LL * v.x + v.y + D, v.x, v.x + D);
    get_point(plus, 1LL * v.x + v.y - D, v.x - D, v.x);

    get_point(minus, 1LL * v.x - v.y + D, v.x, v.x + D);
    get_point(minus, 1LL * v.x - v.y - D, v.x - D, v.x);

    // printf("%d\n",(int)q.size());
  }
  // printf("%d\n",debug);
}
int calc(int which, int val, int lx, int rx) {
  if (rec2[which].find(val) == rec2[which].end())
    return 0;
  vector<int> &tmp = rec2[which][val];
  return lower_bound(tmp.begin(), tmp.end(), rx + 1) -
         lower_bound(tmp.begin(), tmp.end(), lx);
}

void solve() {
  sort(a + 1, a + 1 + n);
  ans = 0;
  Dot v;
  for (int i = 1; i <= n; ++i) {
    v = a[i];
    // printf("%d %d %d\n",v.x,v.y,v.id);
    if (!vis[v.id])
      continue;
    if (1LL * v.x + v.y + D <= 2e9)
      ans += calc(plus, v.x + v.y + D, v.x, v.x + D);
    ans += calc(plus, v.x + v.y - D, v.x - D, v.x);

    ans += calc(minus, v.x - v.y + D, v.x + 1, v.x + D - 1);
    ans += calc(minus, v.x - v.y - D, v.x - D + 1, v.x - 1);

    rec2[plus][v.x + v.y].push_back(v.x);
    rec2[minus][v.x - v.y].push_back(v.x);
    // printf("%lld\n",ans);
  }
  printf("%lld\n", ans);
}

int main() {
#ifndef ONLINE_JUDGE
  // freopen("a.in","r",stdin);
#endif
  scanf("%d%d%d", &n, &A, &B);
  for (int i = 1; i <= n; ++i)
    scanf("%d%d", &a[i].x, &a[i].y), a[i].id = i;
  D = Abs(a[A].x - a[B].x) + Abs(a[A].y - a[B].y);
  prework();
  bfs();
  solve();
  // printf("%d\n",Cnt);
}