Tokitsukaze and Strange Inequality solution codeforces – Tokitsukaze has a permutation 𝑝p of length 𝑛n. Recall that a permutation 𝑝p of length 𝑛n is a sequence 𝑝1,𝑝2,…,𝑝𝑛p1,p2,…,pn consisting of 𝑛n distinct integers, each of which from 11 to 𝑛n (1≤𝑝𝑖≤𝑛1≤pi≤n).
[Solution] Tokitsukaze and Strange Inequality solution codeforces
She wants to know how many different indices tuples [𝑎,𝑏,𝑐,𝑑][a,b,c,d] (1≤𝑎<𝑏<𝑐<𝑑≤𝑛1≤a<b<c<d≤n) in this permutation satisfy the following two inequalities:
The first line contains one integer 𝑡t (1≤𝑡≤10001≤t≤1000) — the number of test cases. Each test case consists of two lines.
The first line contains a single integer 𝑛n (4≤𝑛≤50004≤n≤5000) — the length of permutation 𝑝p.
The second line contains 𝑛n integers 𝑝1,𝑝2,…,𝑝𝑛p1,p2,…,pn (1≤𝑝𝑖≤𝑛1≤pi≤n) — the permutation 𝑝p.
It is guaranteed that the sum of 𝑛n over all test cases does not exceed 50005000.
[Solution] Tokitsukaze and Strange Inequality solution codeforces
For each test case, print a single integer — the number of different [𝑎,𝑏,𝑐,𝑑][a,b,c,d] tuples.
3 6 5 3 6 1 4 2 4 1 2 3 4 10 5 1 6 2 8 3 4 10 9 7
[Solution] Tokitsukaze and Strange Inequality solution codeforces
3 0 28
In the first test case, there are 33 different [𝑎,𝑏,𝑐,𝑑][a,b,c,d] tuples.
𝑝1=5p1=5, 𝑝2=3p2=3, 𝑝3=6p3=6, 𝑝4=1p4=1, where 𝑝1<𝑝3p1<p3 and 𝑝2>𝑝4p2>p4 satisfies the inequality, so one of [𝑎,𝑏,𝑐,𝑑][a,b,c,d] tuples is [1,2,3,4][1,2,3,4].
Similarly, other two tuples are [1,2,3,6][1,2,3,6], [2,3,5,6][2,3,5,6].