Topic Links: Hoj 2275
Test Instructions Analysis: give you a size of 50000 array A, requires in 1 seconds to meet the {i < J < K, Ai < AJ > ak} number of combinations. (0 <= ai <= 32768)
problem-solving ideas: Although the array is very large, but the number in the array is very small, so the bucket row, and then the tree array summed on the line:
First of all, we have learned in Hdu 1541 how to ask for a small number of AI in front of the AI, then the problem is to ask the AI in front of the number of small than AI plus the number of AI smaller than AI, and finally summing up is the answer ~
Counting from the beginning, you can get the number of the AI on the left side of him, then counting from the tail, you can get the AI to the right of the number of small, then for the AI, the two are the answer:
The code is as follows:
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>#define MAXN 33000using namespace STD;intn,bit[33005],l[50005],a[50005];intLowbit (intNUM) {returnnum& (-num);}intSumintIndex) {intans=0; for(intI=index;i>0; I-=lowbit (i)) ans+=bit[i];returnAns;}voidAddintIndexintDelta) { for(intI=index;i<=maxn;i+=lowbit (i)) Bit[i]+=delta;}intMain () { while(~scanf("%d", &n)) {memsetL0,sizeof(l));memset(Bit,0,sizeof(bit));Long Longans=0; for(intI=1; i<=n;i++) {scanf("%d", &a[i]); a[i]++; L[i]=sum (a[i]-1); Add (A[i],1); }memset(Bit,0,sizeof(bit)); for(inti=n;i>=1; i--) {Ans+=sum (a[i]-1) *l[i]; Add (A[i],1); }printf("%lld\n", ans); }return 0;}
Note that counting starts from 1 and data range!
Summary:
1, tree-like array is really a magical data structure Ah!
2, the detail part also needs attention!
Tree-like Array (ii)