Problem p03428 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include <chrono> //1e+9で割る auto end= chrono::system_clock::now()-st;
// cout<<end.count()%1e+9<<endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)

const double EPS = 1e-10;

double add(double a, double b) {
  if (abs(a + b) < EPS * (abs(a) + abs(b)))
    return 0;
  return a + b;
}

struct V {
  double x, y;
  V() {}
  V(double x, double y) : x(x), y(y) {}
  V operator+(V p) { return V(add(x, p.x), add(y, p.y)); }
  V operator-(V p) { return V(add(x, -p.x), add(y, -p.y)); }
  V operator*(double d) { return V(x * d, y * d); }
  bool operator==(V p) { return add(x, -p.x) == 0 && add(y, -p.y) == 0; }
  double dot(V p) { return add(x * p.x, y * p.y); }
  double det(V p) { return add(x * p.y, -y * p.x); }
};

bool on_seg(V v1, V v2, V q) { // v1-v2上にqがあるか判定
  return (v1 - q).det(v2 - q) == 0 && (v1 - q).dot(v2 - q) <= 0;
}

V intersection(V v1, V v2, V q1, V q2) { // 直線v1-v2とq1-q2の交点
  return v1 + (v2 - v1) * ((q2 - q1).det(q1 - v1) / (q2 - q1).det(v2 - v1));
}

bool cmp_x(const V &p, const V &q) {
  if (p.x != q.x)
    return p.x < q.x;
  else
    return p.y < q.y;
}

double dist(V p, V q) { return (p - q).dot(p - q); }

vector<V> cnv(V *vs, int n) {
  sort(vs, vs + n, cmp_x);
  int k = 0;
  vector<V> qs(n * 2);
  rep(i, n) {
    while (k > 1 && (qs[k - 1] - qs[k - 2]).det(vs[i] - qs[k - 1]) <= 0)
      k--;
    qs[k++] = vs[i];
  }
  for (int i = n - 2, t = k; i >= 0; i--) {
    while (k > t && (qs[k - 1] - qs[k - 2]).det(vs[i] - qs[k - 1]) <= 0)
      k--;
    qs[k++] = vs[i];
  }
  qs.resize(k - 1);
  return qs;
}

int N;
V v[100], tmp[100];
double ans[100];
int id[100];
double calc(V p1, V p2, V p3) {
  V s = p3 - p2, t = p1 - p2;
  while (s == t) {
    int i = 0;
    i++;
  }
  return (M_PI - acos(s.dot(t) / (sqrt(s.dot(s)) * sqrt(t.dot(t))))) /
         (2 * M_PI);
}
int main() {
  memset(id, -1, sizeof(id));
  cin >> N;
  rep(i, N) {
    cin >> v[i].x >> v[i].y;
    tmp[i] = v[i];
  }
  vector<V> nv = cnv(v, N);
  rep(i, N) {
    rep(j, nv.size()) {
      if (tmp[i] == nv[j]) {
        id[j] = i;
      }
    }
  }
  rep(i, nv.size()) {
    if (i == 0) {
      ans[id[i]] = calc(nv[nv.size() - 1], nv[0], nv[1]);
    } else if (i == nv.size() - 1) {
      ans[id[i]] = calc(nv[i - 1], nv[i], nv[0]);
    } else
      ans[id[i]] = calc(nv[i - 1], nv[i], nv[i + 1]);
  }
  rep(i, N) printf("%.50lf\n", ans[i]);
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <vector>
// #include <chrono> //1e+9で割る auto end= chrono::system_clock::now()-st;
// cout<<end.count()%1e+9<<endl;
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
typedef pair<ll, ll> LP;
typedef long double db;

#define fr first
#define sc second
#define pb push_back
#define rep(i, x) for (int i = 0; i < x; i++)
#define rep1(i, x) for (int i = 1; i <= x; i++)
#define rrep(i, x) for (int i = x - 1; i >= 0; i--)
#define rrep1(i, x) for (int i = x; i > 0; i--)

const double EPS = 1e-10;

double add(double a, double b) {
  if (abs(a + b) < EPS * (abs(a) + abs(b)))
    return 0;
  return a + b;
}

struct V {
  double x, y;
  V() {}
  V(double x, double y) : x(x), y(y) {}
  V operator+(V p) { return V(add(x, p.x), add(y, p.y)); }
  V operator-(V p) { return V(add(x, -p.x), add(y, -p.y)); }
  V operator*(double d) { return V(x * d, y * d); }
  bool operator==(V p) { return add(x, -p.x) == 0 && add(y, -p.y) == 0; }
  double dot(V p) { return add(x * p.x, y * p.y); }
  double det(V p) { return add(x * p.y, -y * p.x); }
};

bool on_seg(V v1, V v2, V q) { // v1-v2上にqがあるか判定
  return (v1 - q).det(v2 - q) == 0 && (v1 - q).dot(v2 - q) <= 0;
}

V intersection(V v1, V v2, V q1, V q2) { // 直線v1-v2とq1-q2の交点
  return v1 + (v2 - v1) * ((q2 - q1).det(q1 - v1) / (q2 - q1).det(v2 - v1));
}

bool cmp_x(const V &p, const V &q) {
  if (p.x != q.x)
    return p.x < q.x;
  else
    return p.y < q.y;
}

double dist(V p, V q) { return (p - q).dot(p - q); }

vector<V> cnv(V *vs, int n) {
  sort(vs, vs + n, cmp_x);
  int k = 0;
  vector<V> qs(n * 2);
  rep(i, n) {
    while (k > 1 && (qs[k - 1] - qs[k - 2]).det(vs[i] - qs[k - 1]) <= 0)
      k--;
    qs[k++] = vs[i];
  }
  for (int i = n - 2, t = k; i >= 0; i--) {
    while (k > t && (qs[k - 1] - qs[k - 2]).det(vs[i] - qs[k - 1]) <= 0)
      k--;
    qs[k++] = vs[i];
  }
  qs.resize(k - 1);
  return qs;
}

int N;
V v[100], tmp[100];
double ans[100];
int id[100];
double calc(V p1, V p2, V p3) {
  V s = p3 - p2, t = p1 - p2;
  if (s == t)
    return 0.5;
  return (M_PI - acos(s.dot(t) / (sqrt(s.dot(s)) * sqrt(t.dot(t))))) /
         (2 * M_PI);
}
int main() {
  memset(id, -1, sizeof(id));
  cin >> N;
  rep(i, N) {
    cin >> v[i].x >> v[i].y;
    tmp[i] = v[i];
  }
  vector<V> nv = cnv(v, N);
  rep(i, N) {
    rep(j, nv.size()) {
      if (tmp[i] == nv[j]) {
        id[j] = i;
      }
    }
  }
  rep(i, nv.size()) {
    if (i == 0) {
      ans[id[i]] = calc(nv[nv.size() - 1], nv[0], nv[1]);
    } else if (i == nv.size() - 1) {
      ans[id[i]] = calc(nv[i - 1], nv[i], nv[0]);
    } else
      ans[id[i]] = calc(nv[i - 1], nv[i], nv[i + 1]);
  }
  rep(i, N) printf("%.50lf\n", ans[i]);
}