poj1741,POJ 2299Ultra-QuickSort

 2023-10-18 阅读 24 评论 0

摘要:題意:線段樹求逆序對經典題目,需要離散處理,但是用stl處理的話會T,手動二分處理即可; #include<algorithm> #include<iostream> #include<map> #include<set> #include<vector> #include<queue> #inc

題意:線段樹求逆序對經典題目,需要離散處理,但是用stl處理的話會T,手動二分處理即可;

#include<algorithm>
#include<iostream>
#include<map>
#include<set>
#include<vector>
#include<queue>
#include<stack>
#include<cstring>
#include<cstdio>
#define N 500005
using namespace std;
typedef struct node{int x;int y;int date;
}node;
bool cmp(int a,int b){return a>b;
}
node a[4*N];
void built(int root,int first,int end){if(first==end){a[root].x=first;a[root].y=end;a[root].date=0;return ;}int mid=(first+end)/2;built(root*2,first,mid);built(root*2+1,mid+1,end);a[root].x=a[root*2].x;a[root].y=a[root*2+1].y;a[root].date=0;
}
void U(int root,int first,int end,int e){if(first==end){a[root].date++;return ;}int mid=(first+end)/2;if(e<=mid)  U(root*2,first,mid,e);else U(root*2+1,mid+1,end,e);a[root].date=a[root*2].date+a[root*2+1].date;
}
long long sum=0;
void Q(int root,int first,int end,int l,int r){if(l<=first&&end<=r){sum+=a[root].date;return ;}int mid=(first+end)/2;if(l<=mid)  Q(root*2,first,mid,l,r);if(r>mid)  Q(root*2+1,mid+1,end,l,r);
}
long long b[N];
long long c[N];
long long e[N];
long long p[N];
int main(){int m;while(scanf("%d",&m)==1&&m!=0){for(int i=1;i<=m;i++){scanf("%d",&b[i]);p[i]=b[i];}sort(b+1,b+m+1,cmp);e[1]=b[1];int k=1;for(int i=2;i<=m;i++){if(b[i]!=b[i-1]){k++;e[k]=b[i];}}for(int i=1;i<=m;i++){int first=1;int end=k;while(first<=end){int mid=(first+end)/2;if(p[i]==e[mid]){c[i]=mid;break;}else if(p[i]<e[mid]){first=mid+1;}else{end=mid-1;}}}built(1,1,k);long long ans=0;for(int i=1;i<=m;i++){sum=0;if(c[i]==1){U(1,1,k,c[i]);}else{Q(1,1,k,1,c[i]-1);ans+=sum;U(1,1,k,c[i]);}}printf("%lld\n",ans);}return 0;
}


轉載于:https://www.cnblogs.com/wang9897/p/7624402.html

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/5/146383.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息