結果

問題 No.59 鉄道の旅
ユーザー aimyaimy
提出日時 2017-06-18 11:44:33
言語 Haskell
(9.8.2)
結果
AC  
実行時間 756 ms / 5,000 ms
コード長 1,477 bytes
コンパイル時間 3,274 ms
コンパイル使用メモリ 207,900 KB
実行使用メモリ 119,680 KB
最終ジャッジ日時 2024-04-09 19:30:03
合計ジャッジ時間 14,120 ms
ジャッジサーバーID
(参考情報)
judge3 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 628 ms
117,632 KB
testcase_01 AC 622 ms
117,632 KB
testcase_02 AC 633 ms
117,376 KB
testcase_03 AC 620 ms
117,632 KB
testcase_04 AC 756 ms
108,416 KB
testcase_05 AC 631 ms
117,376 KB
testcase_06 AC 630 ms
117,376 KB
testcase_07 AC 629 ms
117,376 KB
testcase_08 AC 591 ms
96,896 KB
testcase_09 AC 583 ms
97,152 KB
testcase_10 AC 592 ms
96,896 KB
testcase_11 AC 629 ms
119,680 KB
testcase_12 AC 589 ms
98,944 KB
testcase_13 AC 668 ms
109,184 KB
testcase_14 AC 682 ms
108,416 KB
testcase_15 AC 642 ms
117,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 BangPatterns #-}

import Data.List
import qualified Data.ByteString.Char8 as B
import Data.Maybe

data SegTree a = Leaf !a | Node !a (SegTree a) (SegTree a) deriving (Eq, Show)

val :: SegTree a -> a
val (Leaf !v) = v
val (Node !v _ _) = v

fromList :: (Num a) => Int -> [a] -> SegTree a
fromList 1 [x] = Leaf x
fromList n xs = Node (val left + val right) left right
 where
  n' = div n 2
  (xs1,xs2) = splitAt n' xs
  left = fromList n' xs1
  right = fromList (n-n') xs2

update :: (Num a) => Int -> SegTree a -> Int -> a -> SegTree a
update 1 (Leaf !v) 1 x = Leaf (v+x)
update n (Node _ l r) i x
 | i <= n' = Node (val left + val r) left r
 | otherwise = Node (val l + val right) l right
 where
  n' = div n 2
  left = update n' l i x
  right = update (n-n') r (i-n') x

query :: (Num a) => Int -> SegTree a -> Int -> Int -> a
query 1 (Leaf !v) 1 1 = v
query n (Node !v l r) i j
 | (i,j) == (1,n) = v
 | j <= n' = query n' l i j
 | i > n' = query (n-n') r (i-n') (j-n')
 | otherwise = query n' l i n' + query (n-n') r 1 (j-n')
 where n' = div n 2

maxv = 1000000

main = do
 [_,k] <- map read . words <$> getLine
 ws <- map (fst . fromJust . B.readInt) . B.words <$> B.getContents
 let seg = fromList maxv (replicate maxv 0)
 print (freight k seg ws)

freight k seg = val . foldl' (dp k) seg

dp k seg w
 | w < 0 && query maxv seg (abs w) (abs w) /= 0 = update maxv seg (abs w) (-1)
 | w > 0 && query maxv seg w maxv < k = update maxv seg w 1
 | otherwise = seg
0