
  where n = maximum xs
        b = new n :: BIT (Sum Int)
        go []     b acc = zip xs (reverse acc)
        go (x:xs) b acc = let acc' = b ! n - b ! x : acc
                              b' = inc' x 1 b
                          in go xs b' acc'

{- | 転倒対象リスト
1-9 までの数だけでできたリストの転倒している対象要素をリストアップ.

>>> inverse9' [4,1,5,2,6,3]
[(4,[]),(1,[4]),(5,[]),(2,[4,5]),(6,[]),(3,[4,5,6])]
-}
inverse9' :: [Int] -> [(Int, [Int])]
inverse9' xs = go xs b []
  where n = maximum xs
        b = new n :: BIT [Int]
        go []     b acc = zip xs (reve
inverse9 :: [Int] -> [(Int, Int)]
inverse9 xs = map (second getSum) $ go xs b []
  where n = maximum xs
        b = new n :: BIT (Sum Int)
        go []     b acc = zip xs (reverse acc)
        go (x:xs) b acc = let acc' = b ! n - b ! x : acc
                              b' = inc' x 1 b
                          in go xs b' acc'

{- | 転倒対象リスト
1-9 までの数だけでできたリストの転倒している対象要素をリストアップ.

>>> inverse9' [4,1,5,2,6,3]
[(4,[]),(1,[4]),(5,[]),(2,[4,5]),(6,[]),(3,[4,5,6])]
-}
inverse9 xs = map (second getSum) $ go xs b []
  where n = maximum xs
        b = new n :: BIT (Sum Int)
        go []     b acc = zip xs (reverse acc)
        go (x:xs) b acc = let acc' = b ! n - b ! x : acc
                              b' = inc' x 1 b
                          in go xs b' acc'

{- | 転倒対象リスト
1-9 までの数だけでできたリストの転倒している対象要素をリストアップ.

>>> inverse9' [4,1,5,2,6,3]
[(4,[]),(1,[4]),(5,[]),(2,[4,5]),(6,[]),(3,[4,5,6])]
-}