1009 Triple Inversions
Statement
Metadata
- 作者: CAO, Peng
- 单位: Google
- 代码长度限制: 16 KB
- 时间限制: 300 ms
- 内存限制: 64 MB
Given a list of
Now we have a new challenging problem. You are supposed to count the number of triple inversions in it. As you may guess, a triple inversion is defined as a triple of indices
Input Specification
Each input file contains one test case. For each case, the first line gives a positive integer
Output Specification
For each case, print in a line the number of triple inversions in the list.
Sample Input
Sample Output
Solution
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const int N = 1e5 + 10;
int n, a[N];
struct BIT {
ll a[N];
void init() {
memset(a, 0, sizeof a);
}
void update(int x, ll v) {
for (; x < N; x += x & -x) a[x] += v;
}
ll query(int x) {
ll res = 0;
for (; x > 0; x -= x & -x) res += a[x];
return res;
}
ll query(int l, int r) {
if (l > r)
return 0;
return query(r) - query(l - 1);
}
} bit[2];
int main() {
while (scanf("%d", &n) != EOF) {
for (int i = 1; i <= n; ++i) scanf("%d", a + i);
ll res = 0;
for (int i = 1; i <= n; ++i) {
bit[0].update(a[i], 1);
bit[1].update(a[i], bit[0].query(a[i] + 1, n));
res += bit[1].query(a[i] + 1, n);
}
printf("%lld\n", res);
}
return 0;
}