#include <bits/stdc++.h>
using namespace std;
#define int long long
struct FenwickTree {
vector<int> bit;
int n;
FenwickTree(int n) {
this -> n = n;
bit.assign(n + 1, 0);
}
int query(int k) {
int res = 0;
for (; k >= 1; k -= k & -k) {
res += bit[k];
}
return res;
}
int query(int l, int r) {
return query(r) - query(l - 1);
}
void update(int k, int d) {
for (; k <= n; k += k & -k) {
bit[k] += d;
}
}
};
signed main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> h(n);
for (int i = 0; i < n; i++) {
cin >> h[i];
}
auto b = h;
sort(b.begin(), b.end());
for (int i = 0; i < n; i++) {
h[i] = lower_bound(b.begin(), b.end(), h[i]) - b.begin() + 1;
}
FenwickTree left(n + 5), right(n + 5);
for (int i = 0; i < n; i++) {
right.update(h[i], 1);
}
int res = 0;
for (int i = 0; i < n; i++) {
res += left.query(h[i] - 1) * right.query(h[i] - 1);
left.update(h[i], 1);
right.update(h[i], -1);
}
cout << res << '\n';
return 0;
}
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |