forked from ngthanhtrung23/CompetitiveProgramming
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathA.cpp
More file actions
176 lines (143 loc) · 4.68 KB
/
A.cpp
File metadata and controls
176 lines (143 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
#include <bits/stdc++.h>
#define int long long
#define FOR(i,a,b) for(int i=(a),_b=(b); i<=_b; ++i)
#define FORD(i,a,b) for(int i=(a),_b=(b); i>=_b; --i)
#define REP(i,a) for(int i=0,_a=(a); i < _a; ++i)
#define DEBUG(X) { cout << #X << " = " << (X) << endl; }
#define PR(A,n) { cout << #A << " = "; FOR(_,1,n) cout << A[_] << ' '; cout << endl; }
#define PR0(A,n) { cout << #A << " = "; REP(_,n) cout << A[_] << ' '; cout << endl; }
#define sqr(x) ((x) * (x))
#define ll long long
#define __builtin_popcount __builtin_popcountll
#define SZ(x) ((int) (x).size())
using namespace std;
int RI(int& x) {
return scanf("%lld", &x);
}
const int MN = 600111;
struct Point {
int x, y;
Point() { x = y = 0; }
Point(int x, int y) : x(x), y(y) {}
Point operator - (const Point& a) const {
return Point(x-a.x, y-a.y);
}
Point operator + (const Point& a) const {
return Point(x+a.x, y+a.y);
}
int operator % (const Point& a) const {
return x*a.y - y*a.x;
}
void read() {
RI(x); RI(y);
}
int sqlen() const { return x*x + y*y; }
};
bool operator == (const Point& a, const Point& b) {
return a.x == b.x && a.y == b.y;
}
bool operator != (const Point& a, const Point& b) {
return a.x != b.x || a.y != b.y;
}
int ccw(const Point &a, const Point &b, const Point &c) {
int t = (b - a) % (c - a);
if (t == 0) return 0;
if (t < 0) return -1;
return 1;
}
int n;
Point a[MN];
int RE_TRAI = ccw(Point(0, 0), Point(0, 1), Point(-1, 1));
int RE_PHAI = ccw(Point(0, 0), Point(0, 1), Point(1, 1));
bool cmp(const Point& u, const Point& v) {
int t = ccw(a[1], u, v);
if (t == RE_TRAI) return true;
if (t == RE_PHAI) return false;
return (u - a[1]).sqlen() < (v - a[1]).sqlen();
}
void answer(int a, int b) {
int g = __gcd(a, b);
cout << (a/g) << '/' << (b/g) << endl;
}
bool sameLine() {
FOR(i,3,n) if (ccw(a[1], a[2], a[i])) return 0;
return 1;
}
int st[MN], top;
int st2[MN], top2;
#undef int
int main() {
#define int long long
freopen("average.in", "r", stdin);
freopen("average.out", "w", stdout);
while (RI(n) == 1) {
FOR(i,1,n) a[i].read();
FOR(i,2,n) if (a[i].y < a[1].y || (a[i].y == a[1].y && a[i].x < a[1].x)) swap(a[1], a[i]);
sort(a+2, a+n+1, cmp);
if (sameLine()) {
answer(2, 1);
continue;
}
int last = n;
while (last > 2 && ccw(a[1], a[last], a[last-1]) == 0) --last;
reverse(a+last, a+n+1);
FOR(i,n+1,n+n+n) a[i] = a[i-n];
st[1] = 1;
st[2] = 2;
top = 2;
FOR(i,3,n) {
while (top >= 2 && ccw(a[st[top-1]], a[st[top]], a[i]) != RE_TRAI) {
--top;
}
st[++top] = i;
}
while (top >= 2 && ccw(a[st[top-1]], a[st[top]], a[1]) != RE_TRAI) --top;
assert(top >= 3);
int res = top * (n - top);
st[top+2] = st[2];
st[top+1] = st[1];
st[0] = st[top];
FOR(turn,2,top) {
int u = st[turn-1] + 1;
int v = st[turn+1] - 1;
if (v < u) v += n;
top2 = 2;
st2[1] = st[turn-2];
st2[2] = st[turn-1];
FOR(i,u,v) if (a[i] != a[st[turn]]) {
while (ccw(a[st2[top2-1]], a[st2[top2]], a[i]) != RE_TRAI) --top2;
assert(top2 >= 2);
st2[++top2] = i;
}
while (top2 >= 3 && ccw(a[st2[top2-1]], a[st2[top2]], a[st[turn+1]]) != RE_TRAI)
--top2;
int cur = top - 1 + top2 - 2;
if (cur < 2) cur = 2;
// cout << "REM " << a[st[turn]].x << ' ' << a[st[turn]].y << endl;
// DEBUG(cur);
res += cur;
}
swap(a[n], a[1]); --n;
FOR(i,2,n) if (a[i].y < a[1].y || (a[i].y == a[1].y && a[i].x < a[1].x)) swap(a[1], a[i]);
sort(a+2, a+n+1, cmp);
if (sameLine()) res += 2;
else {
int last = n;
while (last > 2 && ccw(a[1], a[last], a[last-1]) == 0) --last;
reverse(a+last, a+n+1);
FOR(i,n+1,n+n+n) a[i] = a[i-n];
st[1] = 1;
st[2] = 2;
top = 2;
FOR(i,3,n) {
while (top >= 2 && ccw(a[st[top-1]], a[st[top]], a[i]) != RE_TRAI) {
--top;
}
st[++top] = i;
}
while (top >= 2 && ccw(a[st[top-1]], a[st[top]], a[1]) != RE_TRAI) --top;
res += top;
}
answer(res, n+1);
}
}