# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
223807 | bortoz | Mountains (NOI20_mountains) | C++17 | 0 ms | 0 KiB |
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;
typedef long long ll;
#define fi first
#define se second
constexpr int MAXN = 1 << 19;
int fw[MAXN];
void add(int p, int v) {
for (p++; p < MAXN; p += p & -p) {
fw[p] += v;
}
}
int sum(int p) {
int r = 0;
for (p++; p > 0; p -= p & -p) {
r += fw[p];
}
return r;
}
int main() {
ios::sync_with_stdio(false);
cin.tie(0);
int N;
cin >> N;
vector<ll> V(N);
map<ll, int> M;
for (ll& i : V) {
cin >> i;
M[i] = 0;
}
int x = 0;
for (auto& [k, v] : M) {
v = x++;
}
for (ll& i : V) {
i = M[i];
}
dbg(V);
vector<int> L(N), R(N);
for (int i = 0; i < N; i++) {
L[i] = sum(V[i] - 1);
add(V[i], 1);
}
fill(fw, fw + MAXN, 0);
for (int i = N - 1; i >= 0; i--) {
R[i] = sum(V[i] - 1);
add(V[i], 1);
}
ll res = 0;
for (int i = 0; i < N; i++) {
res += 1ll * L[i] * R[i];
}
cout << res << endl;
return 0;
}