Topic links
Sequence II
Time limit:5000/2500 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 331 Accepted Submission (s): 151
Problem Descriptionlong long ago, there is a sequence a with length n. All numbers-sequence is no smaller than 1 and no bigger than N, and all numbers are different in this sequence.
Calculate how many quad (A,B,C,D) satisfy:
1.1≤a<b<c<d≤n
2.a<ab
3.aC<ad
Inputthe first line contains a single integer T, indicating the number of test cases.
Each test case is begins with a line contains an integer n.
The next line follows N integers a1,< Span id= "mathjax-span-48" class= "Msubsup" >a2, ... , An .
[Technical specification]
1 <= T <= 100
1 <= N <= 50000
1 <= Ai <= n
Outputfor each case output one line contains a integer,the number of quad.
Sample Input151 3 2 4 5
Sample OUTPUT4 Test Instructions:
Once upon a time, there was a sequence of n of length A, each number in the sequence is not less than 1 and not greater than N, and there are no more than two identical numbers in the sequence. Please count how many four tuples (a,b,c,d) meet: 1.1≤a<b<c<d≤n 2.a<ab 3.aC<ad
Analysis:
my approach is to divide the four-tuple into two-tuple to deal with, the decomposition of the method is to enumerate the array into two parts, and then to each part of the tree-like array to find the reverse number, the result is multiplied by the number of the four-tuple that satisfies the condition.
the way to find the inverse number of a tree array is to know that the number in the sequence is 1-n, so you can use the element of the array as the number c[], the value is subscript, and the sum is greater than the current number or less than the current number
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <cstdlib>5#include <cmath>6#include <vector>7#include <algorithm>8 #defineLL __int649 Const intMAXN = 1e5 +Ten;Ten using namespacestd; One LL A[MAXN], C[MAXN], N, F[MAXN]; A - intLowbit (intx) - { the returnx& (-x); - } - voidAddintXintd) - { + while(x <=N) - { +C[X] + =D; AX + =lowbit (x); at } - } -LL sum (intx) - { -LL ret =0; - while(X >0) in { -RET + =C[x]; toX-=lowbit (x); + } - returnret; the } * $ intMain ()Panax Notoginseng { - intT; the LL i, ans, tmp; +scanf"%d", &t); A while(t--) the { +Ans =0; -scanf"%i64d", &n); $ $Memset (F,0,sizeof(f)); -Memset (c,0,sizeof(c)); - for(i =1; I <= N; i++) the { -scanf"%i64d", &a[i]);WuyiAdd (A[i],1); theF[i] = f[i-1] + sum (a[i]-1); - } Wu -Memset (c,0,sizeof(c)); AboutTMP =0; $ for(i = n; I >=1; i--) - { -TMP = SUM (n)-sum (a[i]); -Add (A[i],1); AAns + = tmp*f[i-1]; + } theprintf"%i64d\n", ans); - } $ return 0; the}
Hdu 5147 Sequence II (tree array for reverse order number)