Title Link: https://vjudge.net/problem/UVALive-4329
Test instructions: N number, find three number Ai,aj,ak, make i<j<k and Ai<aj<ak or Ai>aj>ak. Ask how many combination methods.
Enumerate AJ, which is the number of smaller than AJ in CJ [1,j], the number of DJs (j,n) smaller than AJ.
So the number of J-1-CJ is [1,j] larger than AJ, N-J-DJ is the number of (j,n) larger than AJ.
The answer is ∑dj* (J-1-CJ) + (N-J-DJ) *CJ
You can get C and D separately with bit in O (NLGN).
1#include <bits/stdc++.h>2 using namespacestd;3 4typedefLong LongLL;5 Const intMAXN =20020;6 Const intMAXM =100100;7 intN, M;8 intA[MAXN];9 LL BIT[MAXM];Ten LL C[MAXM], D[MAXM]; One AInlineintLowbit (intx) { - returnX & (-x); - } the - voidAddinti) { - while(I <=m) { -bit[i]++; +i + =lowbit (i); - } + } A atLL sum (inti) { -LL ret =0; - while(i) { -RET + =Bit[i]; -I-=lowbit (i); - } in returnret; - } to + - intMain () { the //freopen ("in", "R", stdin); * intT; $scanf"%d", &T);Panax Notoginseng while(t--) { -scanf"%d", &n); them =-1; + for(inti =1; I <= N; i++) { Ascanf"%d", &a[i]); them =Max (M, A[i]); + } -LL ret =0; $memset (bit,0,sizeof(bit)); $ for(inti =1; I <= N; i++) { - Add (A[i]); -C[i] = SUM (a[i]-1); the } -memset (bit,0,sizeof(bit));Wuyi for(inti = n; I >=1; i--) { the Add (A[i]); -D[i] = SUM (a[i]-1); Wu } - for(inti =1; I <= N; i++) { AboutRET + = ((I-1)-c[i]) * D[i] + (N-i-d[i]) *C[i]; $ } -cout << ret <<Endl; - } - return 0; A}
[UVALive4329] Ping Pong (tree-like array, combination)