1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
|
#include <bits/stdc++.h> using namespace std; using ll = long long;
const int maxn = 5e5 + 50; int n; int a[ maxn ], b[ maxn ], c[ maxn ]; ll ans = 0;
int lowbit( int x ) { return x & -x; }
void add( int x, int v ) { for( int i = x; i <= n; i += lowbit( i ) ) { c[i] += v; } }
int query( int x ) { int res = 0; for( int i = x; i; i -= lowbit( i ) ) { res += c[i]; } return res; }
int main( ) { ios::sync_with_stdio( 0 ); cin.tie( 0 );
cin >> n; for( int i = 1; i <= n; ++ i ) { cin >> a[i]; b[i] = a[i]; }
sort( b + 1, b + n + 1 ); int m = unique( b + 1, b + n + 1 ) - b - 1;
for( int i = 1; i <= n; ++ i ) { int rank = lower_bound( b + 1, b + m + 1, a[i] ) - b; ans += ( i - 1 - query( rank ) ); add( rank, 1 ); }
cout << ans << endl;
return 0; }
|