Problem p02556 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC optimize("O2")
/*
Arthor : Ender_zzm
E-mail zzm_ender_wiggin@outlook.com
Blog oi.ender-zzm.pro
*/
#include <bits/stdc++.h>
using namespace std;

inline int Isdigit(char c) {
  if (c < '0' || c > '9')
    return 0;
  return 1;
}
inline int readint() {
  register int x = 0, flag = 1;
  register char ch;
  while (!Isdigit(ch = getchar()))
    if (ch == '-')
      flag = -1;
  while (Isdigit(ch))
    x = x * 10 + (ch & 15), ch = getchar();
  return x * flag;
}

inline long long readlong() {
  register long long x = 0, flag = 1;
  register char ch;
  while (!Isdigit(ch = getchar()))
    if (ch == '-')
      flag = -1;
  while (Isdigit(ch))
    x = x * 10 + (ch & 15), ch = getchar();
  return x * flag;
}
template <class T> inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}

template <class T> inline bool Chkmax(T &x, const T &y) {
  return x < y ? x = y, true : false;
}
template <class T> inline bool Chkmin(T &x, const T &y) {
  return x > y ? x = y, true : false;
}

#define debug(x) cout << #x << " = " << x << endl;
#define For(i, x, y) for (int i = (x); i <= (y); i++)
#define Rep(i, x, y) for (int i = (x); i >= (y); i--)
#define ri readint
#define rl readlong

const int maxn = 2e5 + 30;
#define ls rt << 1
#define rs rt << 1 | 1
int n, cnt;
struct Point {
  int x, y, pp;
  bool operator<(const Point &a) const { return x < a.x; }
} a[maxn];
int cmpx(Point a, Point b) { return a.x < b.x; }
int cmpy(Point a, Point b) { return a.y < b.y; }
int main() {
  n = ri();
  for (int i = 1; i <= n; i++) {
    a[i].x = ri(), a[i].y = ri();
    int x = a[i].x, y = a[i].y;
    a[i].x = x + y;
    a[i].y = x - y;
  }
  sort(a + 1, a + 1 + n, cmpx);
  int ans = a[n].x - a[1].x;
  sort(a + 1, a + 1 + n, cmpy);
  ans = max(ans, a[n].y - a[1].y);
  printf("%d\\n", ans);
  return 0;
}


Error: TLE

Fixed submission:

#pragma GCC optimize("O2")
/*
Arthor : Ender_zzm
E-mail zzm_ender_wiggin@outlook.com
Blog oi.ender-zzm.pro
*/
#include <bits/stdc++.h>
using namespace std;

inline int Isdigit(char c) {
  if (c < '0' || c > '9')
    return 0;
  return 1;
}
inline int readint() {
  register int x = 0, flag = 1;
  register char ch;
  while (!Isdigit(ch = getchar()))
    if (ch == '-')
      flag = -1;
  while (Isdigit(ch))
    x = x * 10 + (ch & 15), ch = getchar();
  return x * flag;
}

inline long long readlong() {
  register long long x = 0, flag = 1;
  register char ch;
  while (!Isdigit(ch = getchar()))
    if (ch == '-')
      flag = -1;
  while (Isdigit(ch))
    x = x * 10 + (ch & 15), ch = getchar();
  return x * flag;
}
template <class T> inline void write(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x >= 10)
    write(x / 10);
  putchar(x % 10 + '0');
}

template <class T> inline bool Chkmax(T &x, const T &y) {
  return x < y ? x = y, true : false;
}
template <class T> inline bool Chkmin(T &x, const T &y) {
  return x > y ? x = y, true : false;
}

#define debug(x) cout << #x << " = " << x << endl;
#define For(i, x, y) for (int i = (x); i <= (y); i++)
#define Rep(i, x, y) for (int i = (x); i >= (y); i--)
#define ri readint
#define rl readlong

const int maxn = 2e5 + 30;
#define ls rt << 1
#define rs rt << 1 | 1
struct Point {
  int x, y, pp;
  bool operator<(const Point &a) const { return x < a.x; }
} a[maxn];
int n, cnt;
int Minn[3][maxn * 5], t[maxn];
void build(int rt, int l, int r) {
  if (l == r) {
    Minn[1][rt] = Minn[2][rt] = 0x3f3f3f3f;
    return;
  }
  int mid = (l + r) / 2;
  build(ls, l, mid);
  build(rs, mid + 1, r);
}
void insert(int rt, int l, int r, int pos, int v, int opt) {
  if (l == r) {
    Minn[opt][rt] = min(Minn[opt][rt], v);
    return;
  }
  int mid = (l + r) / 2;
  if (pos <= mid)
    insert(ls, l, mid, pos, v, opt);
  if (pos > mid)
    insert(rs, mid + 1, r, pos, v, opt);
  Minn[1][rt] = min(Minn[1][ls], Minn[1][rs]);
  Minn[2][rt] = min(Minn[2][ls], Minn[2][rs]);
}
int Query(int rt, int l, int r, int L, int R, int opt) {
  if (l == r) {
    return Minn[opt][rt];
  }
  int mid = (l + r) / 2;
  int ans = 0x3f3f3f3f;
  if (L <= mid)
    ans = min(ans, Query(ls, l, mid, L, R, opt));
  if (R > mid)
    ans = min(ans, Query(rs, mid + 1, r, L, R, opt));
  return ans;
}
int main() {
  n = ri();
  for (int i = 1; i <= n; i++) {
    a[i].x = ri(), a[i].y = ri();
    t[++cnt] = a[i].y;
  }
  sort(a + 1, a + 1 + n);
  sort(t + 1, t + 1 + cnt);
  cnt = unique(t + 1, t + 1 + cnt) - t - 1;
  build(1, 1, cnt);
  for (int i = 1; i <= n; i++) {
    a[i].pp = lower_bound(t + 1, t + 1 + cnt, a[i].y) - t;
  }
  int ans = 0;
  for (int i = 1; i <= n; i++) {
    ans = max(ans, a[i].x + a[i].y - Query(1, 1, cnt, 1, a[i].pp, 1));
    ans = max(ans, a[i].x - a[i].y - Query(1, 1, cnt, a[i].pp, cnt, 2));
    insert(1, 1, cnt, a[i].pp, a[i].x + a[i].y, 1);
    insert(1, 1, cnt, a[i].pp, a[i].x - a[i].y, 2);
  }
  printf("%d\\n", ans);
  return 0;
}