Previously written in a tree-like array, then use the line tree to write a bit ~ ~ ~
1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <cmath>6#include <vector>7 using namespacestd;8 #defineLP (P << 1)9 #defineRP (P << 1 | 1)Ten #defineGetmid (L,r) (L + (R-L)/2) One A Const intMAXN =50005; - structnode{ - intl,r,s; the}t[4*MAXN]; - - intA[maxn],n; - + intCalcintAintb) {returnA +b;} - + voidPUSH_UP (intp) { AT[P].S =Calc (t[lp].s,t[rp].s); at } - - voidBuild_tree (intPintLintR) { -T[P].L =l; -T[P].R =R; - if(L = = r) {T[P].S =0;return;} in intMID =Getmid (l,r); - Build_tree (lp,l,mid); toBuild_tree (rp,mid+1, R); + push_up (p); - } the * voidUpdateintPintSintW) {//add W to s dot $ if(T[P].L = =T[P].R) {Panax NotoginsengT[p].s + =W; - return; the } + intMID =Getmid (T[P].L,T[P].R); A if(S <=mid) Update (LP,S,W); the ElseUpdate (RP,S,W); + push_up (P); - } $ $ intSumintPintLintR) { - if(T[P].L = = L && T[P].R = = r)returnT[p].s; - the intMID =Getmid (T[P].L,T[P].R); - if(R <= Mid)returnsum (lp,l,r);Wuyi Else if(Mid < L)returnsum (rp,l,r); the Else returnCalc (sum (lp,l,mid), SUM (rp,mid+1, R)); - } Wu - intMain () { About intT; $ while(SCANF ("%d", &n)! =EOF) { - for(inti =1; I <= n;i++) scanf ("%d",&A[i]), a[i]; -Build_tree (1,1, n); - A intAns =0; + for(inti =1; I <= n;i++){ theAns + = SUM (1, a[i]+1, n); Update (1, A[i],1); - //printf ("ans =%d\n", ans); $ } the the intres =ans; the for(inti =1; I <= n;i++){ theAns + = n2*a[i]-1; -res =min (res,ans); in //printf ("res =%d\n", res); the } theprintf"%d\n", res); About } the return 0; the}
View Code
HDU 1394 Minimum Inversion number "segment tree in reverse order"