=> BIT a -> Int -> a
b ! i | 1 <= i && i <= n = foldr (\i acc -> acc <> v V.! i) mempty xs
      | otherwise = error $ "index must be between 1 to " ++ show n
  where (n, v) = (getN b, vec b)
        xs = takeWhile (>0) . iterate (\x -> x - x .&. (-x)) $ i

--------------------------------------------------------------------------------


{- | 転倒数
1-9 までの数だけでできたリストの転倒数をもとの数と組にして返す.

>>> inverse9 [4,1,5,2,6,3]
[(4,0),(1,1),(5,0),(2,2),(6,0),(3,3)]
-}
inverse9 :: [Int] -> [(Int, Int)]
inverse9 xs = map (second getSum) $ go xs b [
(!) :: Monoid a => BIT a -> Int -> a
b ! i | 1 <= i && i <= n = foldr (\i acc -> acc <> v V.! i) mempty xs
      | otherwise = error $ "index must be between 1 to " ++ show n
  where (n, v) = (getN b, vec b)
        xs = takeWhile (>0) . iterate (\x -> x - x .&. (-x)) $ i

--------------------------------------------------------------------------------


{- | 転倒数
1-9 までの数だけでできたリストの転倒数をもとの数と組にして返す.

>>> inverse9 [4,1,5,2,6,3]
[(4,0),(1,1),(5,0),(2,2),(6,0),(3,3)]
-}
b ! i | 1 <= i && i <= n = foldr (\i acc -> acc <> v V.! i) mempty xs
      | otherwise = error $ "index must be between 1 to " ++ show n
  where (n, v) = (getN b, vec b)
        xs = takeWhile (>0) . iterate (\x -> x - x .&. (-x)) $ i

--------------------------------------------------------------------------------


{- | 転倒数
1-9 までの数だけでできたリストの転倒数をもとの数と組にして返す.

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