Link: poj 2299 ultra-quicksort
After finding the number of Adjacent Elements in a series, it is a series of ascending values.
Idea: Calculate the reverse number of the string sequence. The data is relatively large and needs to be discretization.
AC code:
# Include <stdio. h> # include <string. h> # include <set> # include <map> # include <algorithm> # define ll _ int64using namespace STD; const ll maxn = 500010; ll N, C [maxn], A [maxn]; struct node {ll Val, Id ;}; struct node P [maxn]; ll lowbit (ll x) {return X & (-x );} void Update (ll x, ll I) {While (x <= N) {C [x] + = I; x + = lowbit (x );}} ll sum (ll x) {ll ret = 0; while (x> 0) {RET + = C [X]; X-= lowbit (x);} return ret ;} bool CMP (node A, Node B) {return. val <B. Val;} int main () {ll I; while (scanf ("% i64d", & N )! = EOF, n) {memset (A, 0, sizeof A); memset (C, 0, sizeof C); for (I = 1; I <= N; I ++) {scanf ("% i64d", & P [I]. val); P [I]. id = I;} Sort (p + 1, P + n + 1, CMP); for (I = 1; I <= N; I ++) // discretization A [p [I]. id] = I; ll temp, ANS = 0; for (I = 1; I <= N; I ++) {update (A [I], 1 ); temp = sum (A [I]); ans + = I-temp;} printf ("% i64d \ n", ANS);} return 0 ;}