結果
問題 | No.135 とりあえず1次元の問題 |
ユーザー | こまる |
提出日時 | 2020-10-07 22:07:34 |
言語 | Haskell (9.8.2) |
結果 |
CE
(最新)
AC
(最初)
|
実行時間 | - |
コード長 | 5,223 bytes |
コンパイル時間 | 1,139 ms |
コンパイル使用メモリ | 176,512 KB |
最終ジャッジ日時 | 2024-11-14 23:51:14 |
合計ジャッジ時間 | 2,004 ms |
ジャッジサーバーID (参考情報) |
judge1 / judge3 |
(要ログイン)
コンパイルエラー時のメッセージ・ソースコードは、提出者また管理者しか表示できないようにしております。(リジャッジ後のコンパイルエラーは公開されます)
ただし、clay言語の場合は開発者のデバッグのため、公開されます。
ただし、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:44:24: error: [GHC-87543] Ambiguous occurrence ‘.<<.’. It could refer to either ‘Data.Bits..<<.’, imported from ‘Data.Bits’ at Main.hs:5:1-26, or ‘Main..<<.’, defined at Main.hs:22:1. | 44 | $ (x + 0x3fffffff) .<<. 31 .|. (y + 0x3fffffff) | ^^^^ Main.hs:47:15: error: [GHC-87543] Ambiguous occurrence ‘.>>.’. It could refer to either ‘Data.Bits..>>.’, imported from ‘Data.Bits’ at Main.hs:5:1-26, or ‘Main..>>.’, defined at Main.hs:25:1. | 47 | !x = xy .>>. 31 - 0x3fffffff | ^^^^ Main.hs:49:49: error: [GHC-87543] Ambiguous occurrence ‘.<<.’. It could refer to either ‘Data.Bits..<<.’, imported from ‘Data.Bits’ at Main.hs:5:1-26, or ‘Main..<<.’, defined at Main.hs:22:1. | 49 | encodeNonNegative64 (x, y) = unsafeCoerce $ x .<<. 31 .|. y | ^^^^ Main.hs:52:15: error: [GHC-87543] Ambiguous occurrence ‘.>>.’. It could refer to either ‘Data.Bits..>>.’, imported from ‘Data.Bits’ at Main.hs:5:1-26, or ‘Main..>>.’, defined at Main.hs:25:1. | 52 | !x = xy .>>. 31 | ^^^^ Main.hs:57:22: error: [GHC-87543] Ambiguous occurrence ‘.<<.’. It could refer to either ‘Data.Bits..<<.’, imported from ‘Data.Bits’ at Main.hs:5:1-26, or ‘Main..<<.’, defined at Main.hs:22:1. | 57 | $ ((x + 0xfffff) .<<. 21 .|. (y + 0xfffff)) .<<. 21 .|. (z + 0xfffff) | ^^^^ Main.hs:57:49: error: [GHC-87543] Ambiguous occurrence ‘.<<.’. It could refer to either ‘Data.Bits..<<.’, i
ソースコード
{-# LANGUAGE BangPatterns #-} {-# LANGUAGE FlexibleInstances #-} import Control.Arrow import Control.Monad import Data.Bits import qualified Data.ByteString.Char8 as BSC8 import qualified Data.Foldable as F import qualified Data.Vector.Fusion.Stream.Monadic as VFSM import qualified Data.Vector.Unboxed as VU import qualified Data.Vector.Unboxed.Mutable as VUM import Data.Word import Unsafe.Coerce type Parser a = BSC8.ByteString -> Maybe (a, BSC8.ByteString) parseInt :: Parser Int parseInt = fmap (second BSC8.tail) . BSC8.readInt parseM :: Int -> IO (VU.Vector Int) parseM m = VU.unfoldrN m parseInt <$> BSC8.getLine infixl 8 .<<., .>>. (.<<.) :: Bits b => b -> Int -> b (.<<.) = unsafeShiftL {-# INLINE (.<<.) #-} (.>>.) :: Bits b => b -> Int -> b (.>>.) = unsafeShiftR {-# INLINE (.>>.) #-} class Word64Encode a where encode64 :: a -> Word64 decode64 :: Word64 -> a encodeNonNegative64 :: a -> Word64 encodeNonNegative64 = encode64 decodeNonNegative64 :: Word64 -> a decodeNonNegative64 = decode64 instance Word64Encode Int where encode64 x = unsafeCoerce $ x + 0x3fffffffffffffff decode64 x = unsafeCoerce x - 0x3fffffffffffffff encodeNonNegative64 = unsafeCoerce decodeNonNegative64 = unsafeCoerce instance Word64Encode (Int, Int) where encode64 (x, y) = unsafeCoerce $ (x + 0x3fffffff) .<<. 31 .|. (y + 0x3fffffff) decode64 xy = unsafeCoerce (x, y) where !x = xy .>>. 31 - 0x3fffffff !y = (xy .&. 0x7fffffff) - 0x3fffffff encodeNonNegative64 (x, y) = unsafeCoerce $ x .<<. 31 .|. y decodeNonNegative64 xy = unsafeCoerce (x, y) where !x = xy .>>. 31 !y = xy .&. 0x7fffffff instance Word64Encode (Int, Int, Int) where encode64 (x, y, z) = unsafeCoerce $ ((x + 0xfffff) .<<. 21 .|. (y + 0xfffff)) .<<. 21 .|. (z + 0xfffff) decode64 xyz = unsafeCoerce (x, y, z) where !x = xyz .>>. 42 - 0xfffff !y = (xyz .>>. 21 .&. 0x1fffff) - 0xfffff !z = xyz .&. 0x1fffff - 0xfffff encodeNonNegative64 (x, y, z) = unsafeCoerce $ (x .<<. 21 .|. y) .<<. 21 .|. z decodeNonNegative64 xyz = unsafeCoerce (x, y, z) where !x = xyz .>>. 42 !y = xyz .>>. 21 .&. 0x1fffff !z = xyz .&. 0x1fffff radixSortInt :: VU.Vector Int -> VU.Vector Int radixSortInt = unsafeCoerce . radixSort64 . unsafeCoerce radixSort64 :: VU.Vector Word64 -> VU.Vector Word64 radixSort64 vword = F.foldl' step vword [0,16,32,48] where mask k x = fromIntegral $ x .>>. k .&. 0xffff step v k = VU.create $ do pref <- VU.unsafeThaw . VU.prescanl' (+) 0 . VU.unsafeAccumulate (+) (VU.replicate 0x10000 0) $ VU.map (flip (,) 1. mask k) v res <- VUM.unsafeNew $ VU.length v VU.forM_ v $ \x -> do let !masked = mask k x i <- VUM.unsafeRead pref masked VUM.unsafeWrite pref masked $ i + 1 VUM.unsafeWrite res i x return res {-# INLINE radixSort64 #-} radixSort :: (VU.Unbox a, Word64Encode a) => VU.Vector a -> VU.Vector a radixSort = VU.map decode64 . radixSort64 . VU.map encode64 {-# INLINE radixSort #-} radixSortNonNegative :: (VU.Unbox a, Word64Encode a) => VU.Vector a -> VU.Vector a radixSortNonNegative = VU.map decodeNonNegative64 . radixSort64 . VU.map encodeNonNegative64 {-# INLINE radixSortNonNegative #-} radixSort32 :: VU.Vector Word32 -> VU.Vector Word32 radixSort32 vec = F.foldl' step vec [0, 16] where mask k x = fromIntegral $ x .>>. k .&. 0xffff step v k = VU.create $ do pref <- VU.unsafeThaw . VU.prescanl' (+) 0 . VU.unsafeAccumulate (+) (VU.replicate 0x10000 0) $ VU.map (flip (,) 1 . mask k) v res <- VUM.unsafeNew $ VU.length v VU.forM_ v $ \x -> do let !masked = mask k x i <- VUM.unsafeRead pref masked VUM.unsafeWrite pref masked $ i + 1 VUM.unsafeWrite res i x return res {-# INLINE radixSort32 #-} compress :: VU.Vector Int -> VU.Vector Int compress vec = VU.create $ do mvec <- VUM.unsafeNew (VU.length vec) VU.mapM_ (\(i, x) -> VUM.unsafeWrite mvec (x .&. 0xffffffff) i) . VU.postscanl' (\(!i, !x) y -> if x .>>. 32 == y .>>. 32 then (i, y) else (i + 1, y) ) (-1, -1) . radixSortInt $ VU.imap (\i x -> x .<<. 32 .|. i) vec return mvec {-# INLINE compress #-} streamR :: Monad m => Int -> Int -> VFSM.Stream m Int streamR !l !r = VFSM.Stream step (r - 1) where step x | x >= l = return $ VFSM.Yield x (x - 1) | otherwise = return $ VFSM.Done {-# INLINE [0] step #-} {-# INLINE [1] streamR #-} rev :: Monad m => Int -> (Int -> m ()) -> m () rev n = flip VFSM.mapM_ (streamR 0 n) {-# INLINE rev #-} main :: IO () main = do m <- readLn :: IO Int print =<< solver m . radixSortInt =<< parseM m solver :: Int -> VU.Vector Int -> IO Int solver m xs = do ret <- VU.unsafeThaw $ VU.replicate 1 (1000001 :: Int) rev (m - 1) $ \i -> do k <- VUM.unsafeRead ret 0 let j = xs VU.! (i + 1) - xs VU.! i when (j /= 0 && k > j) $ VUM.unsafeWrite ret 0 j y <- VUM.unsafeRead ret 0 return $ if y == 1000001 then 0 else y