#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MASK(i) (1LL << (i))
#define GETBIT(mask, i) (((mask) >> (i)) & 1)
#define ALL(v) (v).begin(), (v).end()
ll max(ll a, ll b){return (a > b) ? a : b;}
ll min(ll a, ll b){return (a < b) ? a : b;}
ll gcd(ll a, ll b){return __gcd(a, b);}
ll LASTBIT(ll mask){return (mask) & (-mask);}
int pop_cnt(ll mask){return __builtin_popcountll(mask);}
int ctz(ll mask){return __builtin_ctzll(mask);}
int logOf(ll mask){return __lg(mask);}
mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
ll rngesus(ll l, ll r){return l + (ull) rng() % (r - l + 1);}
template <class T1, class T2>
bool maximize(T1 &a, T2 b){
if (a < b) {a = b; return true;}
return false;
}
template <class T1, class T2>
bool minimize(T1 &a, T2 b){
if (a > b) {a = b; return true;}
return false;
}
template <class T>
void printArr(T& container, string separator = " ", string finish = "\n"){
for(auto item: container) cout << item << separator;
cout << finish;
}
template <class T>
void remove_dup(vector<T> &a){
sort(ALL(a));
a.resize(unique(ALL(a)) - a.begin());
}
struct FenwickTree{
int n;
vector<ll> a;
FenwickTree(int _n){
n = _n;
a.resize(n+1);
}
void update(int i, ll v){
while(i <= n){
a[i] += v;
i += LASTBIT(i);
}
}
ll get(int i){
ll ans = 0;
while(i > 0){
ans += a[i];
i -= LASTBIT(i);
}
return ans;
}
ll get(int l, int r){return get(r) - get(l-1);}
int binary_lift(ll x){
int p = MASK(logOf(n)), idx =0;
while(p > 0){
if (idx + p <= n && x > a[idx + p]){
idx += p;
x -= a[idx];
}
p >>= 1;
}
return idx + 1;
}
};
void solve(){
int n; cin >> n;
vector<int> a(n);
for(int i = 0; i<n; ++i) cin >> a[i];
vector<int> b = a; b.push_back(-1);
remove_dup(b);
for(int &i: a) i = lower_bound(ALL(b), i) - b.begin();
vector<int> pref(n), suff(n);
int m = b.size() - 1;
FenwickTree bit(m);
for(int i = 0; i < n; ++i){
bit.update(a[i], 1);
pref[i] = bit.get(a[i] - 1);
}
bit = FenwickTree(m);
for(int i = n-1; i>=0; --i){
bit.update(a[i], 1);
suff[i]= bit.get(a[i] - 1);
}
ll ans = 0;
for(int i = 0; i < n; ++i) ans += 1LL * pref[i] * suff[i];
cout << ans << "\n";
}
int main(void){
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
clock_t start = clock();
solve();
cerr << "Time elapsed: " << clock() - start << "ms!\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... |