Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=5147
Test instructions
Statistics of how many four-tuple, meet A[i]<a[j]<a[k]<a[q] I<j<k<p
Analysis:
To count the number of four tuples we can enumerate through C and then the statistic interval [1,c-1] How many binary groups (a, b) meet a
<
B and
a< a b
, as well as a statistical interval
[C+1,n] how much D satisfies
A c < a
, the two items can be counted in the answer according to the multiplication principle.
We then deal with the sub-problem: How many binary groups (A, b) are in the interval [1,c-1]. Then we can enumerate B and then how many a in the statistical interval [1,b-1] satisfies
aa < A b
, then this can be done by asking for a prefix in a tree-like array.
Time complexity is O (NLOGN). See Code for details
The code is as follows:
#include <iostream> #include <cstring> #include <cstdio> #include <algorithm>using namespace Std;typedef Long Long ll;const int maxn = 50010;int t[maxn],n; LL A[MAXN]; LL num1[maxn],num2[maxn];int lowbit (int x) {return x& (-X);} void Update (int i,int val) {while (i<=n) {t[i]+=val; I+=lowbit (i); }}ll query (int i) {LL sum=0; while (i>0) {sum+=t[i]; I-=lowbit (i); } return sum;} int main () {int TT; scanf ("%d", &TT); while (tt--) {scanf ("%d", &n); memset (t,0,sizeof (t)); for (int i=1;i<=n;i++) {scanf ("%d", &a[i]); Update (a[i],1); Num1[i]=query (A[i]) -1;//num1[] The number of all smaller than a[i] before the number of "I"} memset (T,0,sizeof (t)); for (int i=n;i>0;i--) {update (a[i],1); Num2[i]=n-i+1-query (A[i]);//num2[] The number of large numbers after the number of first I (A[i])} LL ans = 0,tmp=0; for (int i=1;i<=n;i++) {//i Enumeration C ans+=tmp*num2[i];//number of i-1 before smallThe number of C-1 numbers a<b two of the number of *num2[] The number of all the number of numbers greater than C number tmp=tmp+num1[i];//the number of all a<b two tuples} printf (" %i64d\n ", ans); } return 0;}
HDU5147 Sequence II (tree-like array + prefix and + suffix and)