Test instructions
N (5*10^5) a different number consisting of sequence a finds number groups that meet the following constraints: I1<i2<i3<i4 && ai1<ai2 && ai3<ai4
Ideas:
The obvious observation is that Nlogn's algorithm we found in fact ai1 and AI2 can be put together to consider the same ai3 and ai4 the two groups do not affect each other
Let's see how the answer is made up of the hypothetical enumeration of i3 's position then we'd like to know that "i3 is a few numbers greater than ai3" This can be handled by a tree-like array
The same assumption we know I2 also want to know "i2 in front of a few numbers less than AI2" This can also be a tree-like array
Then using I3>I2, the answer can be expressed as SUM (num (>ai3) * SUM (num (<AI2)))
Code:
#include <cstdio> #include <iostream> #include <cstring> #include <string> #include < algorithm> #include <map> #include <set> #include <vector> #include <queue> #include < cstdlib> #include <ctime> #include <cmath>using namespace std;typedef long long LL; #define N 50000#define Lowbit (x) (x&-X) inline void Scand (int &ret) {char C; ret = 0; while ((c = GetChar ()) < ' 0 ' | | c > ' 9 '); while (c >= ' 0 ' && C <= ' 9 ') ret = ret * + (C-' 0 '), C = GetChar ();} int T, N;int C[n +], D[n +], G[n + 10]; LL ans;void Add (int f[], int x, int key) {while (x <= N) {f[x] + = key; x + = Lowbit (x); }}int sum (int f[], int x) {int res = 0; while (x) {res + = f[x]; X-= Lowbit (x); } return res; struct Node {int val, id;} nd[n + 10];bool cmp1 (node FA, node fb) {return fa.val < Fb.val;} BOOL CMP2 (node FA, node fb) {return fa.id < fb.id;} INT Main () {Scand (t); while (t--) {Scand (n); for (int i = 1; I <= n; i++) {Scand (nd[i].val); Nd[i].id = i; } sort (nd + 1, nd + n + 1, CMP1); memset (c, 0, sizeof (c)); memset (d, 0, sizeof (d)); for (int i = 1; I <= n; i++) {int tmp = SUM (c, nd[i].id); Add (C, nd[i].id, 1); Add (d, nd[i].id, TMP); G[nd[i].id] = SUM (c, nd[i].id-1); } sort (nd + 1, nd + n + 1, CMP2); Ans = 0; for (int i = 3; I <= n; i++) {int tmp = ND[I].VAL-1; TMP = N-i-(tmp-g[i]); Ans + = (LL) (TMP) * SUM (d, i-1); } printf ("%i64d\n", ans); } return 0;}
HDU 5147 Sequence II