-
Notifications
You must be signed in to change notification settings - Fork 3
/
camels.cpp
65 lines (57 loc) · 1.45 KB
/
camels.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
#include <bits/stdc++.h>
using namespace std;
typedef vector<int> vi;
typedef long long ll;
int left(int p) { return p << 1; }
int right(int p) { return (p << 1) + 1; }
class SegTree {
public:
vi st, v;
SegTree(const vi &A, const vi &B) : st(4 * A.size(), 0), v(A.size()) {
vi ord(A.size());
for (int i = 0; i < A.size(); ++i) ord[A[i]] = i;
for (int i = 0; i < A.size(); ++i) v[i] = ord[B[i]];
}
void insert(int p, int L, int R, int i) {
if (i > R || i < L) return;
++st[p];
if (L == R) return;
int mid = (L + R) >> 1;
insert(left(p), L, mid, i);
insert(right(p), mid + 1, R, i);
}
ll query(int p, int L, int R, int l, int r) {
if (l > R || r < L) return 0;
if (l <= L && R <= r) return st[p];
int mid = (L + R) >> 1;
return query(left(p), L, mid, l, r) + \
query(right(p), mid + 1, R, l, r);
}
ll num_inv() {
ll res = 0, j = v.size() - 1;
for (int i = 0; i <= j; ++i) {
res += query(1, 0, j, 0, v[i]);
insert(1, 0, j, v[i]);
}
return res;
}
};
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
int n;
cin >> n;
vi A(n), B(n), C(n);
for (auto &e : A) cin >> e, --e;
for (auto &e : B) cin >> e, --e;
for (auto &e : C) cin >> e, --e;
SegTree T[] = { SegTree(A, B), SegTree(A, C), SegTree(B, C) };
ll res = -n;
res *= (n - 1);
res >>= 1;
for (int i = 0; i < 3; ++i) {
res += T[i].num_inv();
}
printf("%lld\n", res / 2);
return 0;
}