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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 200050;
struct point {
  ll x, y;
  point(ll x0 = 0, ll y0 = 0) : x(x0), y(y0) {}
};
point p[maxn], res[maxn];
int n;

int cmp(point a, point b) { return a.y < b.y || (a.y == b.y && a.x < b.x); }
ll cross(point a, point b, point c) {
  return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
ll dist(point a, point b) { return abs(b.x - a.x) + abs(b.y - a.y); }
int convex(int n) {
  int now = 1;
  for (int i = 1; i <= n; i++) {
    while (now > 2 && (cross(res[now - 2], p[i], res[now - 1])) >= 0)
      now--;
    res[now++] = p[i];
  }
  int k = now;
  for (int i = n - 1; i >= 1; i--) {
    while (now > k && (cross(res[now - 2], p[i], res[now - 1])) >= 0)
      now--;
    res[now++] = p[i];
  }
  if (n > 1)
    now--;
  return now - 1;
}
ll caliper(int now) {
  int cnt = 2;
  ll ans = 0;
  for (int i = 1; i <= now; i++) {
    while ((cross(res[i], res[i + 1], res[cnt + 1]) -
            cross(res[i], res[i + 1], res[cnt])) > 0)
      cnt = cnt % now + 1;
    ll d1 = dist(res[i], res[cnt]);
    ll d2 = dist(res[i + 1], res[cnt]);
    ans = max(ans, max(d1, d2));
  }
  return ans;
}

void solve() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld %lld", &p[i].x, &p[i].y);
  sort(p + 1, p + n + 1, cmp);
  int now = convex(n);
  printf("%lld\\n", caliper(now));
}
int main() {
  solve();
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
using namespace std;
typedef long long ll;
const int maxn = 200050;
struct point {
  ll x, y;
  point(ll x0 = 0, ll y0 = 0) : x(x0), y(y0) {}
};
point p[maxn], res[maxn];
int n;

int cmp(point a, point b) { return a.y < b.y || (a.y == b.y && a.x < b.x); }
ll cross(point a, point b, point c) {
  return (b.x - a.x) * (c.y - a.y) - (b.y - a.y) * (c.x - a.x);
}
ll dist(point a, point b) { return abs(b.x - a.x) + abs(b.y - a.y); }
int convex(int n) {
  int now = 1;
  for (int i = 1; i <= n; i++) {
    while (now > 2 && (cross(res[now - 2], p[i], res[now - 1])) > 0)
      now--;
    res[now++] = p[i];
  }
  int k = now;
  for (int i = n - 1; i >= 1; i--) {
    while (now > k && (cross(res[now - 2], p[i], res[now - 1])) > 0)
      now--;
    res[now++] = p[i];
  }
  if (n > 1)
    now--;
  return now - 1;
}
ll caliper(int n) {
  ll ans = 0;
  for (int i = 1; i <= n; i++) {
    int j = i + 1, k = j;
    while ((cross(res[i], res[j], res[k + 1]) - cross(res[i], res[j], res[k])) >
           0)
      k = k % n + 1;
    while (i != k && i != j) {
      while ((cross(res[i], res[j], res[k + 1]) -
              cross(res[i], res[j], res[k])) > 0 &&
             i != k)
        k = k % n + 1;
      ans =
          max(ans, max(max(dist(res[i], res[j]), dist(res[i], res[j % n + 1])),
                       max(dist(res[i % n + 1], res[j]),
                           dist(res[i % n + 1], res[j % n + 1]))));
      j = j % n + 1;
    }
  }
  return ans;
}

void solve() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++)
    scanf("%lld %lld", &p[i].x, &p[i].y);
  sort(p + 1, p + n + 1, cmp);
  int now = convex(n);
  printf("%lld\\n", caliper(now));
}
int main() {
  solve();
  return 0;
}