A:逆序数
写过一篇原理,这里不想多说。 传送门
代码:
1 #include<bits/stdc++.h> 2 using namespace std; 3 #define LL long long 4 #define ULL unsigned LL 5 #define fi first 6 #define se second 7 #define lson l,m,rt<<1 8 #define rson m+1,r,rt<<1|1 9 #define max3(a,b,c) max(a,max(b,c)) 10 const int INF = 0x3f3f3f3f; 11 const LL mod = 1e9+7; 12 typedef pair<int,int> pll; 13 const int N = 100000+10; 14 int tree[N]; 15 int lowbit(int x) 16 { 17 return x&(-x); 18 } 19 void add(int x) 20 { 21 while(x < N) 22 { 23 tree[x]++; 24 x += lowbit(x); 25 } 26 } 27 int Query(int x) 28 { 29 int ret = 0; 30 while(x) 31 { 32 ret += tree[x]; 33 x -= lowbit(x); 34 } 35 return ret; 36 } 37 int main() 38 { 39 ios::sync_with_stdio(false); 40 cin.tie(0); 41 cout.tie(0); 42 int n; 43 cin >> n; 44 LL ans = 0; 45 while(n--) 46 { 47 int tmp; 48 cin >> tmp; 49 tmp++; 50 ans = ans + Query(N-1) - Query(tmp); 51 add(tmp); 52 } 53 cout << ans << endl; 54 return 0; 55 }