We need to compute number of triplets (i<j<k) such that a_i > a_j > a_k. That is number of decreasing subsequences of length 3. Given distinct integers. n up to 1e6.
We need efficient O(n log n) solution.
Let left[i] = number of indices j < i such that a_j > a_i. That is count of greater elements on left.
Similarly right[i] = number of indices k > i such that a_i > a_k? Actually for triplet with middle at j, we need i<j<k, a_i > a_j > a_k. So for each j as middle, the number of triplets with that j is left_greater[j] * right_smaller[j]? Let's verify: left_greater[j] = count of i a_j. right_smaller[j] = count of k>j with a_j > a_k. Then each pair (i,k) with i from left_greater and k from right_smaller gives a valid triplet? But careful: We need i<j<k, a_i > a_j > a_k. For a given j, any i from left_greater (a_i > a_j) and any k from right_smaller (a_j > a_k) automatically satisfy i<j<k because ij, but there's no guarantee about a_i > a_k? Actually we don't need a_i > a_k directly; the condition is a_i > a_j > a_k, which only requires a_i > a_j and a_j > a_k. It does not require a_i > a_k? But if a_i > a_j and a_j > a_k, then a_i > a_k by transitivity (since numbers are distinct, but they are numbers, so yes if a_i > a_j and a_j > a_k then a_i > a_k). So it's automatically satisfied. So triplets = sum over j of left_greater[j] * right_smaller[j].
We need to compute left_greater and right_smaller efficiently.
Since values are up to 1e9 distinct, we can compress coordinates. Then use Fenwick tree (BIT) to count numbers.
For left_greater: iterate from left to right, maintain BIT of frequencies of values seen so far. For current a_j, left_greater[j] = total seen - prefix_sum(rank of a_j) which counts number of seen <= a_j. Since all seen are distinct and we want > a_j: so left_greater = i - query(rank) where i is current index (1-indexed) count of previous elements = i-1? Actually when processing j, we have processed j-1 elements. So left_greater[j] = (j-1) - query(rank of a_j) (since query gives count of numbers <= a_j among previous). Because numbers are distinct, we could also query for rank-1? But easier: total_previous - count of <= a_j. That works.
Similarly for right_smaller: iterate from right to left, maintain BIT of frequencies of values seen from right. For current a_j, right_smaller[j] = query(rank_of_a_j - 1) gives count of numbers to the right that are smaller than a_j. Alternatively we can compute count of numbers to the right that are less than a_j: use BIT that holds frequencies of values seen from right. Then right_smaller = query(rank-1) because those are values < a_j.
But careful: Since values are distinct, query(rank-1) works. Or we could also compute total_right - count of >=? But we'll use query(rank-1). BIT query returns sum up to index inclusive; so to get < a_j, query(rank-1).
So we need two passes. n up to 1e6, BIT with size up to n (compressed) works fine.
Memory: We need arrays left_greater and right_smaller of size n (int 64 maybe). But left_greater and right_smaller can be up to O(n^2) in worst case (e.g., decreasing order, left_greater[j] = j-1, right_smaller[j] = n-j). But we only need product, which can be as high as about (n^3) ~ 1e18, so we need 64-bit integer for sum. So use long long.
We can compute sum on the fly: as we compute right_smaller during the second pass, we already have left_greater computed from first pass and stored. Then ans += left_greater[j] * right_smaller[j]. That requires storing left_greater (size n) of type long long? Actually left_greater can be up to 1e6, fits in int, but product may need up to ~(5e11)? Actually worst-case left_greater for middle index is (j-1) and right_smaller is (n-j). The maximum product occurs around j=n/2: left ~ n/2, right ~ n/2, product ~ n^2/4 ~ 2.5e11 for n=1e6, which fits in 64-bit. But sum over all j might be O(n^3) worst-case? Actually number of triplets in decreasing order is C(n,3) = n(n-1)(n-2)/6 ~ 1.67e17 for n=1e6? Let's compute: 1e6^3/6 = 1e18/6 ≈ 1.67e17, which is beyond 64-bit? 2^63-1 is about 9.22e18, so 1.67e17 is well within 64-bit. So 64-bit (unsigned long long) is fine. So we need ans as long long.
Thus algorithm: compress values to ranks 1..n (since distinct). Then first pass: BIT of size n, initially zeros. For i from 0 to n-1 (0-index), compute rank r = compressed[a[i]]. left_greater[i] = i - query(r) (since i previous count). Then update BIT at r with +1. That's O(n log n).
Second pass: BIT cleared, then iterate i from n-1 down to 0. For each i, rank r. right_smaller = query(r-1) (if r>1 else 0). Then ans += left_greater[i] * right_smaller. Then update BIT at r with +1. Output ans.
Check examples:
Example1: 3 2 1 -> ranks: 3->3,2->2,1->1? Actually compress order ascending: [1,2,3] -> ranks: 3:3,2:2,1:1. Pass1: i=0: left_greater[0]=0-0=0; update(3). i=1: r=2, query(2)=? only 3 present -> query(2) returns count up to 2 =0? Actually BIT stores at rank3, so query(2)=0. So left_greater[1]=1-0=1; update(2). i=2: r=1, query(1)=? we have 3 and 2? query(1) counts ranks <=1 => maybe 0? Actually value 1 rank 1, but we have 2 and 3, so query(1)=0. left_greater[2]=2-0=2. So left: [0,1,2]. Pass2: BIT empty. i=2: r=1, query(0)=0, right_smaller=0; ans+=20=0; update(1). i=1: r=2, query(1)= count of ranks <=1 => we have 1 at rank1, so query(1)=1. right_smaller=1; ans+=11=1; update(2). i=0: r=3, query(2)= count of ranks <=2 => we have rank1 and 2? Actually after previous updates: we have updated rank1 and rank2, so query(2)=2. right_smaller=2; ans+=0*2=0. total=1. Correct.
Example2: 2 3 1 -> compress: values sorted [1,2,3] -> ranks: 2->2, 3->3, 1->1. Pass1: i=0 left_greater[0]=0; update(2). i=1 r=3, query(3)=? have 2 => query(3)=1 (since rank2 <=3). left_greater[1]=1-1=0; update(3). i=2 r=1, query(1)=0 (since ranks 2 and 3 >1). left_greater[2]=2-0=2. So left: [0,0,2]. Pass2: i=2 r=1, query(0)=0 -> right=0 ans+=20=0; update(1). i=1 r=3, query(2)= count <=2? we have rank1 only, query(2)=1 (since rank1 <=2). right_smaller=1? But we need numbers smaller than 3 to the right: rank1 is smaller, rank? also? only rank1, so right_smaller=1; ans+=01=0; update(3). i=0 r=2, query(1)= count <=1? we have rank1, so query(1)=1, right_smaller=1; ans+=0*1=0. total=0. correct.
Example3: 10 8 3 1 -> compress: sorted [1,3,8,10] -> ranks:10->4,8->3,3->2,1->1. Pass1: i=0 left=0; update(4). i=1 r=3, query(3)=? have 4>3, so query(3)=0? Actually query(3) sums ranks <=3, only if we have something at 3? no, we have at 4 only. So left[1]=1-0=1; update(3). i=2 r=2, query(2)=? have ranks 4 and 3, both >2, so 0; left[2]=2-0=2; update(2). i=3 r=1, query(1)=0; left[3]=3-0=3. So left: [0,1,2,3]. Pass2: i=3 r=1, query(0)=0 right=0 ans+=30=0; update(1). i=2 r=2, query(1)= count <=1: we have rank1, so 1; right=1; ans+=21=2; update(2). i=1 r=3, query(2)= count <=2: ranks 1 and 2, so 2; right=2; ans+=12=2; total=4; i=0 r=4, query(3)= count <=3: ranks 1,2,3? we have rank1,2,3? Actually after updates: we have updated rank1,2,3? At i=0 we have updated? Let's see: after i=1? Wait we start from i=3 (index3): update rank1. i=2: update rank2. i=1: update rank3. So before i=0, BIT has ranks1,2,3. query(3) = 3. right=3; ans+=03=0. total=4. Correct.
Example4: 1 5 4 3 -> compress: sorted [1,3,4,5] -> ranks:1->1,5->4,4->3,3->2. Pass1: i=0 left=0; update(1). i=1 r=4, query(4)=? have 1 at rank1, so query(4)=1 (since 1 <=4). left[1]=1-1=0; update(4). i=2 r=3, query(3)=? have ranks 1 and 4, so query(3) = count up to 3 includes rank1 only => 1. left[2]=2-1=1; update(3). i=3 r=2, query(2)=? have ranks1,4,3; query(2) includes rank1 =>1. left[3]=3-1=2. So left: [0,0,1,2]. Pass2: i=3 r=2, query(1)= count <2 (ranks<=1): we have none? starting from right, BIT empty. So right=0; ans+=20=0; update(2). i=2 r=3, query(2)= count <=2: we have rank2? Actually after previous update: we have updated rank2, so query(2)=1 (since rank2 <=2). right=1; ans+=11=1; update(3). i=1 r=4, query(3)= count <=3: we have ranks2,3 =>2; right=2; ans+=02=0; update(4). i=0 r=1, query(0)=0; ans+=00=0; total=1. correct.
So algorithm works.
Complexities: O(n log n) time, O(n) memory.
Potential improvements: Because n up to 1e6, we need to be careful with BIT implementation (use 1-indexed arrays). Use arrays of int for BIT, but frequency counts can be up to n, but int is fine (since n <= 1e6). left_greater can be stored as int (or long long). Use long long for ans and maybe for product.
Edge Cases: All increasing: left_greater all 0; right_smaller some may be non-zero? Actually if increasing, then for each i, numbers to right are larger, so right_smaller=0, so ans=0. All decreasing: left_greater[i] = i; right_smaller[i] = n-i-1? Actually indices 0..n-1: for i (0-index) left_greater = i (since all previous are larger because decreasing). Right_smaller = count of numbers to right smaller than a_i. Since decreasing, all numbers to right are smaller? If array is strictly decreasing, then for any i, all elements to the right are smaller (since a_i > a_{i+1} > ...). So right_smaller = n - i - 1. So product = i * (n-i-1). Sum_{i=0}^{n-1} i*(n-i-1) = number of triplets = C(n,3). Good.
Implementation details:
-
Use fast IO (scanf/printf) because n up to 1e6, reading via cin with sync off or using scanf.
-
BIT operations: add(idx, val) and sum(idx). Since only adding 1, we can simplify.
-
Compression: store values in array a, create vector sorted unique copy, map each value to its index (1-based) using binary search or map. Since n up to 1e6, we can sort copy O(n log n) and then for each a[i], rank = lower_bound(s.begin(), s.end(), a[i]) - s.begin() + 1.
Memory: a array of n ints, left array of n ints (or maybe unsigned int), BIT array of size n+2. That's fine.
But careful: left_greater[i] can be up to i (max 1e6-1) fits into 32-bit. However product left*right may need 64-bit. But storing left as 32-bit is fine.
Potential overflow if we use int for product: ensure we cast to long long.
We could compute ans as:
long long ans = 0;
for (int i=0; i<n; i++) {
ans += (long long) left[i] * right_smaller;
}
right_smaller computed in second pass.
Implementation steps:
-
Read n.
-
Read array a of n ints.
-
Compression: copy a to vector v_sorted, sort, unique (but all distinct so unique not needed but okay). Then for each i, compute rank[i] = lower_bound(v_sorted.begin(), v_sorted.end(), a[i]) - v_sorted.begin() + 1.
Alternatively we can avoid storing rank array and use on the fly? But we need rank for both passes. Could store rank array of size n (int). That's okay.
-
BIT1 (size n). Initialize zero.
For i from 0 to n-1:
r = rank[i];
left[i] = i - query(r); // i = number of elements processed before (since 0-index, i is count of previous? Actually at iteration i (0-index), number of previous elements = i. So left_greater = i - query(r) because query(r) gives number of previous elements with rank <= r (i.e., <= a_i). Since we want >, it's previous count - number <= a_i.
add(r, 1);
-
Clear BIT (maybe reuse same array, reinitialize to zero).
ans = 0;
For i from n-1 down to 0:
r = rank[i];
int right_smaller = (r > 1) ? query(r-1) : 0;
ans += (long long) left[i] * right_smaller;
add(r, 1);
-
Print ans.
Fenwick tree implementation:
int bit[N+1];
void add(int idx, int val) {
while (idx <= N) {
bit[idx] += val;
idx += idx & -idx;
}
}
int query(int idx) {
int sum = 0;
while (idx > 0) {
sum += bit[idx];
idx -= idx & -idx;
}
return sum;
}
We need N = n (since compressed ranks 1..n). So BIT size n+1.
Complexities fine.
Testing with examples.
One more check: n up to 1e6, BIT operations O(log n) ~ 20 steps each, total ~ 40 million operations, which is fine in C++.
Potential pitfalls:
-
Use of int for left may overflow product? But we cast.
-
For left[i] = i - query(r): at i=0, i=0, query(r) = 0, left[0]=0.
-
During second pass, we compute query(r-1) for right_smaller. Need to ensure BIT is zero initially.
-
Memory: storing a, rank, left, bit. a (int) 4MB, rank (int) 4MB, left (int) 4MB, bit (int) 4MB, plus sorted vector (int) 4MB. That's about 20 MB, okay.
Could also avoid storing a and rank separately if we compress in place: store a as ints, compute ranks after sorting. But fine.
Alternative: We can compute left and right in one pass using two BITs? Not needed.
Edge Cases: n=3 minimal.
Check correctness for random small arrays via brute force.
Now, we should also consider using 0-index for left and right. Ensure we treat indices correctly.
Let's code.
But the problem statement says: "Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d)." So we can use cout with ans.
Implementation in C++:
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 5;
int a[MAXN];
int rnk[MAXN];
int left_greater[MAXN]; // number of greater on left
int bit[MAXN];
int n;
void add(int idx, int val) {
while (idx <= n) {
bit[idx] += val;
idx += idx & -idx;
}
}
int query(int idx) {
int res = 0;
while (idx > 0) {
res += bit[idx];
idx -= idx & -idx;
}
return res;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; i++) {
scanf("%d", &a[i]);
}
// coordinate compression
vector v(a, a + n);
sort(v.begin(), v.end());
// unique not necessary as all distinct, but we can keep
for (int i = 0; i < n; i++) {
rnk[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1;
}
// first pass: compute left_greater
memset(bit, 0, sizeof(bit));
for (int i = 0; i < n; i++) {
int r = rnk[i];
// number of previous elements: i
left_greater[i] = i - query(r);
add(r, 1);
}
// second pass: compute right_smaller and answer
memset(bit, 0, sizeof(bit));
ll ans = 0;
for (int i = n-1; i >= 0; i--) {
int r = rnk[i];
int right_smaller = (r > 1) ? query(r-1) : 0;
ans += (ll) left_greater[i] * right_smaller;
add(r, 1);
}
printf("%lld\n", ans);
// or cout << ans << endl;
return 0;
}
Check: bit array size: we set MAXN = 1e6+5, bit size enough.
Potential improvement: using vector for bit dynamic based on n to save memory? But static is fine.
Note: left_greater can be up to i (max 1e6). But int is fine. However, left_greater[i] * right_smaller can be up to 2.5e11, within 64-bit.
Test with example:
Input:
4
1 5 4 3
Should output 1. Let's simulate: compression: v sorted [1,3,4,5] ranks: 1->1,5->4,4->3,3->2. First pass: i=0 r=1 left=0; add1. i=1 r=4 left=1 - query(4)=? query(4) counts <=4; have 1 ->1, so left=0; add4. i=2 r=3 left=2 - query(3)=? counts <=3: have 1, (4 not counted because 4>3) ->1, so left=1; add3. i=3 r=2 left=3 - query(2)=? counts <=2: have 1 ->1, left=2. So left:0,0,1,2. Second pass: bit empty. i=3 r=2 right=query(1)=0 ans+=20=0 add2. i=2 r=3 right=query(2)=1 ans+=11=1 add3. i=1 r=4 right=query(3)=2 ans+=0*2=0 add4. i=0 r=1 right=0 ans+=0. total=1. correct.
So answer is good.
We need to ensure reading/writing 64-bit: we used %lld in printf which is allowed? Problem says: "Please, do not use %lld specificator to read or write 64-bit integers in C++. It is preffered to use cout (also you may use %I64d)." But this note may be about the judge environment? It's typical for Codeforces (since they use mingw, %lld may cause issues, they recommend cout or %I64d). However many modern judges accept %lld. To be safe, we can use cout << ans << '\n'; or use %I64d. I'll use printf("%I64d\n", ans) or cout. Since the problem statement says "preffered to use cout (also you may use %I64d)". I'll use cout.
But careful: with cout, if you include sync_with_stdio(false) you can speed up. However we are already using scanf for input. Mixing may be okay but need to ensure not to cause issues. Usually mixing stdio and iostream is okay if not synchronizing. But to be safe, we can use printf with %I64d. I'll adopt that.
Alternatively we can use printf("%lld\n", (long long)ans) but maybe they have issue. Many Codeforces problems accept %lld now. But let's follow recommendation: use %I64d. Actually they say "do not use %lld ... It is preffered to use cout (also you may use %I64d)." So we can use %I64d. So printf("%I64d\n", ans); But ans is long long, that's fine.
We'll include cstdio and iostream? Not needed.
Thus final solution.
Complexities: O(n log n) time, O(n) memory.
But can we do O(n) using some advanced data structure? Not necessary.
Let's think about possible integer overflow in left_greater calculation: i - query(r). i is up to 1e6, query(r) up to i, so difference fits int. Good.
One nuance: when i is large, left_greater may be large but still int.
Now test with maximum n: 1e6, all decreasing. left_greater values up to ~1e6, right_smaller up to ~1e6, product up to ~1e12. Sum up to C(1e6,3) ~ 1.67e17, within 64-bit.
Potential performance: BIT operations are O(log n). With n=1e6, log2(1e6)~20. 2 passes * 1e6 * 20 = 40 million operations, which is okay in 1-2 seconds in C++.
But we also have sorting O(n log n) for compression, which is ~20 million operations. So total ~60 million, still okay.
Memory: as estimated, 5 arrays of size 1e6 each: a (4MB), rank (4), left (4), bit (4), sorted (4) = 20MB. Plus overhead, fine.
Edge Cases: n=3 min.
Thus final answer.
We can also avoid storing a and rank separately by reusing a as ranks after compression? Actually we could compress in place: store original values, then create sorted vector, then replace a[i] with compressed rank (overwrite a). That way we don't need separate rank array. And left can be stored as int. So memory slightly less. But fine.
Let's produce final code accordingly.
Now output the solution.
But the user is ChatGPT, we need to respond with the solution? The instruction: "Help Shapur find out how weak the Romans are." Possibly they want code. The problem statement is from Codeforces problem "A. The Weakness of the Roman Army"? Actually it's "Weakness" perhaps. We'll output solution in appropriate format.
But the user didn't explicitly ask for code; they described the problem. Usually the interaction: They give problem, we provide solution explanation and code. As an AI assistant, I'd respond with the solution approach and the code.
Given the format, I will produce a final answer with explanation and the C++ code.
Thus final. We need to count the number of triples (i < j < k) with a_i > a_j > a_k, where all values are distinct. The straightforward O(n^3) is impossible for n up to 1e6. The efficient solution uses a Fenwick tree (Binary Indexed Tree) after coordinate compression and processes the array twice: first to compute for each position the number of larger elements to its left, then to compute for each position the number of smaller elements to its right. The answer is the sum over all positions of the product of these two counts.
Algorithm
- Read input – n and the array a[0..n-1].
- Coordinate compression – Since values are distinct, we can map them to ranks 1..n.
- Create a sorted copy of a.
- For each original value, compute its rank using binary search.
- First pass (left to right)
- Maintain a Fenwick tree that stores frequencies of ranks seen so far.
- For each index i (0‑based):
- let r = rank[i]
- left_greater[i] = i - query(r) // i elements before, query(r) = count ≤ r
- add 1 at position r in the tree.
- Second pass (right to left)
- Clear the Fenwick tree.
- For each index i from n-1 down to 0:
- r = rank[i]
- right_smaller = query(r-1) // count of ranks < r among elements to the right
- ans += left_greater[i] * right_smaller (use 64‑bit integer)
- add 1 at position r.
- Output the answer.
Complexities: O(n log n) time, O(n) memory.
C++ Implementation
#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
using namespace std;
typedef long long ll;
const int MAXN = 1e6 + 5;
int a[MAXN]; // original values (or later ranks)
int leftGreater[MAXN]; // number of greater elements on the left
int bit[MAXN]; // Fenwick tree
int n;
void add(int idx, int val) {
while (idx <= n) {
bit[idx] += val;
idx += idx & -idx;
}
}
int query(int idx) {
int res = 0;
while (idx > 0) {
res += bit[idx];
idx -= idx & -idx;
}
return res;
}
int main() {
scanf("%d", &n);
for (int i = 0; i < n; ++i)
scanf("%d", &a[i]);
// ----- coordinate compression -----
vector<int> sorted(a, a + n);
sort(sorted.begin(), sorted.end());
for (int i = 0; i < n; ++i) {
// lower_bound gives position in sorted array (0‑based), +1 for 1‑based rank
a[i] = lower_bound(sorted.begin(), sorted.end(), a[i]) - sorted.begin() + 1;
}
// ----- first pass: count greater on the left -----
memset(bit, 0, sizeof(bit));
for (int i = 0; i < n; ++i) {
int r = a[i];
leftGreater[i] = i - query(r); // i elements before, minus those ≤ r
add(r, 1);
}
// ----- second pass: count smaller on the right and accumulate answer -----
memset(bit, 0, sizeof(bit));
ll ans = 0;
for (int i = n - 1; i >= 0; --i) {
int r = a[i];
int rightSmaller = (r > 1) ? query(r - 1) : 0;
ans += (ll)leftGreater[i] * rightSmaller;
add(r, 1);
}
printf("%I64d\n", ans); // or cout << ans << endl;
return 0;
}
Note: As recommended by the problem, %I64d is used for 64‑bit output. Alternatively, you can use cout.