{-# Language StrictData, BangPatterns #-} import Data.List import qualified Data.Vector.Unboxed as U import Control.Monad data Fenwick = Leaf Int | Node Fenwick Int Int Fenwick deriving (Show, Eq) create :: Int -> Fenwick create 1 = Leaf 0 create n = Node (create $! n - 1) (2 ^ (n - 1)) 0 (create $! n - 1) add :: Int -> Int -> Fenwick -> Fenwick add _ x (Leaf y) = Leaf $! x + y add i x (Node lch sz ps rch) | i == sz - 1 = Node lch sz (ps + x) rch | i < sz = Node (add i x lch) sz (ps + x) rch | otherwise = Node lch sz ps $! add (i - sz) x rch binSearch :: Int -> Fenwick -> Int binSearch = go 0 where go !i rest (Leaf x) | x == rest = i | otherwise = i + 1 go !i !rest (Node lch sz ps rch) | rest <= ps = go i rest lch | otherwise = go (i + sz) (rest - ps) rch findNum :: Int -> U.Vector Int -> Int findNum x v = go 0 (U.length v) where go !l !r | r - l == 1 = l | otherwise = let m = (l + r) `div` 2 in if v U.! m <= x then go m r else go l m data Query = Add Int | Print deriving (Show, Eq) parse s = case words s of [_, n] -> Add (read n) _ -> Print collectValues :: [Query] -> U.Vector Int collectValues = U.fromList . sort . foldl' iter [] where iter !acc (Add v) = v : acc iter !acc _ = acc main :: IO () main = do [q, k] <- mapM readIO . words =<< getLine qs <- replicateM q $ parse <$> getLine let vals = collectValues qs go :: [Query] -> Int -> Fenwick -> IO () go [] _ _ = return () go (Print : rest) !cnt !fwt | cnt < k = do print $ -1 go rest cnt fwt | otherwise = do let index = binSearch k fwt print $! vals U.! index go rest (cnt - 1) (add index (-1) fwt) go (Add v : rest) !cnt !fwt = let index = findNum v vals in go rest (cnt + 1) (add index 1 fwt) go qs 0 $ create 18