結果

問題 No.875 Range Mindex Query
ユーザー HaarHaar
提出日時 2020-02-14 16:41:31
言語 Haskell
(9.8.2)
結果
CE  
(最新)
AC  
(最初)
実行時間 -
コード長 2,941 bytes
コンパイル時間 5,936 ms
コンパイル使用メモリ 156,928 KB
最終ジャッジ日時 2024-11-14 22:07:05
合計ジャッジ時間 6,504 ms
ジャッジサーバーID
(参考情報)
judge1 / judge5
このコードへのチャレンジ
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。

コンパイルメッセージ
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:17:1: error: [GHC-87110]
    Could not load module ‘Data.Map.Lazy’.
    It is a member of the hidden package ‘containers-0.6.8’.
    Use -v to see a list of the files searched for.
   |
17 | import qualified Data.Map.Lazy as Map
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ソースコード

diff #

{-# LANGUAGE DataKinds #-}
{-# LANGUAGE TypeOperators #-}
{-# LANGUAGE KindSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE MultiWayIf #-}
{-# LANGUAGE RankNTypes #-}
{-# LANGUAGE BangPatterns#-}
{-# OPTIONS_GHC -O2 #-}

import GHC.TypeLits

import Data.Int
import Data.Proxy
import qualified Data.Map.Lazy as Map
import Data.List
import Data.Bits
import Data.Maybe
import qualified Data.ByteString.Char8 as BS
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import qualified Data.Array.IArray as Array
import Data.Array.MArray
import Data.Array.ST
import Data.Monoid
import Data.Semigroup

import Control.Applicative
import Control.Monad
import Control.Monad.ST
--import Control.Monad.Primitive
import Control.Monad.State

import Debug.Trace


getInts :: IO [Int]
getInts = map (fst . fromJust . BS.readInt) . BS.words <$> BS.getLine

main :: IO ()
main = do
  [n,q] <- getInts
  a <- getInts
  q <- replicateM q $ getInts

  let f = runST $ do
        seg <- newSegmentTree n :: ST s (SegmentTree s (Min (Int, Int)))
    
        forM_ (zip [0..] a) $ \(i, x) -> updateSegmentTree seg i (Min (x, i))

        forM q $ \[i, l, r] -> do
          if
            | i == 1 -> do
                (Min (x, _)) <- getSegmentTree seg (l-1) l
                (Min (y, _)) <- getSegmentTree seg (r-1) r

                updateSegmentTree seg (l-1) (Min (y, l-1))
                updateSegmentTree seg (r-1) (Min (x, r-1))

                return Nothing
            | i == 2 -> return . Just =<< getSegmentTree seg (l-1) r
          

  mapM_ print $ map ((+1) . snd . getMin) . catMaybes $ f

  return ()



data SegmentTree s m = SegmentTree {segmentTree :: VM.MVector s m}

newSegmentTree :: Monoid m => Int -> ST s (SegmentTree s m)
newSegmentTree n = return . SegmentTree =<< VM.replicate (2 * size) mempty
  where
    size = head [m | i <- [0..], let m = 2 ^ i, m >= n]

updateSegmentTree :: Monoid m => SegmentTree s m -> Int -> m -> ST s ()
updateSegmentTree seg i val = do
  VM.write s (n + i) val
  aux $ (n + i) `div` 2

  where
    s = segmentTree seg
    n = (VM.length s) `div` 2

    aux i = do
      when (i >= 1) $ do
        VM.read s (i * 2 + 0) >>= \a -> do
          VM.read s (i * 2 + 1) >>= \b -> do
            VM.write s i (a <> b) >> aux (i `div` 2)


getSegmentTree :: Monoid m => SegmentTree s m -> Int -> Int -> ST s m
getSegmentTree seg x y = do
  aux 1 0 n
  where
    s = segmentTree seg
    n = (VM.length s) `div` 2

    aux i l r
      | r <= x || y <= l = return mempty
      | x <= l && r <= y = VM.read s i
      | otherwise =
        aux (i * 2 + 0) l ((l + r) `div` 2) >>= \a -> do
          aux (i * 2 + 1) ((l + r) `div` 2) r >>= \b -> do
            return $ a <> b
    
0