#define ll long long #define I inline #define N 100001
usingnamespacestd;
int n , m , s;
namespace Tree_array{ ll tree[N];
intlowbit(int x){ return x & (- x); }
I voidadd(int x , ll val){ for(int i = x; i <= n; i += lowbit(i)){ tree[i] += val; } }
I ll query(int x){ ll ans = 0; for(int i = x; i ; i -= lowbit(i)){ ans += tree[i]; } return ans; }
}
ll a[N] , b[N] , ans = 0;
I voidinput(){ //离散化输入数据 cin >> n; for(int i = 1; i <= n; i ++){ cin >> a[i]; b[i] = a[i]; } sort(b + 1 , b + 1 + n); ll number = unique(b + 1 , b + 1 + n) - b - 1; for(int i = 1; i <= n; i ++){ a[i] = lower_bound(b + 1 , b + 1 + number , a[i]) - b; } }
intmain(){ input(); ll l = 1 , r = 1; for(int i = 1; i <= n; i ++){ //手动模拟,找出单调下降序列 if(i < n && a[i] > a[i + 1]){ r ++; } else{ if(l < r){ while(l < r){ swap(a[l] , a[r]); l ++ , r --; } ans ++; } l = r = i + 1; } } for(int i = 1; i <= n; i ++){ //树状数组求逆序对数 Tree_array :: add(a[i] , 1); ans += i - Tree_array :: query(a[i]); } cout << ans << "\n"; return0; }