Used to do this kind of problem with tree array and line tree
This time change a thought to try, merge sort!
#include <cstdio> #include <cstring> #include <algorithm>using namespace std;typedef long LL; const int MAXN = 111111;int n;int array[maxn];int TMP[MAXN]; LL ans;void my_sort (int l,int r) {if (L = = r) return; int mid = (L + r) >> 1; My_sort (L,mid); My_sort (mid + 1,r); int cnt = 0,I,J; for (i = L,j = mid + 1;i <= mid && J <= R;) {if (Array[i] <= array[j]) {tmp[cnt++] = Arra Y[i++]; Ans + = (j-mid-1); } else tmp[cnt++] = array[j++]; } while (I <= mid) {tmp[cnt++] = array[i++]; Ans + = (j-mid-1); } while (J <= R) {tmp[cnt++] = array[j++]; } for (int i = 0,J = l; i < CNT; i++,j++) array[j] = Tmp[i];} int main () {while (scanf ("%d", &n)! = EOF) {ans = 0; for (int i = 0; i < n; i++) scanf ("%d", &array[i]); My_sort (0,n-1);//for (int i = 0; i < n; i++)//printf ("%d", aRray[i]);//Puts (""); printf ("%i64d\n", ans); } return 0;}
"SGU" 180. Inversions (merge sort to find the reverse order number)