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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
#define eps 1e-10
using namespace std;
const int maxn = 2e5 + 10;

struct Point {
  double x, y;
  Point(double x = 0, double 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); }
Vector operator/(Vector A, double p) { return Vector(A.x / p, A.y / p); }
bool operator<(const Point &A, const Point &B) {
  return A.x < B.x || (A.x == B.x && A.y < B.y);
}

int dcmp(double x) {
  if (fabs(x) < eps)
    return 0;
  return x < 0 ? -1 : 1;
}

bool operator==(const Point &A, const Point &B) {
  return dcmp(A.x - B.x) == 0 && dcmp(A.y - B.y) == 0;
}

double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) {
  return acos(Dot(A, B)) / Length(A) / Length(B);
}
double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }
double Area2(Point A, Point B, Point C) { return Cross(B - A, C - A); }
// 向量所在象限(点积符号,叉积符号)
Vector Rotate(Vector A, double rad) {
  return Vector(A.x * cos(rad) - A.y * sin(rad),
                A.x * sin(rad) + A.y * cos(rad));
}

Point ps[maxn];

vector<Point> convex_hull(Point *ps, int n) {
  vector<Point> hull(n * 2);
  int tp = 0;
  for (int i = 0; i < n; ++i) {
    while (tp > 1 &&
           dcmp(Cross(hull[tp - 1] - hull[tp - 2], ps[i] - hull[tp - 1])) <= 0)
      tp--;
    hull[tp++] = ps[i];
  }
  for (int i = n - 2, t = tp; i >= 0; --i) {
    while (tp > t &&
           dcmp(Cross(hull[tp - 1] - hull[tp - 2], ps[i] - hull[tp - 1])) <= 0)
      tp--;
    hull[tp++] = ps[i];
  }
  hull.resize(tp - 1);
  return hull;
}

long long dist(Point p) { return abs(p.x) + abs(p.y); }

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> ps[i].x >> ps[i].y;
  }
  sort(ps, ps + n);

  vector<Point> H = convex_hull(ps, n);
  int cnt = H.size();
  if (cnt == 2) {
    printf("%lld\\n", dist(H[0] - H[1]));
    return 0;
  }
  int i = 0, j = 0;
  for (int k = 0; k < cnt; ++k) {
    if (dcmp(H[i].x - H[k].x) > 0)
      i = k;
    if (dcmp(H[j].x - H[k].x) < 0)
      j = k;
  }
  long long res = 0;
  int si = i, sj = j;
  while (i != sj && j != si) {
    res = max(res, dist(H[j] - H[i]));
    if (dcmp(Cross(H[(i + 1) % cnt] - H[i], H[(j + 1) % cnt] - H[j]) < 0)) {
      i = (i + 1) % cnt;
    } else
      j = (j + 1) % cnt;
  }
  printf("%lld\\n", res);
  return 0;
}

Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <vector>
#define eps 1e-10
using namespace std;
const int maxn = 5e4 + 10;

struct Point {
  double x, y;
  Point(double x = 0, double 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); }
Vector operator/(Vector A, double p) { return Vector(A.x / p, A.y / p); }
bool operator<(const Point &A, const Point &B) {
  return A.x < B.x || (A.x == B.x && A.y < B.y);
}

int dcmp(double x) {
  if (fabs(x) < eps)
    return 0;
  return x < 0 ? -1 : 1;
}

bool operator==(const Point &A, const Point &B) {
  return dcmp(A.x - B.x) == 0 && dcmp(A.y - B.y) == 0;
}

double Dot(Vector A, Vector B) { return A.x * B.x + A.y * B.y; }
double Length(Vector A) { return sqrt(Dot(A, A)); }
double Angle(Vector A, Vector B) {
  return acos(Dot(A, B)) / Length(A) / Length(B);
}
double Cross(Vector A, Vector B) { return A.x * B.y - A.y * B.x; }
double Area2(Point A, Point B, Point C) { return Cross(B - A, C - A); }
// 向量所在象限(点积符号,叉积符号)
Vector Rotate(Vector A, double rad) {
  return Vector(A.x * cos(rad) - A.y * sin(rad),
                A.x * sin(rad) + A.y * cos(rad));
}

Point ps[maxn];

vector<Point> convex_hull(Point *ps, int n) {
  vector<Point> hull(n * 2);
  int tp = 0;
  for (int i = 0; i < n; ++i) {
    while (tp > 1 &&
           dcmp(Cross(hull[tp - 1] - hull[tp - 2], ps[i] - hull[tp - 1])) <= 0)
      tp--;
    hull[tp++] = ps[i];
  }
  for (int i = n - 2, t = tp; i >= 0; --i) {
    while (tp > t &&
           dcmp(Cross(hull[tp - 1] - hull[tp - 2], ps[i] - hull[tp - 1])) <= 0)
      tp--;
    hull[tp++] = ps[i];
  }
  hull.resize(tp - 1);
  return hull;
}

long long dist(Point p) { return abs(p.x) + abs(p.y); }

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; ++i) {
    cin >> ps[i].x >> ps[i].y;
  }
  sort(ps, ps + n);

  vector<Point> H = convex_hull(ps, n);
  int cnt = H.size();
  if (cnt == 2) {
    printf("%.0lf\\n", dist(H[0] - H[1]));
    return 0;
  }
  int i = 0, j = 0;
  for (int k = 0; k < cnt; ++k) {
    if (dcmp(H[i].x - H[k].x) > 0)
      i = k;
    if (dcmp(H[j].x - H[k].x) < 0)
      j = k;
  }
  long long res = 0;
  int si = i, sj = j;
  while (i != sj && j != si) {
    res = max(res, dist(H[j] - H[i]));
    if (dcmp(Cross(H[(i + 1) % cnt] - H[i], H[(j + 1) % cnt] - H[j]) < 0)) {
      i = (i + 1) % cnt;
    } else
      j = (j + 1) % cnt;
  }
  printf("%lld\\n", res);
  return 0;
}