The topic is to let you ask for reverse number, with a tree-like array is very simple, but the data is too large, to be discretized first, the data range compressed to 1~n within. and POJ doesn't support c++11, it's killing me. Lambda expression compilation error.
#include <iostream> #include <sstream> #include <fstream> #include <string> #include <map > #include <vector> #include <list> #include <set> #include <stack> #include <queue># Include <deque> #include <algorithm> #include <functional> #include <iomanip> #include < limits> #include <new> #include <utility> #include <iterator> #include <cstdio> #include < cstdlib> #include <cstring> #include <cctype> #include <cmath> #include <ctime>using namespace Std;const int maxn = 500010;int BIT[MAXN], n;struct st{int num, index;}; ST a[maxn];//tree-like array sum int sum (int i) {int s = 0; while (i > 0) {s + = bit[i]; I &= i-1; } return s;} The tree array modifies the element void add (int i, int x) {while (I <= N) {Bit[i] + = x; i + = i & (-i); }}int Main () {while (scanf ("%d", &n) = = 1 && N) {memset (bit, 0, sizeof (bit));for (int i = 0; i < n; ++i) {scanf ("%d", &a[i].num); A[i].index = i; }//Discretization of sort (A, a+n, [] (St A, St B) {return a.num < b.num;}); int cnt = 0, tmp; for (int i = 0; i < n; ++i) if (!i | | a[i].num = TMP) {tmp = A[i].num; A[i].num = ++cnt; } else {tmp = A[i].num; A[i].num = CNT; } sort (A, a+n, [] (St A, St B) {return A.index < b.index;}); Tree array for inverse number long long ans = 0; for (int i = 0; i < n; ++i) {ans + = i-sum (a[i].num); Add (a[i].num, 1); } cout << ans << endl; } return 0;}
Copyright NOTICE: This article is the original blogger articles, reproduced please indicate the source.
poj2299 (Ultra-quicksort) tree-like array + discretization