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>
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
#define iter(id, v) for(auto id : v)
#define fs first
#define se second
#define MP make_pair
#define pb push_back
#define bit(msk, i) ((msk >> i) & 1)
#define SZ(v) (ll)v.size()
#define ALL(v) v.begin(),v.end()
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now ().time_since_epoch().count());
ll Rand (ll l, ll r) { return uniform_int_distribution<ll> (l, r) (rd); }
const int N = 3e5 + 2;
const int Mod = 1e9 + 7;
const ll INF = 1e16;
const ll BASE = 137;
const int szBL = 350;
struct fenwick_Tree {
int m;
int fen[N];
void init (int n) {
m = n;
}
void update (int pos, int val) {
for (; pos <= m; pos += pos & -pos) fen[pos] += val;
}
int get (int pos) {
int res = 0;
for (;pos > 0; pos -= pos & -pos) res += fen[pos];
return res;
}
int get (int u, int v) {
if (u > v) return 0;
return get(v) - get(u - 1);
}
}BIT;
int n;
int a[N];
pii b[N];
void solution () {
cin >> n;
rep (i, 1, n) cin >> a[i];
BIT.init(n);
rep (i, 1, n) b[i] = MP(a[i], i);
sort (b + 1, b + 1 + n);
ll res = 0;
rep (i, 1, n) {
int L = i, R = i;
while (R < n && b[R].fs == b[R + 1].fs) ++R;
// cout << L<<","<<R<<" asd\n";
rep (j, L, R) {
int val, pos;
tie(val, pos) = b[j];
res += 1LL * BIT.get(1, pos - 1) * BIT.get(pos + 1, n);
// cout <<pos<<" "<<BIT.get(1, pos - 1)<<" "<<BIT.get(pos + 1, n)<<"\n";
}
rep (j, L, R) {
int val, pos;
tie(val, pos) = b[j];
BIT.update(pos, 1);
}
i = R;
}
cout << res <<"\n";
}
#define file(name) freopen(name".inp","r",stdin); \
freopen(name".out","w",stdout);
int main () {
// file("DTREE");
ios_base :: sync_with_stdio(false); cin.tie(0); cout.tie(0);
int num_Test = 1;
// cin >> num_Test;
while (num_Test--)
solution();
}
/*
no bug +5
*/
# | 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... |