This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
using namespace std;
const int N = (int) 3e5 + 10;
int n;
long long a[N];
int L[N], R[N];
int tree[N];
void Update(int idx, int val) {
for( ; idx <= n; idx += (idx & - idx)) tree[idx] += val;
}
int Query(int idx) {
int res = 0;
for( ; idx > 0; idx -= (idx & - idx)) res += tree[idx];
return res;
}
void compress() {
vector<long long> Pos = {};
for (int i = 1; i <= n; ++ i) {
Pos.push_back(a[i]);
}
sort(Pos.begin(), Pos.end());
Pos.resize(unique(Pos.begin(), Pos.end()) - Pos.begin());
for (int i = 1; i <= n; ++ i) {
a[i] = lower_bound(Pos.begin(), Pos.end(), a[i]) - Pos.begin() + 1;
}
}
void calc(int cnt[]) {
for (int i = 0; i <= n; ++ i) tree[i] = 0;
for (int i = 1; i <= n; ++ i) {
cnt[i] = Query(a[i]);
Update(a[i] + 1, 1);
}
}
int main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr); cout.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; ++ i) cin >> a[i];
compress();
calc(L);
reverse(a + 1, a + 1 + n);
calc(R);
long long res = 0;
for (int i = 1; i <= n; ++ i) res += 1ll * L[i] * R[n - i + 1];
cout << res;
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... |