方法描述
排序+多指针遍历,复杂度O(n)
代码
#include<iostream>
#include<algorithm>
using namespace std;
const int N = 1e5 + 5;
int a[N];
int b[N];
int c[N];
int main()
{
int n;
scanf("%d", &n);
for (int i = 0; i < n; i++)
scanf("%d", &a[i]);
for (int i = 0; i < n; i++)
scanf("%d", &b[i]);
for (int i = 0; i < n; i++)
scanf("%d", &c[i]);
sort(a, a + n);
sort(b, b + n);
sort(c, c + n);
long long res = 0;
long long pa = 0, pb = 0, pc = 0;
for (pb = 0; pb < n; ++pb)
{
while (pc < n && c[pc] <= b[pb])
{
++pc;
}
while (pa < n && a[pa] < b[pb])
{
++pa;
}
res += pa * (n - pc);
}
printf("%lld\n", res);
return 0;
}