Problem p03858 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>

#define For(i, l, r)                                                           \
  for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l)                                                       \
  for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back

using namespace std;

template <typename T> inline bool chkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, T b) {
  return b > a ? a = b, 1 : 0;
}

inline int read() {
  int x(0), sgn(1);
  char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      sgn = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  return x * sgn;
}

void File() {
#ifdef zjp_shadow
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
#endif
}

const int N = 1e5 + 1e3;

struct Node {
  int x, y, id;
} lt[N];

struct Cmp {
  inline bool operator()(const Node &lhs, const Node &rhs) {
    return lhs.x != rhs.x ? lhs.x < rhs.x : lhs.y < rhs.y;
  }
};

vector<int> G[N];
int con[N];

inline void Add(int u, int v) {
  G[u].pb(v);
  G[v].pb(u);
}

int n, a, b, dis;
void Work(int d) {
  sort(lt + 1, lt + n + 1, Cmp());
  int l = 1, r = 0, pos = 1;
  For(i, 1, n) {
    while (l < n && (lt[l].x < lt[i].x - dis ||
                     (lt[l].x == lt[i].x - dis && lt[l].y < lt[i].y - d)))
      ++l;
    while (r < n &&
           (lt[r + 1].x < lt[i].x - dis ||
            (lt[r + 1].x == lt[i].x - dis && lt[r + 1].y <= lt[i].y + d)))
      ++r;
    con[lt[i].id] += r - l + 1;
    chkmax(pos, l);
    For(j, pos, r) Add(lt[i].id, lt[j].id);
  }
}

long long ans = 0;
bitset<N> vis;

void Dfs(int u) {
  if (vis[u])
    return;
  vis[u] = true;
  ans += con[u];
  for (int v : G[u])
    Dfs(v);
}

int main() {

  File();

  n = read(), a = read(), b = read();

  For(i, 1, n) {
    int x = read(), y = read();
    lt[i] = (Node){x + y, x - y, i};
  }
  dis = max(abs(lt[a].x - lt[b].x), abs(lt[a].y - lt[b].y));

  Work(dis);
  For(i, 1, n) swap(lt[i].x, lt[i].y);
  Work(dis - 1);

  Dfs(a);
  printf("%lld\n", ans);

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

#define For(i, l, r)                                                           \
  for (register int i = (l), i##end = (int)(r); i <= i##end; ++i)
#define Fordown(i, r, l)                                                       \
  for (register int i = (r), i##end = (int)(l); i >= i##end; --i)
#define Set(a, v) memset(a, v, sizeof(a))
#define Cpy(a, b) memcpy(a, b, sizeof(a))
#define debug(x) cout << #x << ": " << (x) << endl
#define DEBUG(...) fprintf(stderr, __VA_ARGS__)
#define pb push_back

using namespace std;

template <typename T> inline bool chkmin(T &a, T b) {
  return b < a ? a = b, 1 : 0;
}
template <typename T> inline bool chkmax(T &a, T b) {
  return b > a ? a = b, 1 : 0;
}

inline int read() {
  int x(0), sgn(1);
  char ch(getchar());
  for (; !isdigit(ch); ch = getchar())
    if (ch == '-')
      sgn = -1;
  for (; isdigit(ch); ch = getchar())
    x = (x * 10) + (ch ^ 48);
  return x * sgn;
}

void File() {
#ifdef zjp_shadow
  freopen("E.in", "r", stdin);
  freopen("E.out", "w", stdout);
#endif
}

const int N = 1e5 + 1e3;

struct Node {
  int x, y, id;
} lt[N];

struct Cmp {
  inline bool operator()(const Node &lhs, const Node &rhs) {
    return lhs.x != rhs.x ? lhs.x < rhs.x : lhs.y < rhs.y;
  }
};

vector<int> G[N];
int con[N];

inline void Add(int u, int v) {
  G[u].pb(v);
  G[v].pb(u);
}

int n, a, b, dis;
void Work(int d) {
  sort(lt + 1, lt + n + 1, Cmp());
  int l = 1, r = 0, pos = 1;
  For(i, 1, n) {
    while (l < n && (lt[l].x < lt[i].x - dis ||
                     (lt[l].x == lt[i].x - dis && lt[l].y < lt[i].y - d)))
      ++l;
    while (r < n &&
           (lt[r + 1].x < lt[i].x - dis ||
            (lt[r + 1].x == lt[i].x - dis && lt[r + 1].y <= lt[i].y + d)))
      ++r;
    con[lt[i].id] += r - l + 1;
    for (chkmax(pos, l); pos <= r; ++pos)
      Add(lt[i].id, lt[pos].id);
    --pos;
  }
}

long long ans = 0;
bitset<N> vis;

void Dfs(int u) {
  if (vis[u])
    return;
  vis[u] = true;
  ans += con[u];
  for (int v : G[u])
    Dfs(v);
}

int main() {

  File();

  n = read(), a = read(), b = read();

  For(i, 1, n) {
    int x = read(), y = read();
    lt[i] = (Node){x + y, x - y, i};
  }
  dis = max(abs(lt[a].x - lt[b].x), abs(lt[a].y - lt[b].y));

  Work(dis);
  For(i, 1, n) swap(lt[i].x, lt[i].y);
  Work(dis - 1);

  Dfs(a);
  printf("%lld\n", ans);

  return 0;
}