Description
The Romans had attacked again. This time they was much more than the Persians but Shapur was ready to defeat them. He says: "A lion is never afraid of a hundred sheep".
Nevertheless Shapur have to find weaknesses in the Roman army to defeat them. So he gives the army a weakness number.
In Shapur's opinion the weakness of an armyare equal to the number of tripletsi,? J,? k such that i? <? J. < k and ai;? AJ.? Ak whereax is the power of man standing at position< C22>x. The Roman Army has a special trait-powers of all the people in it is distinct.
Help Shapur find out how weak the Romans is.
Input
The first line of input contains a single number n (3?≤? N? ≤?106)-the number of men in Roman army. Next line containsn different positive integers ai (1?≤? I? ≤? n,? 1?≤? a i? ≤?109) -powers of men in the Roman army.
Output
A single integer number, the weakness of the Roman army.
%lld specificator to read or write 64-bit integers in C + +. It's preffered to usecout (also).
Sample Input
Input
33 2 1
Output
1
Input
32 3 1
Output
0
Input
410 8 3 1
Output
4
Input
41 5 4 3
Output
1
Test instructions: To satisfy the number of three numbers is subscript i < J < K, value a[i] > A[j] > A[k]
Idea: For each number we look forward to a larger number, looking backwards than its small number, multiplied to get the result of this number. Then count the total number of possible, based on the number is too large, we use the merge algorithm. In the process of calculation
#include <iostream> #include <cstdio> #include <cstring> #include <algorithm>using namespace Std;typedef Long Long ll;const int MAXN = 1e6 + 10;struct Node {ll Val, front, Rear;} A[MAXN], B[maxn];ll ans;void merge_s ORT (node *a, int x, int y, node *t) {if (Y-x > 1) {int m = x + (y-x)/2;int p = x, q = m, i = X;merge_sort (A, X, M, t); Merge_sort (A, M, y, T), while (P < m | | Q < y) {if (q >= y | | (P < m && a[p].val <= a[q].val)) {ans + = A[p].front * (q-m); A[p].rear + = (q-m); t[i++] = a[p++];} else {ans + = A[q].rear * (m-p); A[q].front + = (m-p); t[i++] = a[q++];}} for (i = x; i < y; i++) a[i] = T[i];}} int main () {int n;scanf ("%d", &n), for (int i = 0; i < n; i++) scanf ("%lld", &a[i].val); ans = 0;merge_sort (A, 0, n, b); cout << ans << endl;return 0;}
CODEFORCES-61E Enemy is weak