#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace std;
using namespace __gnu_pbds;
// #pragma GCC target ("avx2")
// #pragma GCC optimize ("O3")
// #pragma GCC optimize ("unroll-loops")
// #pragma GCC optimize("fast-math")
#define ll long long
#define ld long double
#define vi vector<ll>
#define endl "\n"
#define pr pair<ll, ll>
#define ff first
#define ss second
#define all(x) x.begin(), x.end()
const int mod = 1e9+7;
void _p(ll a){cout<<a<<endl;}
void _p(string a){cout<<a<<endl;}
void _p(ld a) {cout<<a<<endl;}
template <class T>
void _p(vector<T> a){for(T val:a)cout<<val<<" ";cout<<endl;}
#define debug(x) cout<<#x<<" -> ";_p(x)
typedef tree<ll, null_type, less<ll>, rb_tree_tag, tree_order_statistics_node_update > ordered_set;
vector<pr> move8 = {{1, 0}, {1, 1}, {0, 1}, {-1, 1}, {-1, 0}, {-1, -1}, {0, -1}, {1, -1}};
vector<pr> move4 = {{1, 0}, {0, 1}, {-1, 0}, {0, -1}};
vector<pr> move2 = {{0, 1}, {1, 0}};
void solution();
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
solution();
return 0;
}
struct Node {
vi x;
Node* left;
Node* right;
Node() {left = nullptr; right = nullptr;}
};
struct WaveletTree{
ll n;
vi a;
vi idx;
Node* root;
WaveletTree(const vi &temp, ll mx) {
n = mx;
a = temp;
idx.resize(a.size());
iota(all(idx), 0);
root = new Node();
}
Node* build(ll l, ll r, vi &curr) {
Node* m = new Node();
m->x = curr;
if (l == r) {
return m;
}
ll mid = (l + r)/2;
vi L, R;
for (auto val : curr) {
if (a[val] <= mid) L.push_back(val);
else R.push_back(val);
}
m->left = build(l, mid, L);
m->right = build(mid+1, r, R);
return m;
}
ll binarySearch(vi& temp, ll l, ll r) {
return upper_bound(all(temp), r) - upper_bound(all(temp), l-1);
}
ll query(Node* curr, ll l, ll r, ll L, ll R, ll c, ll d) {
if (R < L) return 0;
if (c > r || l > d) return 0;
if (c <= l && d >= r) return binarySearch(curr->x, L, R);
ll mid = (l + r)/2;
ll a1 = query(curr->left, l, mid, L, R, c, d);
ll a2 = query(curr->right, mid+1, r, L, R, c, d);
return a1 + a2;
}
void build() {root = build(0, n, idx);}
ll query(ll l, ll r, ll c, ll d) {return query(root, 0, n, l, r, c, d);}
};
void solution() {
ll n; cin >> n;
vi a(n);
for (ll &z : a) cin >> z;
vi temp(a);
sort(all(temp));
temp.erase(unique(all(temp)), temp.end());
for (int i = 0; i < n; i++) a[i] = lower_bound(all(temp), a[i]) - temp.begin();
WaveletTree M(a, *max_element(all(a)));
M.build();
ll ans = 0;
for (int i = 1; i < n-1; i++) {
ans += M.query(0, i-1, 0, a[i]-1) * M.query(i+1, n, 0, a[i]-1);
}
cout << ans << endl;
}
| # | 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... |