-
Notifications
You must be signed in to change notification settings - Fork 0
/
CNVX4HUL.cpp
216 lines (161 loc) · 5.03 KB
/
CNVX4HUL.cpp
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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
short sgn(const ll n) {
if (n < 0) return -1;
return n > 0;
}
struct Point {
ll x, y;
int idx;
inline Point operator-(const Point& p) const { return {x - p.x, y - p.y, idx}; }
inline ll cross(const Point& p) const { return x * p.y - y * p.x; }
inline bool is_above() const { return y > 0 || (y == 0 && x > 0); }
inline bool operator<(const Point& p) const {
return is_above() != p.is_above() ? is_above() : cross(p) > 0;
}
Point to(const Point& p) const { return p - *this; }
bool inside_triangle(const Point A, const Point B, const Point C) const {
const short o1 = sgn(A.to(B).cross(A.to(*this)));
const short o2 = sgn(B.to(C).cross(B.to(*this)));
const short o3 = sgn(C.to(A).cross(C.to(*this)));
return o1 == o2 && o2 == o3;
}
} points[307];
int N;
const ll MOD = 1e9 + 7;
int points_in_triangle[302][302];
ll two_power[307];
ll total, total_concave;
ll points_inside_aux(const int i, const int j, const int k) {
return points_in_triangle[j][k] - points_in_triangle[i][j] - points_in_triangle[i][k] -
1;
}
ll points_inside(const int i, const int j, const int k) {
if (i == 0) return points_in_triangle[j][k];
if (j == 0) return points_in_triangle[i][k];
if (k == 0) return points_in_triangle[i][j];
const Point& A = points[i];
const Point& B = points[j];
const Point& C = points[k];
const Point& O = points[0];
if (O.inside_triangle(A, B, C)) {
return points_in_triangle[i][j] + points_in_triangle[i][k] +
points_in_triangle[j][k] + 1;
}
if (A.inside_triangle(O, B, C)) return points_inside_aux(i, j, k);
if (B.inside_triangle(O, A, C)) return points_inside_aux(j, i, k);
if (C.inside_triangle(O, A, B)) return points_inside_aux(k, i, j);
array<Point, 3> vec{A - O, B - O, C - O};
sort(vec.begin(), vec.end(),
[](const Point& v1, const Point& v2) { return v1.cross(v2) > 0; });
const int a = vec[0].idx;
const int b = vec[1].idx;
const int c = vec[2].idx;
return points_in_triangle[a][b] + points_in_triangle[b][c] - points_in_triangle[a][c];
}
void precompute_points_inside_triangles() {
for (int i = 1; i < N; i++) {
for (int j = i + 1; j < N; j++) {
for (int p = 1; p < N; p++) {
if (p == i || p == j) continue;
if (points[p].inside_triangle(points[0], points[i], points[j]))
points_in_triangle[i][j]++;
}
points_in_triangle[j][i] = points_in_triangle[i][j];
}
}
}
void count_all(const vector<Point>& points, const int center_idx) {
const int n = points.size();
for (int i = 0; i < n; i++) {
const Point diagonal = points[i];
ll all = 0;
for (const Point& p : points) {
if (diagonal.cross(p) < 0) {
const ll inside = points_inside(center_idx, diagonal.idx, p.idx);
all = (all + two_power[inside]) % MOD;
}
}
if (all == 0) continue;
const int first_i = (i + 1) % n;
if (diagonal.cross(points[first_i]) < 0) continue;
Point p1, p2;
int k = first_i;
for (;; k = (k + 1) % n) {
p2 = points[k];
if (diagonal.cross(p2) < 0) break;
}
ll concave = 0;
for (int j = first_i;; j++) {
p1 = points[j % n];
if (diagonal.cross(p1) < 0) break;
while (diagonal.cross(p2) < 0) {
if (p1.cross(p2) < 0) break;
const ll inside = points_inside(center_idx, diagonal.idx, p2.idx);
concave = (concave + two_power[inside]) % MOD;
k = (k + 1) % n;
p2 = points[k];
}
const ll inside_p1 = points_inside(center_idx, diagonal.idx, p1.idx);
total = (total + all * two_power[inside_p1]) % MOD;
total_concave = (total_concave + concave * two_power[inside_p1]) % MOD;
}
}
}
ll gcd_extended(ll a, ll b, ll& x, ll& y) {
if (a == 0) {
x = 0, y = 1;
return b;
}
ll x1, y1;
ll gcd = gcd_extended(b % a, a, x1, y1);
x = y1 - (b / a) * x1;
y = x1;
return gcd;
}
ll mod_inverse(ll b, ll m) {
ll x, y;
ll g = gcd_extended(b, m, x, y);
if (g != 1) return -1;
return (x % m + m) % m;
}
ll mod_divide(ll a, ll b, ll m) {
a = a % m;
ll inv = mod_inverse(b, m);
assert(~inv);
return (inv * a) % m;
}
void solve() {
for (int i = 0; i < N; i++) {
cin >> points[i].x >> points[i].y;
points[i].idx = i;
}
memset(points_in_triangle, 0, sizeof points_in_triangle);
precompute_points_inside_triangles();
total = 0;
total_concave = 0;
for (int i = 0; i < N; i++) {
vector<Point> centered_points;
for (int j = 0; j < N; j++) {
if (i == j) continue;
centered_points.push_back(points[j] - points[i]);
}
sort(centered_points.begin(), centered_points.end());
count_all(centered_points, i);
}
total += (-2 * total_concave) + MOD;
total = mod_divide(total, 4, MOD) + MOD;
total %= MOD;
cout << total << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
two_power[0] = 1;
for (int i = 1; i <= 305; i++) {
two_power[i] = two_power[i - 1] * 2;
two_power[i] %= MOD;
}
while (cin >> N) solve();
}