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

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define endl '\\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;

// Most important thing in CP - Have Fun :)

const int N = 2e5 + 5;

struct Point {
  int x, y;
  bool operator<(const Point &b) const {
    return (x < b.x) || (x == b.x && y < b.y);
  }
};

Point d[N], p0;

ll dist(Point p1, Point p2) { return (abs(p1.x - p2.x) + abs(p1.y - p2.y)); }
ll crossProduct(Point a, Point b, Point c) {
  Point ab, bc;
  ab.x = b.x - a.x;
  ab.y = b.y - a.y;
  bc.x = c.x - b.x;
  bc.y = c.y - b.y;
  ll s = 1ll * (ab.x) * bc.y - 1ll * (bc.x) * ab.y;
  return s;
}
void findP0(int n, Point d[]) {
  p0 = d[1];
  for (int i = 2; i <= n; ++i)
    if (p0 < d[i])
      p0 = d[i];
}

bool compare(Point p1, Point p2) {
  ll o = crossProduct(p0, p1, p2);
  if (o == 0)
    return (dist(p0, p1)) < dist(p0, p2);
  return o > 0;
}
Point q[N];
int t;
void convex(int n, Point d[]) {
  findP0(n, d);
  sort(d + 1, d + 1 + n, compare);
  t = 1;
  q[1] = d[1];
  for (int i = 2; i <= n; ++i) {
    while (t > 1 && (crossProduct(q[t - 1], q[t], d[i]) <= 0))
      t--;
    q[++t] = d[i];
  }
}
ll kc(int i, int j) {
  Point a = q[i];
  Point b = q[j];
  return abs(a.x - b.x) + abs(a.y - b.y);
}
int nextVertex(int i) { return i % t + 1; }

void solve() {
  // write here
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  int n;
  cin >> n;
  for (int i = 1; i <= n; ++i)
    cin >> d[i].x >> d[i].y;

  convex(n, d);

  int j = 1;
  ll maxdist = 0;
  for (int i = 1; i < t; ++i) {
    while (kc(i, j) < kc(i, nextVertex(j)))
      j = nextVertex(j);
    maxdist = max(maxdist, kc(i, j));
  }
  cout << maxdist << "\\n";
  return;
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  // insert code

  solve();
  return 0;
}

Error: TLE

Fixed submission:

#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")
#include <bits/stdc++.h>
#define ll long long
#define pb push_back
#define pii pair<int, int>
#define vi vector<int>
#define vii vector<pii>
#define mi map<int, int>
#define mii map<pii, int>
#define all(a) (a).begin(), (a).end()
#define ff first
#define ss second
#define sz(x) (int)x.size()
#define endl '\\n'
#define hell 1000000007
#define rep(i, a, b) for (int i = a; i < b; i++)
using namespace std;

// Most important thing in CP - Have Fun :)

ll binpow(ll a, ll p) {
  ll res = 1;
  while (p > 0) {
    if (p & 1)
      res = (res * a) % hell;
    a = (a * a) % hell;
    p = p >> 1;
  }
  return res;
}

void solve() {
  // write here
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  ll n;
  cin >> n;
  vector<pair<ll, ll>> arr(n);
  rep(i, 0, n) { cin >> arr[i].ff >> arr[i].ss; }
  ll maxdist = 0;
  rep(i, 0, n) {
    rep(j, i + 1, n) {
      maxdist =
          max(maxdist, abs(arr[i].ff - arr[j].ff) + abs(arr[i].ss - arr[j].ss));
    }
  }
  cout << maxdist << endl;
  return;
}

int main() {
  cin.tie(NULL);
  ios::sync_with_stdio(false);
  // insert code

  solve();
  return 0;
}