Problem:
MathJax.Hub.Config({ tex2jax: { inlineMath: [["$","$"], ["\\(","\\)"]], processEscapes: true }});

The Number of Inversions

For a given sequence $A = \{a_0, a_1, ... a_{n-1}\}$, the number of pairs $(i, j)$ where $a_i > a_j$ and $i < j$, is called the number of inversions. The number of inversions is equal to the number of swaps of Bubble Sort defined in the following program:

bubbleSort(A)
cnt = 0 // the number of inversions
for i = 0 to A.length-1
for j = A.length-1 downto i+1
if A[j] < A[j-1]
swap(A[j], A[j-1])
cnt++

return cnt

For the given sequence $A$, print the number of inversions of $A$. Note that you should not use the above program, which brings Time Limit Exceeded.

Input

In the first line, an integer $n$, the number of elements in $A$, is given. In the second line, the elements $a_i$ ($i = 0, 1, .. n-1$) are given separated by space characters.

output

Print the number of inversions in a line.

Constraints

$ 1 \leq n \leq 200,000$

$ 0 \leq a_i \leq 10^9$

$a_i$ are all different

Sample Input 1

5
3 5 2 1 4

Sample Output 1

6

Sample Input 2

3
3 1 2

Sample Output 2

2

Solution in Haskell (judge verdict: Time Limit Exceeded)

{-# OPTIONS_GHC -fno-warn-orphans #-}

import Control.Applicative
import Data.Monoid
import Data.List
import Data.Ord
import qualified Data.ByteString.Char8 as B

readInt :: B.ByteString -> Int
readInt = maybe undefined fst . B.readInt

readInts :: B.ByteString -> [Int]
readInts = map readInt . B.words

main :: IO ()
main = do
  n <- readLn :: IO Int
  xs <- readInts <$> B.getLine
  print (inversion n xs)

inversion :: Size -> [Int] -> Int
inversion n = fst . foldl go (0, seg0) . zip [0 .. pred n] . icomp
  where
    seg0 = fromList n (replicate n mempty)
    go (acc, seg) (j, a) = (acc + (j - query n seg 1 a), update n seg a succ)

icomp :: Ord a => [a] -> [Int]
icomp = map fst . sortBy (comparing (fst.snd)) . zip [1..] . sortBy (comparing snd) . zip [0..]

instance Monoid Int where
  mempty = 0
  mappend = (+)

data SegTree m =
  Leaf !m |
  Node !m !(SegTree m) !(SegTree m)
  deriving (Eq, Show)

type Index = Int
type Size = Int

val :: Monoid m => SegTree m -> m
val (Leaf v) = v
val (Node v _ _) = v

fromList :: Monoid m => Size -> [m] -> SegTree m
fromList 1 [x] = Leaf x
fromList n xs = Node (val left <> val right) left right
  where
    m = div n 2
    (xs1, xs2) = splitAt m xs
    left = fromList m xs1
    right = fromList (n - m) xs2

update :: Monoid m => Size -> SegTree m -> Index -> (m -> m) -> SegTree m
update 1 (Leaf v) 1 f = Leaf (f v)
update _ (Leaf _) _ _ = error "update: Pattern match failed."
update n (Node _ l r) i x
  | i <= m = Node (val left <> val r) left r
  | otherwise = Node (val l <> val right) l right
  where
    m = div n 2
    left = update m l i x
    right = update (n - m) r (i - m) x

query :: Monoid m => Size -> SegTree m -> Index -> Index -> m
query 1 (Leaf v) 1 1 = v
query _ (Leaf _) _ _ = error "query: Pattern match failed."
query n (Node v l r) i j
  | (i, j) == (1, n) = v
  | j <= m = query m l i j
  | i > m = query (n - m) r (i - m) (j - m)
  | otherwise = query m l i m <> query (n - m) r 1 (j - m)
  where
    m = div n 2