Triple Inversions

Given a list of N integers A1​​, A2​​, A3​​,...AN​​, there's a famous problem to count the number of inversions in it. An inversion is defined as a piar of indices i<j such that Ai​​>Aj​​.

Now we have a new challenging problem. You are supposed to count the number of triple inversions in it. As you may guess, a triple inversion is defined as a triple of indices i<j<k such that Ai​​>Aj​​>Ak​​. For example, in the list { 5, 1, 4, 3, 2 } there are 4 triple inversions, namely (5,4,3), (5,4,2), (5,3,2) and (4,3,2). To simplify the problem, the list A is given as a permutation of integers from 1 to N.

Input Specification:

Each input file contains one test case. For each case, the first line gives a positive integer N in [3]. The second line contains a permutation of integers from 1 to N and each of the integer is separated by a single space.

Output Specification:

For each case, print in a line the number of triple inversions in the list.

Sample Input:

22
1 2 3 4 5 16 6 7 8 9 10 19 11 12 14 15 17 18 21 22 20 13
 

Sample Output:

8
 1 #include
 2 #include
 3 using namespace std;
 4 using namespace __gnu_pbds;
 5 int main()
 6 {
 7 //    freopen("data.txt","r",stdin);
 8     tree<long long,null_type,less<long long>,rb_tree_tag,tree_order_statistics_node_update> tr;
 9     long long n,x,s=0;
10     long long r,b;
11     scanf("%lld",&n);
12     for(long long i=1;i<=n;i++)
13     {
14         scanf("%lld",&x);
15         tr.insert(x);
16         r=tr.size()-tr.order_of_key(x)-1;
17         b=r-i+x;
18         s=s+r*b;
19     }
20     printf("%lld",s);
21     return 0;
22 }

 

你可能感兴趣的:(Triple Inversions)