http://poj.org/problem?id=2299
Test instructions
Give a set of numbers, in order to reverse the pair.
Ideas:
This problem can be solved in a tree-like array, but prior to this, the data needs to be preprocessed.
The data of this topic can be as large as 999,999,999, but the array must not open so big, but each group of data up to only 500,000, then, how to do, discretization!
Discretization, that is, the data and 1~n do one by one mapping.
Like what:
9 1 0) 5 4
Discretization then becomes
5 2 1) 4 3
In this way, you can safely open the array!
As for the calculation of the tree-like array, I am too lazy to write a direct excerpt of the http://www.cnblogs.com/shenshuyang/archive/2012/07/14/2591859.html of the great God.
based on the intermediate results of discrete results, the process of calculating the inverse number is a process. 1, enter 5, call Update (5,1), set the 5th bit to 11 2 3 4 50 0 0 0 1Calculation 1-Is there a number on the 5 that is smaller than 5? This uses the getsum of the tree-like array (5) =1 operation, now with the input subscript 1-Getsum (5) =0you can get an inverse number of 5 for 0. 2. Enter 2 to call Update (2,1), set the 2nd bit to 11 2 3 4 50 1 0 0 1Calculation 1-Is there a number on the 2 that is smaller than 2? This uses the getsum of the tree-like array (2) =1 operation, now with the input subscript 2-Getsum (2) =1you can get an inverse number of 2 for 1. 3. Enter 1 to call Update (1,1), set the 1th bit to 11 2 3 4 51 1 0 0 1Calculation 1-Is there a number on the 1 that is smaller than 1? This uses the getsum of the tree-like array (1) =1 operation, now with the input subscript3-Getsum (1) =2you can get an inverse number of 1 for 2. 4. Enter 4 to call Update (4,1), set the 5th bit to 11 2 3 4 51 1 0 1 1Calculation 1-Is there a number on the 4 that is smaller than 4? This uses the getsum of the tree-like array (4) =3 operation, now with the input subscript 4-Getsum (4) =1you can get an inverse number of 4 for 1. 5. Enter 3 to call Update (3,1), set the 3rd bit to 11 2 3 4 51 1 1 1 1Calculation 1-Is there a number on the 3 that is smaller than 3? This uses the getsum of the tree-like array (3) =3 operation, now with the input subscript 5-Getsum (3) =2you can get an inverse number of 3 for 2. 6.0+1+2+1+2=6This is the final reverse number.
1#include <iostream>2#include <algorithm>3#include <cstring>4#include <cstdio>5#include <vector>6#include <queue>7#include <cmath>8#include <map>9#include <stack>Ten using namespacestd; One A Const intmaxn=500000+5; - - intN; the - structnode - { - intVal; + intPos; - }A[MAXN]; + A intB[MAXN]; at intC[MAXN]; - - BOOLCMP (node A,node b) - { - returna.val<B.val; - } in - intLowbit (intx) to { + returnx&-x; - } the * intSumintx) $ {Panax Notoginseng intret=0; - while(x>0) the { +ret+=C[x]; Ax-=lowbit (x); the } + returnret; - } $ $ voidAddintXintd) - { - while(x<=N) the { -c[x]+=D;Wuyix+=lowbit (x); the } - } Wu - intMain () About { $ //freopen ("D:\\input.txt", "R", stdin); - while(~SCANF ("%d", &n) &&N) - { - for(intI=1; i<=n;i++) A { +scanf"%d",&a[i].val); thea[i].pos=i; - } $Sort (A +1, A +1+n,cmp); the for(intI=1; i<=n;i++) theb[a[i].pos]=i; theMemset (c,0,sizeof(c)); the Long Longans=0; - for(intI=1; i<=n;i++) in { theAdd (B[i],1); theans+=i-sum (b[i]); About } theprintf"%lld\n", ans); the } the return 0; +}
POJ 2299 Ultra-quicksort (tree-like array + discretization)