aboutsummaryrefslogtreecommitdiff
path: root/2020/day2/triangles.cpp
blob: c30cbc7f10394a342941ee7515645fe68396f4e1 (plain)
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
#include <bits/stdc++.h>
#define f first
#define s second
using namespace std;
typedef long long ll;
typedef pair<int, int> ii;
constexpr ll MOD = 1e9+7;

ii P[2002];

int main() {
	ifstream cin("triangles.in");
	ofstream cout("triangles.out");

	int N;
	cin >> N;
	for (int i = 0; i < N; ++i) cin >> P[i].f >> P[i].s;
	sort(P, P + N);

	ll ans = 0;
	for (int i = 0; i < N; ++i) {
		ii p = P[i];
		vector<ii> l, r;
		for (int j = 0; j < i; ++j) l.push_back(P[j]);
		for (int j = i + 1; j < N; ++j) r.push_back(P[j]);
		sort(l.begin(), l.end(), [&p](const ii& a, const ii& b) {
			int x = (a.f - p.f) * (b.s - p.s) - (a.s - p.s) * (b.f - p.f);
			if (x == 0) return (a.f - p.f) * (a.f - p.f) + (a.s - p.s) * (a.s - p.s) < (b.f - p.f) * (b.f - p.f) + (b.s - p.s) * (b.s - p.s);
			return x < 0;
		});
		sort(r.begin(), r.end(), [&p](const ii& a, const ii& b) {
			int x = (a.f - p.f) * (b.s - p.s) - (a.s - p.s) * (b.f - p.f);
			if (x == 0) return (a.f - p.f) * (a.f - p.f) + (a.s - p.s) * (a.s - p.s) < (b.f - p.f) * (b.f - p.f) + (b.s - p.s) * (b.s - p.s);
			return x < 0;
		});

		for (int j = 0, k = 0; j < i; ++j) {
			while (k < N - i - 1 && (l[j].f - p.f) * (r[k].s - p.s) - (l[j].s - p.s) * (r[k].f - p.f) <= 0) ++k;
			ans += (ll)(2 * j + N - 2 * i - 2 * k) * (p.s + l[j].s) * (p.f - l[j].f) + MOD * MOD;
			ans %= MOD;
		}
	}

	cout << ans << '\n';
}