-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathN1_2016_F2_Arco_e_Flecha.cpp
99 lines (73 loc) · 1.48 KB
/
N1_2016_F2_Arco_e_Flecha.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
// OBI 2016 - Nível Sênior - Fase 2: Arco e flecha (https://olimpiada.ic.unicamp.br/pratique/ps/2016/f2/arco-online/)
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
struct Point { ll x, y; };
class BITree {
private:
vector<ll> ts;
int N;
public:
BITree(int n) : ts(n + 1, 0), N(n) {}
ll RSQ(int i, int j)
{
return RSQ(j) - RSQ(i - 1);
}
private:
int LSB(int n) { return n & (-n); }
ll RSQ(int i)
{
ll sum = 0;
while (i >= 1)
{
sum += ts[i];
i -= LSB(i);
}
return sum;
}
public:
void add(int i, ll x)
{
if (i == 0)
return;
while (i <= N)
{
ts[i] += x;
i += LSB(i);
}
}
};
ll solve(int N, const vector<Point>& ps)
{
vector<ll> dist(N);
set<ll> ds;
for (int i = 0; i < N; ++i)
{
dist[i] = ps[i].x * ps[i].x + ps[i].y * ps[i].y;
ds.insert(dist[i]);
}
map<ll, int> idx;
int j = 0;
for (auto d : ds)
idx[d] = ++j;
BITree tree(N);
ll ans = 0;
for (int i = 0; i < N; ++i)
{
ans += tree.RSQ(0, idx[dist[i]]);
tree.add(idx[dist[i]], 1);
}
return ans;
}
int main()
{
ios::sync_with_stdio(false);
int N;
cin >> N;
vector<Point> ps(N);
for (int i = 0; i < N; ++i)
cin >> ps[i].x >> ps[i].y;
auto ans = solve(N, ps);
cout << ans << '\n';
return 0;
}