Title Link: Hdu 5147 Sequence II
preprocessing each location as B and C can be composed of logarithms, and then enumeration B's position is computed.
#include <cstdio> #include <cstring> #include <algorithm>using namespace Std;typedef long ll; const int MAXN = 50005;int N, ARR[MAXN], FENW[MAXN], LEF[MAXN], RIG[MAXN]; #define LOWBIT (x) ((x) & (-X)) void Add (int x, int v) {while (x <= N) {fenw[x] + = v;x + lowbit (x);}} int sum (int x) {int ret = 0;while (x) {ret + = fenw[x];x-= lowbit (x);} return ret;} int main () {int cas;scanf ("%d", &cas), while (cas--) {scanf ("%d", &n), memset (fenw, 0, sizeof (FENW)), and for (int i = 1 ; I <= N; i++) scanf ("%d", &arr[i]); ll ans = 0, tmp = 0;for (int i = 1; I <= N; i++) {lef[i] = SUM (Arr[i]); Rig[i] = n-i-AR R[i] + lef[i] + 1;add (Arr[i], 1); TMP + = Rig[i];} for (int i = 1; I <= N; i++) {tmp-= Rig[i];ans + tmp * Lef[i];} printf ("%i64d\n", ans);} return 0;}
HDU 5147 Sequence II (tree-like array)