Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- #define MAXN 100005
- long long merge(int A[],int left,int mid,int right)
- {
- int i=left,j=mid,k=0;
- int temp[right-left+1];
- long long count=0;
- while(i<mid&&j<=right)
- {
- if(A[i]<=A[j]){
- temp[k++]=A[i++];
- }
- else{
- temp[k++]=A[j++];
- count+=mid-i;
- }
- }
- while(i<mid){
- temp[k++]=A[i++];
- }
- while(j<=right){
- temp[k++]=A[j++];
- }
- for(int i=left,k=0;i<=right;i++,k++){
- A[i]=temp[k];}
- return count;
- }
- long long merge_sort(int A[],int left,int right){
- long long count=0;
- if(right>left){
- int mid=(left+right)/2;
- long long countleft=merge_sort(A,left,mid);
- long long countright=merge_sort(A,mid+1,right);
- long long mycount=merge(A,left,mid+1,right);
- return mycount+countleft+countright;
- }
- return count;
- }
- long long solve(int A[],int n)
- {
- long long ans=merge_sort(A,0,n-1);
- return ans;
- }
- int main()
- {
- int n,A[MAXN];
- cin>>n;
- for(int i = 0; i < n ; i++){
- cin>>A[i];
- }
- cout<<solve(A,n)<<endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement