http://acm.hdu.edu.cn/showproblem.php?pid=1394
A very basic line-segment tree.
The first query in the update, if the next number than the previous number is definitely queried to the previous updated values, the return sum is the number of reverse order of the current number.
In this way, the inverse number of the initial sequence is obtained, and the minimum number of reverse order of all sequences is required, and it needs to be cycled once.
Set the initial sequence of ABCDE in the reverse number is k, less than a number is t-1 so more than a number is n-t, when a left to move a bit, the original is larger than a is the reverse of a, that is, the reverse number increased n-t, but the original than a small number has become the order,
So the number of reverse order decreases t-1, so the reverse number of the new sequence is k+=n-t-t+1;
1#include <iostream>2#include <cstdio>3#include <cmath>4#include <vector>5#include <cstring>6#include <string>7#include <algorithm>8#include <string>9#include <Set>Ten#include <functional> One#include <numeric> A#include <sstream> -#include <stack> -#include <map> the#include <queue> -#include <deque> - //#pragma COMMENT (linker, "/stack:102400000,102400000") - #defineCL (arr, Val) memset (arr, Val, sizeof (arr)) + - #definell Long Long + #defineINF 0x7f7f7f7f A #defineLC L,m,rt<<1 at #defineRC M + 1,r,rt<<1|1 - #definePi ACOs (-1.0) - - #defineL (x) (x) << 1 - #defineR (x) (x) << 1 | 1 - #defineMID (L, R) (L + R) >> 1 in #defineMin (x, y) (x) < (y)? (x): (y) - #defineMax (x, y) (x) < (y)? (y): (x) to #defineE (x) (1 << (x)) + #defineIabs (x) (x) < 0? -(x): (x) - #defineOut (x) printf ("%i64d\n", X) the #defineLowbit (x) (x) & (-X) * #defineRead () freopen ("A.txt", "R", stdin) $ #defineWrite () freopen ("B.txt", "w", stdout);Panax Notoginseng #defineMAXN 5010 - #defineMAXV 50010 the #defineMoD 1000000000 + using namespacestd; A #defineLson l,m,rt<<1 the #defineRson m+1,r,rt<<1|1 + - intsum[maxn<<2]; $ $ voidPushup (intRT) - { -sum[rt]=sum[rt<<1]+sum[rt<<1|1]; the } - Wuyi voidBuildintLintRintRT) the { -sum[rt]=0; Wu if(L==R)return; - intM= (l+r) >>1; About build (Lson); $ build (Rson); - } - - voidUpdateintPintLintRintRT) A { + if(l==R) the { -sum[rt]++; $ return; the } the intM= (l+r) >>1; the if(p<=m) update (P,lson); the ElseUpdate (P,rson); - pushup (RT); in } the the intQueryintLintRintLintRintRT) About { the if(l<=l&&r<=R) the { the returnSum[rt]; + } - intM= (l+r) >>1; the intret=0;Bayi if(l<=m) ret+=query (L,r,lson); the if(r>m) ret+=query (L,r,rson); the returnret; - } - the intX[MAXN]; the intMain () the { the //Read (); - intN; the while(~SCANF ("%d",&N)) the { theBuild0, N-1,1);94 intans=0; the for(intI=0; i<n;i++) the { thescanf"%d",&x[i]);98Ans+=query (x[i],n-1,0, N-1,1); AboutUpdate (X[i],0, N-1,1); - }101 intret=ans;102 for(intI=0; i<n;i++)103 {104ans+=n-x[i]-x[i]-1; theret=min (ret,ans);106 }107printf"%d\n", ret);108 }109 return 0; the}
hdu-1394 Minimum inversion Number (line tree water problem)