publicstaticlongsamllSum_merge(int[] arr, int low, int high){ if(low == high) return0; // 计算mid的位置 int mid = low + (( high - low ) >> 1); // 这样可以避免溢出,而且使用了位运算,效率更高 return samllSum_merge(arr, low, mid) + samllSum_merge(arr, mid+1, high) + merge(arr, low, mid, high); } // 归并两个有序的数组 publicstaticlongmerge(int[]arr, int low, int mid, int high){ int[] help = newint[high - low + 1]; // 注意此处数组的大小 long result = 0; int p = low; int q = mid + 1; int k = 0; while (p <= mid && q <= high){ if(arr[p] <= arr[q]){ // 左边比又变小,产生小和 result += arr[p] * ( high - q + 1); help[k++] = arr[p++]; }elseif(arr[p] > arr[q]){ help[k++] = arr[q++]; } } while(p <= mid){ help[k] = arr[p++]; k++; } while(q <= high){ help[k] = arr[q++]; k++; } // copy for(int i = 0; i < high - low + 1; i++){ arr[low + i] = help[i]; } return result; }
publicstaticvoidmain(String[] args){
Scanner scanner = new Scanner(System.in); int n = scanner.nextInt(); int[] arr = newint[n]; for(int i = 0; i <n; i++){ arr[i] = scanner.nextInt(); } long sum = samllSum_merge(arr,0, arr.length -1); System.out.println(sum);