結果

問題 No.346 チワワ数え上げ問題
ユーザー pekempeypekempey
提出日時 2016-08-03 23:51:21
言語 Haskell
(9.8.2)
結果
AC  
実行時間 432 ms / 2,000 ms
コード長 1,419 bytes
コンパイル時間 4,332 ms
コンパイル使用メモリ 168,064 KB
実行使用メモリ 86,784 KB
最終ジャッジ日時 2024-04-24 15:15:54
合計ジャッジ時間 5,051 ms
ジャッジサーバーID
(参考情報)
judge5 / judge4
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
5,248 KB
testcase_01 AC 2 ms
5,248 KB
testcase_02 AC 2 ms
5,376 KB
testcase_03 AC 2 ms
5,376 KB
testcase_04 AC 2 ms
5,376 KB
testcase_05 AC 2 ms
5,376 KB
testcase_06 AC 2 ms
5,376 KB
testcase_07 AC 1 ms
5,376 KB
testcase_08 AC 1 ms
5,376 KB
testcase_09 AC 1 ms
5,376 KB
testcase_10 AC 39 ms
15,104 KB
testcase_11 AC 23 ms
12,672 KB
testcase_12 AC 31 ms
13,824 KB
testcase_13 AC 19 ms
11,776 KB
testcase_14 AC 3 ms
5,376 KB
testcase_15 AC 26 ms
12,928 KB
testcase_16 AC 35 ms
13,824 KB
testcase_17 AC 37 ms
13,696 KB
testcase_18 AC 41 ms
14,848 KB
testcase_19 AC 36 ms
13,952 KB
testcase_20 AC 37 ms
18,944 KB
testcase_21 AC 416 ms
86,784 KB
testcase_22 AC 432 ms
86,656 KB
testcase_23 AC 2 ms
5,376 KB
testcase_24 AC 1 ms
5,376 KB
testcase_25 AC 2 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 #

data Tree = NilNode | Node Integer Tree Tree

segWidth = 2 ^ 17

segVal NilNode = 0
segVal (Node val _ _) = val

segUpdate = f 0 segWidth
    where
        f l r k v NilNode = f l r k v (Node 0 NilNode NilNode)
        f l r k v (Node val left right)
            | r - l == 1      = Node (val + v) NilNode NilNode
            | otherwise       =
                let mid = (l + r) `quot` 2 in
                if k < mid then
                    let newLeft = f l mid k v left in
                        Node (segVal newLeft + segVal right) newLeft right
                else
                    let newRight = f mid r k v right in
                        Node (segVal left + segVal newRight) left newRight

segQuery = f 0 segWidth
    where
        n = 2 ^ 17
        f l r a b NilNode = 0
        f l r a b (Node val left right)
            | r <= a || b <= l = 0
            | a <= l && r <= b = val
            | otherwise        =
                let mid = (l + r) `quot` 2
                    vl = f l mid a b left
                    vr = f mid r a b right in vl + vr

main = getLine >>= print . solve
    
solve = fst . foldr f (0, NilNode) . flip zip [0..]
    where
        f (v, i) (s, tree)
            | v == 'c'  = let num = segQuery (i + 1) segWidth tree in (s + num * (num - 1) `quot` 2, tree)
            | v == 'w'  = (s, segUpdate i 1 tree)
            | otherwise = (s, tree)
                        
0