POJ2299 树状数组求逆序对
生活随笔
收集整理的這篇文章主要介紹了
POJ2299 树状数组求逆序对
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
裸題,不多解釋。
1 #include<iostream> 2 #include<cstdio> 3 #include<algorithm> 4 #include<cstring> 5 using namespace std; 6 #define maxn 500005 7 int a[maxn],b[maxn],c[maxn]; 8 int n; 9 long long ans; 10 int lowbit(int x){ 11 return x&(-x); 12 } 13 void updata(int x){ 14 for(int i=x;i<=n;i+=lowbit(i)) 15 c[i]+=1; 16 } 17 int ask(int x){ 18 int tot=0; 19 for(int i=x;i>=1;i-=lowbit(i)) 20 tot+=c[i]; 21 return tot; 22 } 23 int main(){ 24 while(scanf("%d",&n),n){ 25 int cnt=0; 26 for(int i=1;i<=n;i++){ 27 scanf("%d",&a[i]); 28 b[i]=a[i]; 29 } 30 ans=0; 31 sort(a+1,a+n+1); 32 cnt=unique(a+1,a+n+1)-a-1; 33 memset(c,0,sizeof c); 34 for(int i=1;i<=n;i++){ 35 b[i]=lower_bound(a+1,a+cnt+1,b[i])-a; 36 updata(b[i]); 37 ans+=i-ask(b[i]); 38 } 39 printf("%I64d\n",ans); 40 } 41 }?
轉載于:https://www.cnblogs.com/Elfish/p/7655153.html
總結
以上是生活随笔為你收集整理的POJ2299 树状数组求逆序对的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Mac androidStudio ca
- 下一篇: c#基础知识第九节