|
1 | | -#include <iostream> |
2 | | -#include <vector> |
3 | | -#include <algorithm> |
4 | | - |
5 | | -#define all(v) (v).begin(), (v).end() |
6 | | -using namespace std; |
7 | | - |
8 | | -const int MAX_N = 5005; |
9 | | -int prefix_smaller[MAX_N][MAX_N], suffix_smaller[MAX_N][MAX_N]; |
10 | | - |
11 | | -void solve() |
12 | | -{ |
13 | | - int no_of_elements; |
14 | | - cin >> no_of_elements; |
15 | | - |
16 | | - vector <int> A(no_of_elements + 1); |
17 | | - vector <int> index(no_of_elements + 1); |
18 | | - for(int i = 1; i <= no_of_elements; i++) |
19 | | - { |
20 | | - cin >> A[i]; |
21 | | - |
22 | | - index[A[i]] = i; |
23 | | - } |
24 | | - |
25 | | - for(int b = 1; b <= no_of_elements; b++) |
26 | | - { |
27 | | - for(int c = b + 1; c <= no_of_elements; c++) |
28 | | - { |
29 | | - prefix_smaller[b][c] = suffix_smaller[b][c] = 0; |
30 | | - } |
31 | | - } |
32 | | - |
33 | | - vector <int> prefix_insertions(no_of_elements + 1, 0); |
34 | | - for(int element = 1; element <= no_of_elements; element++) |
35 | | - { |
36 | | - for(int i = index[element]; i <= no_of_elements; i++) |
37 | | - { |
38 | | - prefix_insertions[i]++; |
39 | | - } |
40 | | - |
41 | | - for(int c = index[element], b = 1; b < c; b++) |
42 | | - { |
43 | | - prefix_smaller[b][c] = prefix_insertions[b - 1]; |
44 | | - } |
45 | | - |
46 | | - for(int b = index[element], c = b + 1; c <= no_of_elements; c++) |
47 | | - { |
48 | | - suffix_smaller[b][c] = prefix_insertions[no_of_elements] - prefix_insertions[c]; |
49 | | - } |
50 | | - } |
51 | | - |
52 | | - long long answer = 0; |
53 | | - for(int b = 2; b + 2 <= no_of_elements; b++) |
54 | | - { |
55 | | - for(int c = b + 1; c + 1 <= no_of_elements; c++) |
56 | | - { |
57 | | - long long no_of_a = prefix_smaller[b][c], no_of_d = suffix_smaller[b][c]; |
58 | | - answer += no_of_a*no_of_d; |
59 | | - } |
60 | | - } |
61 | | - |
62 | | - cout << answer << "\n"; |
63 | | -} |
64 | | - |
65 | | -int main() |
66 | | -{ |
67 | | - ios_base::sync_with_stdio(false); |
68 | | - cin.tie(NULL); |
69 | | - |
70 | | - int no_of_test_cases; |
71 | | - cin >> no_of_test_cases; |
72 | | - |
73 | | - while(no_of_test_cases--) |
74 | | - solve(); |
75 | | - |
76 | | - return 0; |
77 | | -} |
78 | | - |
79 | | - |
80 | | - |
| 1 | +#include <iostream> |
| 2 | +#include <vector> |
| 3 | +#include <algorithm> |
| 4 | + |
| 5 | +#define all(v) (v).begin(), (v).end() |
| 6 | +using namespace std; |
| 7 | + |
| 8 | +const int MAX_N = 5005; |
| 9 | +int prefix_smaller[MAX_N][MAX_N], suffix_smaller[MAX_N][MAX_N]; |
| 10 | + |
| 11 | +void solve() |
| 12 | +{ |
| 13 | + int no_of_elements; |
| 14 | + cin >> no_of_elements; |
| 15 | + |
| 16 | + vector <int> A(no_of_elements + 1); |
| 17 | + vector <int> index(no_of_elements + 1); |
| 18 | + for(int i = 1; i <= no_of_elements; i++) |
| 19 | + { |
| 20 | + cin >> A[i]; |
| 21 | + |
| 22 | + index[A[i]] = i; |
| 23 | + } |
| 24 | + |
| 25 | + for(int b = 1; b <= no_of_elements; b++) |
| 26 | + { |
| 27 | + for(int c = b + 1; c <= no_of_elements; c++) |
| 28 | + { |
| 29 | + prefix_smaller[b][c] = suffix_smaller[b][c] = 0; |
| 30 | + } |
| 31 | + } |
| 32 | + |
| 33 | + vector <int> prefix_insertions(no_of_elements + 1, 0); |
| 34 | + for(int element = 1; element <= no_of_elements; element++) |
| 35 | + { |
| 36 | + for(int i = index[element]; i <= no_of_elements; i++) |
| 37 | + { |
| 38 | + prefix_insertions[i]++; |
| 39 | + } |
| 40 | + |
| 41 | + for(int c = index[element], b = 1; b < c; b++) |
| 42 | + { |
| 43 | + prefix_smaller[b][c] = prefix_insertions[b - 1]; |
| 44 | + } |
| 45 | + |
| 46 | + for(int b = index[element], c = b + 1; c <= no_of_elements; c++) |
| 47 | + { |
| 48 | + suffix_smaller[b][c] = prefix_insertions[no_of_elements] - prefix_insertions[c]; |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + long long answer = 0; |
| 53 | + for(int b = 2; b + 2 <= no_of_elements; b++) |
| 54 | + { |
| 55 | + for(int c = b + 1; c + 1 <= no_of_elements; c++) |
| 56 | + { |
| 57 | + long long no_of_a = prefix_smaller[b][c], no_of_d = suffix_smaller[b][c]; |
| 58 | + answer += no_of_a*no_of_d; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + cout << answer << "\n"; |
| 63 | +} |
| 64 | + |
| 65 | +int main() |
| 66 | +{ |
| 67 | + ios_base::sync_with_stdio(false); |
| 68 | + cin.tie(NULL); |
| 69 | + |
| 70 | + int no_of_test_cases; |
| 71 | + cin >> no_of_test_cases; |
| 72 | + |
| 73 | + while(no_of_test_cases--) |
| 74 | + solve(); |
| 75 | + |
| 76 | + return 0; |
| 77 | +} |
| 78 | + |
| 79 | + |
| 80 | + |
0 commit comments