結果

問題 No.649 ここでちょっとQK!
ユーザー KPCCoiLKPCCoiL
提出日時 2018-10-18 20:30:35
言語 Haskell
(9.8.2)
結果
WA  
実行時間 -
コード長 2,000 bytes
コンパイル時間 13,203 ms
コンパイル使用メモリ 207,488 KB
実行使用メモリ 254,208 KB
最終ジャッジ日時 2024-04-24 16:28:34
合計ジャッジ時間 33,919 ms
ジャッジサーバーID
(参考情報)
judge4 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 1 ms
5,248 KB
testcase_02 WA -
testcase_03 AC 183 ms
52,096 KB
testcase_04 AC 1,361 ms
244,480 KB
testcase_05 AC 1,337 ms
244,608 KB
testcase_06 RE -
testcase_07 AC 2 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 2 ms
5,376 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 832 ms
124,800 KB
testcase_13 AC 817 ms
124,800 KB
testcase_14 AC 799 ms
118,528 KB
testcase_15 AC 803 ms
124,928 KB
testcase_16 AC 795 ms
124,928 KB
testcase_17 AC 858 ms
126,720 KB
testcase_18 AC 952 ms
137,984 KB
testcase_19 AC 1,077 ms
148,352 KB
testcase_20 AC 1,150 ms
158,720 KB
testcase_21 AC 1,244 ms
171,904 KB
testcase_22 AC 1,321 ms
185,216 KB
testcase_23 AC 1,441 ms
197,504 KB
testcase_24 AC 1,555 ms
210,816 KB
testcase_25 AC 1,643 ms
224,256 KB
testcase_26 AC 1,769 ms
241,408 KB
testcase_27 WA -
testcase_28 WA -
testcase_29 WA -
testcase_30 WA -
testcase_31 WA -
testcase_32 AC 2 ms
5,376 KB
testcase_33 AC 1 ms
5,376 KB
testcase_34 AC 1 ms
5,376 KB
testcase_35 AC 1 ms
5,376 KB
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

{-# 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
0