-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path03_Count_Number_of_Trapezoids_II.cpp
More file actions
55 lines (41 loc) · 1.28 KB
/
Copy path03_Count_Number_of_Trapezoids_II.cpp
File metadata and controls
55 lines (41 loc) · 1.28 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
// 3625. Count Number of Trapezoids II
class Solution
{
public:
int countTrapezoids(vector<vector<int>> &points)
{
unordered_map<int, unordered_map<int, int>> t, v;
for (int i = 0; i < points.size(); ++i)
{
for (int j = i + 1; j < points.size(); ++j)
{
int dx = points[j][0] - points[i][0];
int dy = points[j][1] - points[i][1];
if (dx < 0 || (dx == 0 && dy < 0))
dx = -dx, dy = -dy;
int g = std::gcd(dx, std::abs(dy));
int sx = dx / g;
int sy = dy / g;
int des = sx * points[i][1] - sy * points[i][0];
int key1 = (sx << 12) | (sy + 2000);
int key2 = (dx << 12) | (dy + 2000);
++t[key1][des];
++v[key2][des];
}
}
return count(t) - count(v) / 2;
}
int count(unordered_map<int, unordered_map<int, int>> &mp)
{
long long ans = 0;
for (auto &[k1, inner] : mp)
{
long long sum = 0;
for (auto &[k2, val] : inner)
sum += val;
for (auto &[k2, val] : inner)
ans += val * (sum -= val);
}
return ans;
}
};