結果
| 問題 |
No.875 Range Mindex Query
|
| コンテスト | |
| ユーザー |
|
| 提出日時 | 2019-09-14 21:56:50 |
| 言語 | Haskell (9.10.1) |
| 結果 |
TLE
|
| 実行時間 | - |
| コード長 | 2,145 bytes |
| コンパイル時間 | 10,050 ms |
| コンパイル使用メモリ | 175,104 KB |
| 実行使用メモリ | 118,272 KB |
| 最終ジャッジ日時 | 2024-07-06 14:45:03 |
| 合計ジャッジ時間 | 14,769 ms |
|
ジャッジサーバーID (参考情報) |
judge5 / judge2 |
(要ログイン)
| ファイルパターン | 結果 |
|---|---|
| sample | AC * 1 |
| other | AC * 10 TLE * 1 -- * 7 |
コンパイルメッセージ
Loaded package environment from /home/judge/.ghc/x86_64-linux-9.8.2/environments/default
[1 of 2] Compiling Main ( Main.hs, Main.o )
Main.hs:10:23: warning: [GHC-63394] [-Wx-partial]
In the use of ‘head’
(imported from Prelude, but defined in GHC.List):
"This is a partial function, it throws an error on empty lists. Use pattern matching or Data.List.uncons instead. Consider refactoring to use Data.List.NonEmpty."
|
10 | | n == 1 = Leaf $ head lis
| ^^^^
[2 of 2] Linking a.out
ソースコード
import Control.Monad
import Control.Monad.ST
import Data.STRef
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] <- map read . words <$> getLine
a <- map read . words <$> getLine
ops <- replicateM q $ map read . words <$> getLine
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