关于三维偏序 O(n log n) 的做法
回顾题意
给出三个长为
其中
相信这个题
考虑
可能到这里还看不出来什么,继续来搞,如果
代码
#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
int n, a[1000005], b[1000005], c[1000005];
LL tot;
struct node {
int tree[1000005];
void mem() { fill(tree + 1, tree + 1000000 + 1, 0); }
void add(int x, int y) {
while (x <= n) {
tree[x] += y;
x += (x & (-x));
}
}
int query(int x) {
int ans = 0;
while (x) {
ans += tree[x];
x -= (x & (-x));
}
return ans;
}
} bit;
struct Node {
int X, Y;
} k[1000005];
bool cmp(Node A, Node B) { return A.X < B.X; }
LL calc() {
sort(k + 1, k + n + 1, cmp);
bit.mem();
LL Ans = 0;
for (int i = 1; i <= n; i++) {
Ans += bit.query(k[i].Y);
bit.add(k[i].Y, 1);
}
return Ans;
}
int main() {
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
for (int i = 1; i <= n; i++) scanf("%d", &b[i]);
for (int i = 1; i <= n; i++) scanf("%d", &c[i]);
for (int i = 1; i <= n; i++) k[i].X = a[i], k[i].Y = b[i];
tot += calc();
for (int i = 1; i <= n; i++) k[i].X = a[i], k[i].Y = c[i];
tot += calc();
for (int i = 1; i <= n; i++) k[i].X = b[i], k[i].Y = c[i];
tot += calc();
printf("%lld", (tot - (1ll * n * (n - 1) / 2)) / 2);
return 0;
}