Problem p02556 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
struct Point {
  ll x, y;
  Point(ll x = 0, ll y = 0) : x(x), y(y) {}
};

typedef Point Vector;

Vector operator+(Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator-(Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator*(Vector A, double p) { return Vector(A.x * p, A.y * p); }

ll Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
ll Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }
ll Length(Vector A) { return Dot(A, A); }

const int maxn = 2e5 + 10;
Point p[maxn], sol[maxn];
ll n, m;

bool cmp(Point a, Point b) {
  ll ans = Cross(a - p[0], b - p[0]);
  return ans > 0 || ans == 0 && Length(a - p[0]) < Length(b - p[0]);
}

ll ConvexHull(Point *p, Point *ch) {
  sort(p + 1, p + n, cmp);
  ll m = 0;
  for (ll i = 0; i < n; i++) {
    while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)
      m--;
    ch[m++] = p[i];
  }

  ll k = m;
  for (ll i = n - 2; i >= 0; i--) {
    while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)
      m--;
    ch[m++] = p[i];
  }

  if (n > 1)
    m--;
  return m;
}
ll f(Point A, Point B) { return abs(A.x - B.x) + abs(A.y - B.y); }
ll Rotate_calipers(Point *ch, int n) {
  ll q = 1;
  ll ans = 0;
  ch[n] = ch[0];
  for (ll i = 0; i < n; i++) {
    while (Cross(ch[q + 1] - ch[i], ch[q + 1] - ch[i + 1]) >
           Cross(ch[q] - ch[i], ch[q] - ch[i + 1]))
      q = (q + 1) % n;
    ans = max(ans, max(f(ch[i], ch[q]), f(ch[i + 1], ch[q + 1])));
    // ans = max( ans,max( Length(ch[i]-ch[q]),Length(ch[i+1]-ch[q+1]) ) );
  }
  return ans;
}

int main() {
  while (scanf("%lld", &n) == 1) {
    ll x, y;
    for (ll i = 0; i < n; i++) {
      scanf("%lld%lld", &x, &y);
      p[i] = Point(x, y);
    }

    if (n == 2) {
      printf("%lld\\n", f(p[0], p[1]));
      continue;
    }

    for (ll i = 1; i < n; i++) {
      if (p[i].x < p[0].x || p[i].x == p[0].x && p[i].y < p[0].y)
        swap(p[i], p[0]);
    }

    // sort(p+1,p+n,cmp);
    // for (int i=0; i<n; i++) printf("%d %d\\n",p[i].x,p[i].y);

    m = ConvexHull(p, sol);
    // printf("%d\\n",m);
    printf("%lld\\n", Rotate_calipers(sol, m));
  }
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cstdio>
using namespace std;
typedef long long ll;
struct Point {
  ll x, y;
  Point(ll x = 0, ll y = 0) : x(x), y(y) {}
};

typedef Point Vector;

Vector operator+(Vector A, Vector B) { return Vector(A.x + B.x, A.y + B.y); }
Vector operator-(Vector A, Vector B) { return Vector(A.x - B.x, A.y - B.y); }
Vector operator*(Vector A, double p) { return Vector(A.x * p, A.y * p); }

ll Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
ll Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }
ll Length(Vector A) { return Dot(A, A); }

const int maxn = 50000 + 10;
Point p[maxn], sol[maxn];
ll n, m;

bool cmp(Point a, Point b) {
  ll ans = Cross(a - p[0], b - p[0]);
  return ans > 0 || ans == 0 && Length(a - p[0]) < Length(b - p[0]);
}

ll ConvexHull(Point *p, Point *ch) {
  sort(p + 1, p + n, cmp);
  ll m = 0;
  for (ll i = 0; i < n; i++) {
    while (m > 1 && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)
      m--;
    ch[m++] = p[i];
  }

  ll k = m;
  for (ll i = n - 2; i >= 0; i--) {
    while (m > k && Cross(ch[m - 1] - ch[m - 2], p[i] - ch[m - 2]) <= 0)
      m--;
    ch[m++] = p[i];
  }

  if (n > 1)
    m--;
  return m;
}
ll f(Point A, Point B) { return abs(A.x - B.x) + abs(A.y - B.y); }
ll Rotate_calipers(Point *ch, int n) {
  ll q = 1;
  ll ans = 0;
  ch[n] = ch[0];
  for (ll i = 0; i < n; i++) {
    while (Cross(ch[q + 1] - ch[i], ch[q + 1] - ch[i + 1]) >
           Cross(ch[q] - ch[i], ch[q] - ch[i + 1]))
      q = (q + 1) % n;
    ans = max(ans, max(f(ch[i], ch[q]), f(ch[i + 1], ch[q + 1])));
    // ans = max( ans,max( Length(ch[i]-ch[q]),Length(ch[i+1]-ch[q+1]) ) );
  }
  return ans;
}

int main() {
  while (scanf("%lld", &n) == 1) {
    ll x, y;
    for (ll i = 0; i < n; i++) {
      scanf("%lld%lld", &x, &y);
      p[i] = Point(x, y);
    }

    if (n == 2) {
      printf("%lld\\n", Length(p[0] - p[1]));
      continue;
    }

    for (ll i = 1; i < n; i++) {
      if (p[i].x < p[0].x || p[i].x == p[0].x && p[i].y < p[0].y)
        swap(p[i], p[0]);
    }

    // sort(p+1,p+n,cmp);
    // for (int i=0; i<n; i++) printf("%d %d\\n",p[i].x,p[i].y);

    m = ConvexHull(p, sol);
    // printf("%d\\n",m);
    printf("%lld\\n", Rotate_calipers(sol, m));
  }
  return 0;
}