统计序列中在元素左边比该元素小的元素个数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#include<bits/stdc++.h>
#define MAX 100
using namespace std;
#define lowbit(i) ((i)&(-i))
//覆盖范围
int c[MAX];
int n;
void update(int x,int v){
for(int i=x;i<=n;i+=lowbit(i)){
c[i]+=v;
}
}
int getSum(int x){
int sum=0;
for(int i=x;i>0;i-=lowbit(i))
{
sum+=c[i];
}
return sum;
}
int main(){
cin>>n;
int x;
memset(c,0,sizeof(c));
int num=0;
for(int i=1;i<=n;i++){
cin>>x;
update(x,1);//x出现次数+1
int t=getSum(x-1);
num+=t;
cout<<"第"<<i<<"位左边比它小的数的个数"<<t<<endl;
}
cout<<"sum:"<<num<<endl;
}