-
Notifications
You must be signed in to change notification settings - Fork 0
/
3359-ancient_towers.cpp
135 lines (97 loc) · 2.93 KB
/
3359-ancient_towers.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
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
template <class T, unsigned int N>
struct BIT {
void clear(const int n) { fill(bit, bit + n, 0); }
T sum(int l, int r) { return sum(r) - sum(l - 1); }
void add(unsigned int idx, const T delta) {
for (; idx < N; idx = idx | (idx + 1)) bit[idx] += delta;
}
private:
T bit[N];
T sum(int r) {
T ret = 0;
for (; r >= 0; r = (r & (r + 1)) - 1) ret += bit[r];
return ret;
}
};
struct Point {
ll x, y;
int id;
inline Point operator-(const Point& p) const { return {x - p.x, y - p.y, id}; }
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;
}
} points[401];
int N, idx_map[401];
ll S, total, concave_count;
BIT<short, 401> active;
void count_all(const vector<Point>& points) {
const int n = points.size();
for (int i = 0; i < n; i++) {
const Point diagonal = points[i];
vector<pair<ll, int>> areas;
for (const Point& p : points) {
const ll area = diagonal.cross(p);
if (area < 0) areas.push_back({-area, p.id});
}
if (areas.empty()) continue;
const int first_i = (i + 1) % n;
if (diagonal.cross(points[first_i]) < 0) continue;
Point p1, p2;
sort(areas.begin(), areas.end());
for (int area_idx = 0; area_idx < (int)areas.size(); area_idx++) {
const auto [_, point_id] = areas[area_idx];
idx_map[point_id] = area_idx;
}
int k = first_i;
for (;; k = (k + 1) % n) {
p2 = points[k];
if (diagonal.cross(p2) < 0) break;
}
active.clear(areas.size());
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;
active.add(idx_map[p2.id], 1);
k = (k + 1) % n;
p2 = points[k];
}
const ll complement = S * 2L - diagonal.cross(p1);
const int idx = lower_bound(areas.begin(), areas.end(), make_pair(complement, -1)) -
areas.begin();
total += areas.size() - idx;
concave_count += active.sum(idx, areas.size() - 1);
}
}
}
void solve() {
for (int i = 0; i < N; i++) {
cin >> points[i].x >> points[i].y;
points[i].id = i;
}
total = 0;
concave_count = 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);
}
total /= 2L;
const ll convex_polygons = (total - concave_count) / 2L;
cout << total - convex_polygons << endl;
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
while (cin >> S >> N) solve();
}