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

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

typedef complex<double> C;

namespace std {
bool operator<(const C a, const C b) {
  return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
}
} // namespace std
//??£??????
C normalize(C c) { return c / abs(c); }

//?§????(rad)
double getarg(C a, C b) { return arg(b * conj(a)); }

//??????
double cross(const C a, const C b) { return imag(conj(a) * b); }
//??????
double dot(const C a, const C b) { return real(conj(a) * b); }

int ccw(C a, C b, C c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return +1; // counter clockwise
  if (cross(b, c) < 0)
    return -1; // clockwise
  if (dot(b, c) < 0)
    return +2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;    // b--a--c on line
}

vector<C> convex_hull(vector<C> ps) {
  int n = (int)ps.size(), k = 0;
  sort(ps.begin(), ps.end());
  vector<C> ch(2 * n);
  for (int i = 0; i < n; ch[k++] = ps[i++]) {
    while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
      k--;
  }
  for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) {
    while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
      k--;
  }
  ch.resize(k - 1);
  return ch;
}

int contains(const vector<C> &ps, const C p) {
  bool flag = false;
  rep(i, ps.size()) {
    C a = ps[i] - p, b = ps[(i + 1) % ps.size()] - p;
    if (imag(a) > imag(b))
      swap(a, b);
    if (imag(a) <= 0 && 0 < imag(b)) {
      if (cross(a, b) < 0)
        flag = !flag;
    }
    if (cross(a, b) == 0 && dot(a, b) <= 0)
      return 1;
  }
  return flag ? 2 : 0;
}

int main() {
  int n;
  cin >> n;
  vector<double> x(n), y(n);
  rep(i, n) { cin >> x[i] >> y[i]; }
  bool flag = 0;
  for (int i = 0; i < n; i++) {
    vector<C> ps;
    vector<C> conv;
    for (int j = 0; j < i; j++) {
      complex<double> z(x[j], y[j]);
      ps.push_back(z);
    }
    complex<double> z(x[i], y[i]);
    if (ps.size() > 2) {
      conv = convex_hull(ps);
      if (contains(conv, z) == 2) {
        flag = 1;
        break;
      }
    }
    vector<C> ps2;
    for (int j = i + 1; j < n; j++) {
      complex<double> z(x[j], y[j]);
      ps2.push_back(z);
    }
    if (ps2.size() > 2) {
      conv = convex_hull(ps2);
      if (contains(conv, z) == 2) {
        flag = 1;
        break;
      }
    }
  }
  if (flag) {
    cout << "Impossible" << endl;
  } else {
    cout << "Possible" << endl;
  }
  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::length_error'
  what():  vector::_M_default_append


Fixed submission:

#include <algorithm>
#include <cmath>
#include <complex>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <string>
#include <utility>
#include <vector>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
typedef vector<int> VI;
typedef vector<VI> VVI;
#define MP make_pair
#define PB push_back
#define inf 1000000007
#define rep(i, n) for (int i = 0; i < (int)(n); ++i)

typedef complex<double> C;

namespace std {
bool operator<(const C a, const C b) {
  return a.real() != b.real() ? a.real() < b.real() : a.imag() < b.imag();
}
} // namespace std
//??£??????
C normalize(C c) { return c / abs(c); }

//?§????(rad)
double getarg(C a, C b) { return arg(b * conj(a)); }

//??????
double cross(const C a, const C b) { return imag(conj(a) * b); }
//??????
double dot(const C a, const C b) { return real(conj(a) * b); }

int ccw(C a, C b, C c) {
  b -= a;
  c -= a;
  if (cross(b, c) > 0)
    return +1; // counter clockwise
  if (cross(b, c) < 0)
    return -1; // clockwise
  if (dot(b, c) < 0)
    return +2; // c--a--b on line
  if (norm(b) < norm(c))
    return -2; // a--b--c on line
  return 0;    // b--a--c on line
}

vector<C> convex_hull(vector<C> ps) {
  int n = (int)ps.size(), k = 0;
  sort(ps.begin(), ps.end());
  vector<C> ch(2 * n);
  for (int i = 0; i < n; ch[k++] = ps[i++]) {
    while (k >= 2 && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
      k--;
  }
  for (int i = n - 2, t = k + 1; i >= 0; ch[k++] = ps[i--]) {
    while (k >= t && ccw(ch[k - 2], ch[k - 1], ps[i]) <= 0)
      k--;
  }
  ch.resize(k - 1);
  return ch;
}

int contains(const vector<C> &ps, const C p) {
  bool flag = false;
  rep(i, ps.size()) {
    C a = ps[i] - p, b = ps[(i + 1) % ps.size()] - p;
    if (imag(a) > imag(b))
      swap(a, b);
    if (imag(a) <= 0 && 0 < imag(b)) {
      if (cross(a, b) < 0)
        flag = !flag;
    }
    if (cross(a, b) == 0 && dot(a, b) <= 0)
      return 1;
  }
  return flag ? 2 : 0;
}

int main() {
  int n;
  cin >> n;
  vector<double> x(n), y(n);
  rep(i, n) { cin >> x[i] >> y[i]; }
  bool flag = 0;
  for (int i = 1; i < n; i++) {
    vector<C> ps;
    vector<C> conv;
    for (int j = 0; j < i; j++) {
      complex<double> z(x[j], y[j]);
      ps.push_back(z);
    }
    complex<double> z(x[i], y[i]);
    conv = convex_hull(ps);
    if (contains(conv, z) == 2) {
      flag = 1;
      break;
    }
    vector<C> ps2;
    for (int j = i + 1; j < n; j++) {
      complex<double> z(x[j], y[j]);
      ps2.push_back(z);
    }
    conv = convex_hull(ps2);
    if (contains(conv, z) == 2) {
      flag = 1;
      break;
    }
  }
  if (flag) {
    cout << "Impossible" << endl;
  } else {
    cout << "Possible" << endl;
  }
  return 0;
}