The main idea is given a string of 1 to n arrangement (set to an array of a), which satisfies a[x]<a[z]<a[y], where the number of permutations, where x<y<z
The number of such permutations is not so good that we can convert to the number of a[x]<a[z]<a[y]+a[x]<a[y]<a[z] minus a[x]<a[z]<a[y].
Using the left array to record the number of elements that are smaller than a[i] before the position of I, then we can get the formula for the number of permutations (see Code), which can be preprocessed by a tree array.
#include <cstdio> #include <cstring> #include <cmath> #include <cstdlib> #include <iostream > #include <algorithm> #include <vector> #include <map> #include <queue> #include <stack& Gt #include <string> #include <map> #include <set>using namespace std; #define LL Long Long const LL MAXN = 100000 + 5; LL c[2 * MAXN], LEF[MAXN], N; The lef array represents the number of smaller numbers before I than a[i] LL lowbit (ll x) {return x& (x);} void Add (ll x, ll D) {while (x <= N) {c[x] + = D; x + = Lowbit (x);}} ll sum (ll x) {ll ret = 0;while (x > 0) {ret + = c[x]; x-= Lowbit (x);} return ret;} int main () {LL T, Kase = 0; scanf ("%i64d", &t), while (t--) {memset (c, 0, sizeof (c)); scanf ("%i64d", &n); ll ans = 0;for (ll i = 1; I <= n; i++) {LL tmp;scanf ("%i64d", &tmp), lef[i] = SUM (TMP), add (TMP, 1),//PRLLF ("%i64d\n", Lef[i]); ans = ans + (n + lef[i] + 1-tmp-i) * (n + lef[i]-tmp-i)/2-(n + lef[i] + 1-tmp-i) * Lef[i];} printf ("Case #%i64d:%i64d\n", ++kase, ans% 100000007);} return 0;}
HDU 4000 Fruit Ninja (tree-like array)