Test instructions: Using tree-like array to find the inverse number;
Idea: Because the input range is large, discretization first, the resulting array A records the size of the original array, and then the order to execute Add (a[i],1), so that sum (A[i]) is less than or equal to A[i] number, i-sum (A[i]) is a[i] front than a[i ] The number of large numbers//outer loop n times and accumulate I-sum (A[i]) to get reverse number
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6typedefLong LongLL;7 8 structnode{9 intV,order;Ten}a[500050]; One A BOOLCMP (Node A, Node B) { - returnA.V <B.V; - } the - intaa[500050],c[500050]; - intN; - + intLowbit (intx) - { + returnX & (-x); A } at - intSumintx) { - intRET =0; - while(X >0) - { -RET + = c[x]; X-=lowbit (x); in } - returnret; to } + - voidAddintXintd) { the while(x <=N) { *C[X] + = D; X + =lowbit (x); $ }Panax Notoginseng } - the intMain () + { A while(SCANF ("%d", &n) = =1&&N) the { + for(inti =1; I <= N; ++i) - { $scanf"%d",&a[i].v); $A[i].order =i; - } -Sort (A +1, a+n+1, CMP); the for(inti =1; I <= N; ++i) - {WuyiAa[a[i].order] =i; the } -Memset (c,0,sizeof(c)); WuLL ans =0; - for(inti =1; I <= N; ++i) About { $Add (Aa[i],1); -Ans + =-sum (aa[i]); - } -printf"%lld\n", ans); A } + return 0; the}
poj2299 tree-like array entry question