Permutations on the Road: Alice
InterviewTime limit3sMemory limit1024 MB
Given a permutation, sum the number of inversions over every contiguous subarray. Each inversion pair contributes once for every subarray that contains both positions.
- Level
Medium5 of 10
- Topics
- Array, Combinatorics, Prefix sum, Math
- Solved
- No attempts yet
Problem
Alice and Bob frequently take long road trips to get to various programming competitions in their area. Since everything is bigger in the state they live in, they have turned to playing car games to pass the time.
Alice and Bob are both computer scientists, so they quickly tired of "guess the number", since the guesser could always identify the number using a logarithmic number of guesses. To raise the challenge, they created a new game: "guess the permutation".
A permutation of length is an arrangement of the numbers . For a given permutation , define to be the number of pairs with such that .
When playing this game, Alice thinks of a permutation, and Bob can ask Alice for the result of the function for up to inputs.
Alice has already thought of her permutation . As she mindlessly answers all of Bob's queries, she thinks of a different problem: What is the sum of over all values of and ?
Input
The first line of input contains a single integer (), the length of the permutation. The second line of input contains space-separated integers, Alice's permutation .
Output
Output a single integer, the sum of over all values of and . The answer is guaranteed to fit in a signed -bit integer.
Hint
In the first sample case, there are no inversions.
In the second sample case,
- .
- .
- .
- .
- .
- .
Summing these values gives .