hiho week 39 register

Ended

Participants:2159

Verdict:Accepted
Score:100 / 100
Submitted:2015-03-30 19:49:19

Lang:G++

Edit
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
#include <bits/stdc++.h>
using namespace std;
long long ans=0;
//lpos start of lefe half, rpos start of right half
void Merge(long long a[], long long tmp[], int lpos, int rpos, int rtend){
    int lftend, num_elements, tmp_pos;
    lftend=rpos-1;
    tmp_pos=lpos;
    num_elements=rtend-lpos+1;
    while(lpos<=lftend && rpos<=rtend){
        if( a[lpos]<= a[rpos]){
            tmp[tmp_pos++]=a[lpos++];
        }else{
            tmp[tmp_pos++]=a[rpos++];
            ans+=lftend-lpos+1;//get the answer
        }
    }
    while( lpos<= lftend )
        tmp[tmp_pos++]=a[lpos++];
    while( rpos<=rtend )
        tmp[tmp_pos++]=a[rpos++];
    for(int i=0; i<num_elements; i++, rtend--){
        a[rtend]=tmp[rtend];
    }
}
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX