結果

問題 No.649 ここでちょっとQK!
ユーザー KPCCoiLKPCCoiL
提出日時 2018-10-18 20:30:35
言語 Haskell
(9.8.2)
結果
WA  
実行時間 -
コード長 2,000 bytes
コンパイル時間 10,552 ms
コンパイル使用メモリ 208,128 KB
実行使用メモリ 254,208 KB
最終ジャッジ日時 2024-11-07 01:08:11
合計ジャッジ時間 37,578 ms
ジャッジサーバーID
(参考情報)
judge5 / judge3
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 WA -
testcase_03 AC 198 ms
51,968 KB
testcase_04 AC 1,447 ms
244,608 KB
testcase_05 AC 1,462 ms
244,608 KB
testcase_06 RE -
testcase_07 AC 2 ms
5,248 KB
testcase_08 AC 2 ms
5,248 KB
testcase_09 AC 2 ms
5,248 KB
testcase_10 WA -
testcase_11 WA -
testcase_12 AC 917 ms
124,672 KB
testcase_13 AC 904 ms
124,928 KB
testcase_14 AC 887 ms
118,784 KB
testcase_15 AC 909 ms
124,800 KB
testcase_16 AC 918 ms
124,928 KB
testcase_17 AC 977 ms
126,720 KB
testcase_18 AC 1,071 ms
138,112 KB
testcase_19 AC 1,264 ms
148,352 KB
testcase_20 AC 1,309 ms
158,592 KB
testcase_21 AC 1,408 ms
171,904 KB
testcase_22 AC 1,531 ms
185,216 KB
testcase_23 AC 1,652 ms
197,376 KB
testcase_24 AC 1,797 ms
210,944 KB
testcase_25 AC 1,858 ms
224,256 KB
testcase_26 AC 1,979 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,248 KB
testcase_33 AC 2 ms
5,248 KB
testcase_34 AC 2 ms
5,248 KB
testcase_35 AC 2 ms
5,248 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