結果

問題 No.59 鉄道の旅
ユーザー aimyaimy
提出日時 2017-06-18 11:44:33
言語 Haskell
(9.8.2)
結果
AC  
実行時間 865 ms / 5,000 ms
コード長 1,477 bytes
コンパイル時間 7,939 ms
コンパイル使用メモリ 208,416 KB
実行使用メモリ 119,424 KB
最終ジャッジ日時 2024-10-01 19:40:49
合計ジャッジ時間 15,923 ms
ジャッジサーバーID
(参考情報)
judge5 / judge2
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 665 ms
117,504 KB
testcase_01 AC 658 ms
117,376 KB
testcase_02 AC 670 ms
117,248 KB
testcase_03 AC 691 ms
117,504 KB
testcase_04 AC 865 ms
108,288 KB
testcase_05 AC 672 ms
117,504 KB
testcase_06 AC 671 ms
117,504 KB
testcase_07 AC 667 ms
117,504 KB
testcase_08 AC 634 ms
96,896 KB
testcase_09 AC 623 ms
96,896 KB
testcase_10 AC 651 ms
97,024 KB
testcase_11 AC 678 ms
119,424 KB
testcase_12 AC 648 ms
99,072 KB
testcase_13 AC 703 ms
109,056 KB
testcase_14 AC 744 ms
108,160 KB
testcase_15 AC 668 ms
117,504 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