結果

問題 No.875 Range Mindex Query
ユーザー wesriverywesrivery
提出日時 2019-09-17 01:43:09
言語 Haskell
(9.8.2)
結果
TLE  
実行時間 -
コード長 2,731 bytes
コンパイル時間 2,291 ms
コンパイル使用メモリ 178,868 KB
実行使用メモリ 11,896 KB
最終ジャッジ日時 2023-09-21 08:55:49
合計ジャッジ時間 7,629 ms
ジャッジサーバーID
(参考情報)
judge11 / judge13
このコードへのチャレンジ
(要ログイン)

テストケース

テストケース表示
入力 結果 実行時間
実行使用メモリ
testcase_00 AC 2 ms
6,984 KB
testcase_01 AC 9 ms
11,264 KB
testcase_02 AC 12 ms
11,352 KB
testcase_03 AC 3 ms
7,880 KB
testcase_04 AC 6 ms
10,540 KB
testcase_05 AC 5 ms
9,728 KB
testcase_06 AC 12 ms
11,400 KB
testcase_07 AC 12 ms
11,264 KB
testcase_08 AC 7 ms
11,344 KB
testcase_09 AC 6 ms
10,168 KB
testcase_10 AC 22 ms
11,896 KB
testcase_11 TLE -
testcase_12 -- -
testcase_13 -- -
testcase_14 -- -
testcase_15 -- -
testcase_16 -- -
testcase_17 -- -
testcase_18 -- -
権限があれば一括ダウンロードができます
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.6.1/environments/default
[1 of 2] Compiling Main             ( Main.hs, Main.o )
[2 of 2] Linking a.out

ソースコード

diff #

import Control.Monad
import Data.Maybe
import qualified Data.ByteString.Char8 as BS

tuplify2 (x:y:_) = (x,y)
tuplify2 _ = undefined

--Input functions with ByteString
readInt = fromInteger . fst . fromJust . BS.readInteger
readIntTuple = tuplify2 . map readInt . BS.words
readIntList = map readInt . BS.words

getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine
getIntNList n = map readIntList <$> replicateM (fromIntegral n) BS.getLine
getIntMatrix = map readIntList . BS.lines <$> BS.getContents
getIntTuple = readIntTuple <$> BS.getLine
getIntNTuples n = map readIntTuple <$> replicateM (fromIntegral n) BS.getLine
getIntTuples = map readIntTuple . BS.lines <$> BS.getContents



data SegmentTree = Node Int Int SegmentTree SegmentTree | Leaf Int
    deriving Show

newTree :: [Int] -> SegmentTree
newTree lis
    | n == 1 = Leaf $ head lis
    | otherwise = Node value n left right
    where n = length lis
          half = n `div` 2
          left = newTree $ take half lis
          right = newTree $ drop half lis
          value = min (val left) (val right)

query :: Int -> Int -> SegmentTree -> (Int, Int)
query _ _ (Leaf v) = (0, v)
query from to (Node v n left right)
    | to < half      = qLeft
    | half <= from    = qRight
    | from < half && half <= to =  minTuple qLeft qRight
    where half = n `div` 2
          qLeft = query from to left
          qRight = ((fst qRightRaw) + half, snd qRightRaw)
          qRightRaw = query (from - half) (to - half) right
          minTuple t1 t2 = if (snd t1) < (snd t2) then t1 else t2

update :: Int -> Int -> SegmentTree -> SegmentTree
update _ newval (Leaf v) = Leaf newval
update index newval (Node v n left right)
    | index < half = Node (min (val newLeft) (val right)) n newLeft right
    | otherwise = Node (min (val left) (val newRight)) n left newRight
    where half = n `div` 2
          newLeft = update index newval left
          newRight = update (index - half) newval right

val :: SegmentTree -> Int
val (Node value _ _ _) = value
val (Leaf value) = value

main = do

    [n, q] <- getIntList

    a <- getIntList

    ops <- replicateM q $ getIntList

    let tree = newTree a

    operate tree ops

operate :: SegmentTree -> [[Int]] -> IO ()
operate tree [] = return ()
operate tree (op:ops) = do
    let [operation, ap, bp] = op
    let a = ap - 1
    let b = bp - 1

    case operation of
        1 -> do
            let (aind, aval) = query a a tree
            let (bind, bval) = query b b tree
            let newTree1 = update aind bval tree
            let newTree2 = update bind aval newTree1
            operate newTree2 ops
        2 -> do
            print $ (+1) $ fst (query a b tree)
            operate tree ops


0