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


#include <bits/stdc++.h>

#define gc() std::getchar()
#define pc(i) std::putchar(i)
#define space ' '
#define enter '\\n'
#define RE cout << "Passing [" << __FUNCTION__ << "] on " << __LINE__ << enter
#define DEBUG(...)                                                             \\
  for (auto &i : __VA_ARGS__)                                                  \\
    cout << i << " ";                                                          \\
  cout << enter
using std::make_pair;

template <typename T> inline T read() {
  T x = 0;
  char ch = gc();
  bool f = 0;
  while (!std::isdigit(ch)) {
    f = (ch == '-');
    ch = gc();
  }
  while (std::isdigit(ch)) {
    x = x * 10 + (ch - '0');
    ch = gc();
  }
  return f ? -x : x;
}

template <typename T> void print(T x) {
  if (x < 0) {
    x = -x;
    pc('-');
  }
  if (x < 10) {
    pc(x + 48);
    return;
  }
  print<T>(x / 10);
  pc(x % 10 + 48);
  return;
}

namespace Solution {
#define IOS std::ios::sync_with_stdio(false), std::cin.tie(0), std::cout.tie(0)
#define Rep(i, j, k) for (int i = j; i >= k; --i)
#define rdi() read<int>()
#define rdl() read<long long>()
#define pti(i) print<int>(i), putchar(space)
#define ptl(i) print<long long>(i), putchar(space)
#define For(i, j, k) for (int i = j; i <= k; ++i)
#define int long long
using std::cin;
using std::cout;
using std::deque;
using std::endl;
using std::map;
using std::pair;
using std::queue;
using std::set;
using std::vector;
const int N = 204001, mod = 1e9 + 7;
int n, ans, a[N * 2];
pair<int, int> p[N];
struct Node {
  int l, r, mn;
} elvahs[N * 8];
#define l(i) elvahs[i].l
#define r(i) elvahs[i].r
#define mn(i) elvahs[i].mn
inline int ls(int k) { return k << 1; }
inline int rs(int k) { return k << 1 | 1; }
inline void up(int k) { mn(k) = std::min(mn(ls(k)), mn(rs(k))); }
void Build(int k, int l, int r) {
  l(k) = l;
  r(k) = r;
  mn(k) = 2e9;
  if (l == r)
    return;
  int mid = l + r >> 1;
  Build(ls(k), l, mid);
  Build(rs(k), mid + 1, r);
  return up(k);
}
void Modify(int k, int pos, int x) {
  if (l(k) == r(k)) {
    mn(k) = std::min(mn(k), a[pos] + x);
    return;
  }
  int mid = l(k) + r(k) >> 1;
  pos <= mid ? Modify(ls(k), pos, x) : Modify(rs(k), pos, x);
  return up(k);
}
int Query(int k, int l, int r) {
  if (l <= l(k) && r(k) <= r)
    return mn(k);
  int mid = l(k) + r(k) >> 1, res = 2e9;
  if (l <= mid)
    res = Query(ls(k), l, r);
  if (mid < r)
    res = std::min(res, Query(rs(k), l, r));
  return res;
}
void Fakemain() {
  IOS;
  cin >> n;
  For(i, 1, n) cin >> p[i].first >> p[i].second;
  std::sort(p + 1, p + 1 + n);
  int cnt = std::unique(p + 1, p + 1 + n) - p - 1;
  ans = 0;
  For(i, 1, cnt) a[i] = p[i].second;
  For(i, cnt + 1, cnt * 2) a[i] = -a[i - cnt];
  std::sort(a + 1, a + cnt * 2 + 1);
  int num = std::unique(a + 1, a + 1 + cnt * 2) - a - 1;
  Build(1, 1, num);
  For(i, 1, cnt) {
    int k = std::lower_bound(a + 1, a + 1 + num, p[i].second) - a;
    if (i != 1)
      ans = std::max(ans, p[i].first + p[i].second - Query(1, 1, k));
    Modify(1, k, p[i].first);
  }
  For(i, 1, cnt) p[i].second = -p[i].second;
  Build(1, 1, num);
  For(i, 1, cnt) {
    int k = std::lower_bound(a + 1, a + 1 + num, p[i].second) - a;
    if (i != 1)
      ans = std::max(ans, p[i].first + p[i].second - Query(1, 1, k));
    Modify(1, k, p[i].first);
  }
  cout << ans;
  return;
}

} // namespace Solution
#undef int
int main(int argc, char *argv[]) {
  Solution::Fakemain();
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize(2)
#pragma GCC optimize(3)
#pragma GCC optimize("Ofast")
%:pragma GCC optimize("Ofast")
%:pragma GCC optimize("inline")
%:pragma GCC optimize("-fgcse")
%:pragma GCC optimize("-fgcse-lm")
%:pragma GCC optimize("-fipa-sra")
%:pragma GCC optimize("-ftree-pre")
%:pragma GCC optimize("-ftree-vrp")
%:pragma GCC optimize("-fpeephole2")
%:pragma GCC optimize("-ffast-math")
%:pragma GCC optimize("-fsched-spec")
%:pragma GCC optimize("unroll-loops")
%:pragma GCC optimize("-falign-jumps")
%:pragma GCC optimize("-falign-loops")
%:pragma GCC optimize("-falign-labels")
%:pragma GCC optimize("-fdevirtualize")
%:pragma GCC optimize("-fcaller-saves")
%:pragma GCC optimize("-fcrossjumping")
%:pragma GCC optimize("-fthread-jumps")
%:pragma GCC optimize("-funroll-loops")
%:pragma GCC optimize("-fwhole-program")
%:pragma GCC optimize("-freorder-blocks")
%:pragma GCC optimize("-fschedule-insns")
%:pragma GCC optimize("inline-functions")
%:pragma GCC optimize("-ftree-tail-merge")
%:pragma GCC optimize("-fschedule-insns2")
%:pragma GCC optimize("-fstrict-aliasing")
%:pragma GCC optimize("-fstrict-overflow")
%:pragma GCC optimize("-falign-functions")
%:pragma GCC optimize("-fcse-skip-blocks")
%:pragma GCC optimize("-fcse-follow-jumps")
%:pragma GCC optimize("-fsched-interblock")
%:pragma GCC optimize("-fpartial-inlining")
%:pragma GCC optimize("no-stack-protector")
%:pragma GCC optimize("-freorder-functions")
%:pragma GCC optimize("-findirect-inlining")
%:pragma GCC optimize("-fhoist-adjacent-loads")
%:pragma GCC optimize("-frerun-cse-after-loop")
%:pragma GCC optimize("inline-small-functions")
%:pragma GCC optimize("-finline-small-functions")
%:pragma GCC optimize("-ftree-switch-conversion")
%:pragma GCC optimize("-foptimize-sibling-calls")
%:pragma GCC optimize("-fexpensive-optimizations")
%:pragma GCC optimize("-funsafe-loop-optimizations")
%:pragma GCC optimize("inline-functions-called-once")
%:pragma GCC optimize("-fdelete-null-pointer-checks")
#include <bits/stdc++.h>
using std::cin;
using std::cout;
using std::endl;
using std::pair;
const int N = 200001;
int n, rt, res;
struct Node {
  int ls, rs;
  int L, R, D, U;
  pair<int, int> coo;
} elvahs[N];
pair<int, int> p[N];
#define L(i) elvahs[i].L
#define R(i) elvahs[i].R
#define D(i) elvahs[i].D
#define U(i) elvahs[i].U
#define ls(i) elvahs[i].ls
#define rs(i) elvahs[i].rs
#define coo(i) elvahs[i].coo
#define x(i) coo(i).first
#define y(i) coo(i).second
bool cmpx(Node a, Node b) { return a.coo.first < b.coo.first; }
bool cmpy(Node a, Node b) { return a.coo.second < b.coo.second; }
inline void maintain(int k) {
  L(k) = R(k) = x(k);
  U(k) = D(k) = y(k);
  if (ls(k))
    L(k) = std::min(L(ls(k)), L(k)), R(k) = std::max(R(ls(k)), R(k)),
    U(k) = std::max(U(ls(k)), U(k)), D(k) = std::min(D(ls(k)), D(k));
  if (rs(k))
    L(k) = std::min(L(rs(k)), L(k)), R(k) = std::max(R(rs(k)), R(k)),
    U(k) = std::max(U(rs(k)), U(k)), D(k) = std::min(D(rs(k)), D(k));
  return;
}
void Build(int &k, int l, int r, int d) {
  if (l > r)
    return;
  k = l + r >> 1;
  if (l == r)
    return maintain(l);
  if (d & 1)
    std::nth_element(elvahs + l, elvahs + k, elvahs + r + 1, cmpx);
  else
    std::nth_element(elvahs + l, elvahs + k, elvahs + r + 1, cmpy);
  Build(ls(k), l, k - 1, d ^ 1);
  Build(rs(k), k + 1, r, d ^ 1);
  return maintain(k);
}
int f(int k, int w) {
  return std::max(abs(U(k) - y(w)), abs(D(k) - y(w))) +
         std::max(abs(L(k) - x(w)), abs(R(k) - x(w)));
}
void Query(int l, int r, int w) {
  if (l > r)
    return;
  int k(l + r >> 1), ans = abs(x(w) - x(k)) + abs(y(w) - y(k)),
                     disl = f(ls(k), w), disr = f(rs(k), w);
  res = std::max(res, ans);
  if (disl > res && disr > res) {
    if (disl > disr) {
      Query(l, k - 1, w);
      if (disr > res)
        Query(k + 1, r, w);
    } else {
      Query(k + 1, r, w);
      if (disl > res)
        Query(l, k - 1, w);
    }
  } else {
    if (disl > res)
      Query(l, k - 1, w);
    if (disr > res)
      Query(k + 1, r, w);
  }
  return;
}
int main() {
  int k;
  std::ios::sync_with_stdio(false);
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> p[i].first >> p[i].second;
  std::sort(p + 1, p + 1 + n);
  int cnt = std::unique(p + 1, p + 1 + n) - p - 1;
  if (cnt == 1) {
    cout << 0;
    return 0;
  }
  for (int i = 1; i <= cnt; ++i)
    x(i) = p[i].first, y(i) = p[i].second;
  std::random_shuffle(elvahs + 1, elvahs + 1 + cnt);
  Build(rt, 1, cnt, 1);
  for (int i = 1; i <= cnt; ++i)
    Query(1, cnt, i);
  cout << res;
  return 0;
}