|
| 1 | +Whenever we have to count tuples or triplets, the main idea is to fix the middle and then find each half. |
| 2 | + |
| 3 | +In this, let us try to fix the pair [b, c] |
| 4 | + |
| 5 | +How do we count the number of tuples (a, b, c, d) when [b, c] is fixed ? |
| 6 | + |
| 7 | +----- |
| 8 | + |
| 9 | +To do this, let us see the candidates for a and d ? |
| 10 | + |
| 11 | +All elements in [1, b - 1] which are smaller than A[c] can be 'a'. |
| 12 | + |
| 13 | +All elements in [c + a, d] which are smaller than A[b] can be 'd'. |
| 14 | + |
| 15 | +The number of tuples here is |
| 16 | + |
| 17 | +a_candidates x d_candidates. |
| 18 | + |
| 19 | +----- |
| 20 | + |
| 21 | +Now, how do we calculate the number of elements in [1, b - 1] smaller than A[c] for every pair (b, c) ? |
| 22 | + |
| 23 | +Let us imagine that we have an empty array - B |
| 24 | + |
| 25 | +We will insert the elements of A in B in their index, one by one - in ascending order. |
| 26 | + |
| 27 | +For example, if A = {5, 3, 6, 1, 4, 2} |
| 28 | + |
| 29 | +B = {0, 0, 0, 0, 0, 0} |
| 30 | +{0, 0, 0, 1, 0, 0} |
| 31 | +{0, 0, 0, 1, 0, 2} |
| 32 | +{0, 3, 0, 1, 0, 2} |
| 33 | +{0, 3, 0, 1, 4, 2} |
| 34 | +{5, 3, 0, 1, 4, 2} |
| 35 | +{5, 3, 6, 1, 4, 2} |
| 36 | + |
| 37 | +We will insert the elements one by one and use it to precalculate |
| 38 | + |
| 39 | +Prefix_smaller(b, c) and Suffix_smaller(b, c) for every pair (b, c) in the array. |
| 40 | + |
| 41 | +---- |
| 42 | + |
| 43 | +Suppose we insert A[i] at position i |
| 44 | + |
| 45 | +We will do the following - |
| 46 | + |
| 47 | +1. First, let c = i |
| 48 | + |
| 49 | +Then iterate over all b in [1, i - 1] and count the number of inserted elements. |
| 50 | + |
| 51 | +2. Then, let b = i |
| 52 | + |
| 53 | +Then iterate over all c in [i + 1, n] and count the number of inserted elements. |
| 54 | + |
| 55 | +----- |
| 56 | + |
| 57 | +void solve() |
| 58 | +{ |
| 59 | + int no_of_elements; |
| 60 | + cin >> no_of_elements; |
| 61 | + |
| 62 | + vector <int> A(no_of_elements + 1); |
| 63 | + vector <int> index(no_of_elements + 1); |
| 64 | + for(int i = 1; i <= no_of_elements; i++) |
| 65 | + { |
| 66 | + cin >> A[i]; |
| 67 | + |
| 68 | + index[A[i]] = i; |
| 69 | + } |
| 70 | + |
| 71 | + for(int b = 1; b <= no_of_elements; b++) |
| 72 | + { |
| 73 | + for(int c = b + 1; c <= no_of_elements; c++) |
| 74 | + { |
| 75 | + prefix_smaller[b][c] = suffix_smaller[b][c] = 0; |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + vector <int> prefix_insertions(no_of_elements + 1, 0); |
| 80 | + for(int element = 1; element <= no_of_elements; element++) |
| 81 | + { |
| 82 | + for(int i = index[element]; i <= no_of_elements; i++) |
| 83 | + { |
| 84 | + prefix_insertions[i]++; |
| 85 | + } |
| 86 | + |
| 87 | + for(int c = index[element], b = 1; b < c; b++) |
| 88 | + { |
| 89 | + prefix_smaller[b][c] = prefix_insertions[b - 1]; |
| 90 | + } |
| 91 | + |
| 92 | + for(int b = index[element], c = b + 1; c <= no_of_elements; c++) |
| 93 | + { |
| 94 | + suffix_smaller[b][c] = prefix_insertions[no_of_elements] - prefix_insertions[c]; |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + long long answer = 0; |
| 99 | + for(int b = 2; b + 2 <= no_of_elements; b++) |
| 100 | + { |
| 101 | + for(int c = b + 1; c + 1 <= no_of_elements; c++) |
| 102 | + { |
| 103 | + long long no_of_a = prefix_smaller[b][c], no_of_d = suffix_smaller[b][c]; |
| 104 | + answer += no_of_a*no_of_d; |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + cout << answer << "\n"; |
| 109 | +} |
0 commit comments