Submission #47369059


Source Code Expand

import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List

import qualified Data.Vector.Unboxed.Mutable as MUV
import Data.Function
import Data.Array

main :: IO ()
main = do
  [n,d,w] <- bsGetLnInts
  txs <- replicateM n bsGetLnInts
  ans <- abc327f n d w txs
  print ans

bsGetLnInts :: IO [Int]
bsGetLnInts = BS.getLine >>= return . unfoldr (BS.readInt . BS.dropWhile isSpace)

abc327f :: Int -> Int -> Int -> [[Int]] -> IO Int
abc327f _n d w txs =
  do
    st <- makeSegTree (1 + 2 * 10^5)
    loop 0 st txss txss
  where
    txss = filter (not . null . snd) $ assocs $ accumArray (flip (:)) [] (1, 2*10^5) [(t,x) | t:x:_ <- txs]
-- 時間差D未満を維持して、かごの範囲をセグ木に書き込み、重なりの最大値を見つける
    loop cand st lls@((t1,xs1):ls) rrs@((t2,xs2):rs)
      | t2 - t1 < d = do
        forM_ xs2 (\x2 -> updateSegTree st (x2 - w) x2 1)
        c2 <- topSegTree st
        loop (max cand c2) st lls rs
      | otherwise = do
        forM_ xs1 (\x1 -> updateSegTree st (x1 - w) x1 (-1))
        loop cand st ls rrs
    loop cand _ _ _ = return cand

data SegmentTree =
  SegmentTree Int  -- 要素数 STreeから復元できるけど
              STree -- キャッシュしている、区間の答え
              STree -- 区間更新での追加の値

type STree = MUV.IOVector Int

makeSegTree :: Int            -- 要素数
            -> IO SegmentTree -- 遅延セグ木
makeSegTree n = do
  let w = until (n <=) (2 *) 1
  vec <- MUV.replicate (w * 2) 0
  wec <- MUV.replicate (w * 2) 0
  return $ SegmentTree w vec wec

updateSegTree :: SegmentTree
              -> Int  -- 左端(含む)
              -> Int  -- 右端(含まない)
              -> Int  -- 足し込む値
              -> IO Int -- 更新された区間の結果
updateSegTree (SegmentTree wid vec wec) a b x = loop vec wec 0 wid 0
  where
    loop vec wec p q i
      | q <= a || b <= p = do
--        print ("out",a,b,p,q)
        v <- MUV.read vec i
        w <- MUV.read wec i
        return $ v + w
      | a <= p && q <= b = do
--        print ("in",a,b,p,q)
        v <- MUV.read vec i
        w <- MUV.read wec i
        let w1 = w + x
        MUV.write wec i w1
        return $ v + w1
      | otherwise = do
--        print ("half",a,b,p,q)
        let m = div (p + q) 2
        l <- loop vec wec p m (i * 2 + 1)
        r <- loop vec wec m q (i * 2 + 2)
        let lr = max l r
        MUV.write vec i lr
        w <- MUV.read wec i
        return $ lr + w

topSegTree :: SegmentTree
              -> IO Int -- 答え
topSegTree (SegmentTree _ vec wec) = do
  v <- MUV.read vec 0
  w <- MUV.read wec 0
  return $ v + w

test1 = abc327f 8 4 3 [[1, 1],[3, 4],[6, 4],[5, 2],[4, 2],[4, 3],[5, 5],[7, 3]]
-- 5

{-
ユーザ解説の手法を輸入する。
まず、ソートをバケツソートにするという大技。
-}

Submission Info

Submission Time
Task F - Apples
User joetheshootingst
Language Haskell (GHC 9.4.5)
Score 550
Code Size 3013 Byte
Status AC
Exec Time 423 ms
Memory 100296 KiB

Compile Error

app/Main.hs:7:1: warning: [-Wunused-imports]
    The import of ‘Data.Function’ is redundant
      except perhaps to import instances from ‘Data.Function’
    To import instances alone, use: import Data.Function()
  |
7 | import Data.Function
  | ^^^^^^^^^^^^^^^^^^^^

app/Main.hs:23:34: warning: [-Wtype-defaults]
    • Defaulting the type variable ‘b0’ to type ‘Integer’ in the following constraints
        (Integral b0) arising from a use of ‘^’ at app/Main.hs:23:34
        (Num b0) arising from the literal ‘5’ at app/Main.hs:23:35
    • In the second argument of ‘(*)’, namely ‘10 ^ 5’
      In the second argument of ‘(+)’, namely ‘2 * 10 ^ 5’
      In the first argument of ‘makeSegTree’, namely ‘(1 + 2 * 10 ^ 5)’
   |
23 |     st <- makeSegTree (1 + 2 * 10^5)
   |                                  ^

app/Main.hs:26:82: warning: [-Wtype-defaults]
    • Defaulting the type variable ‘b0’ to type ‘Integer’ in the following constraints
        (Integral b0) arising from a use of ‘^’ at app/Main.hs:26:82
        (Num b0) arising from the literal ‘5’ at app/Main.hs:26:83
    • In the second argument of ‘(*)’, namely ‘10 ^ 5’
      In the expression: 2 * 10 ^ 5
      In the third argument of ‘accumArray’, namely ‘(1, 2 * 10 ^ 5)’
   |
26 |     txss = filter (not . null . snd) $ assocs $ accumArray (flip (:)) [] (1, 2*10^5) [(t,x) | t:x:_ <- txs]
   |                                                                                  ^

app/Main.hs:60:10: warning: [-Wname-shadowing]
    This binding for ‘vec’ shadows the existing binding
      bound at app/Main.hs:58:32
   |
60 |     loop vec wec p q i
   |          ^^^

app/Main.hs:60:14: warning: [-Wname-shadowing]
    This binding for ‘wec’ shadows the existing binding
      bound at app/Main.hs:58:36
   |
60 |     loop vec wec p q i
   |              ^^^

app/Main.hs:90:1: warning: [-Wmissing-signatures]
    Top-level binding with no type signature: test1 :: IO Int
   |
90 | test1 = abc327f 8 4 3 [[1, 1],[3, 4],[6, 4],[5, 2],[4, 2],[4, 3],[5, 5],[7, 3]]
   | ^^^^^

app/Main.hs:90:1: warning: [-Wunused-top-binds]
    Defined but not used: ‘test1’
   |
90 | test1 = abc327f 8 4 3 [[1, 1],[3, 4],[6, 4],[5, 2],[4, 2],[4, 3],[5, 5],[7, 3]]
   | ^^^^^

Judge Result

Set Name Sample All
Score / Max Score 0 / 0 550 / 550
Status
AC × 1
AC × 36
Set Name Test Cases
Sample example_00.txt
All example_00.txt, hand_00.txt, hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, hand_05.txt, hand_06.txt, random_00.txt, random_01.txt, random_02.txt, random_03.txt, random_04.txt, random_05.txt, random_06.txt, random_07.txt, random_08.txt, random_09.txt, random_10.txt, random_11.txt, random_12.txt, random_13.txt, random_14.txt, random_15.txt, random_16.txt, random_17.txt, random_18.txt, random_19.txt, random_20.txt, random_21.txt, random_22.txt, random_23.txt, random_24.txt, random_25.txt, random_26.txt, random_27.txt
Case Name Status Exec Time Memory
example_00.txt AC 6 ms 16660 KiB
hand_00.txt AC 303 ms 73576 KiB
hand_01.txt AC 303 ms 73580 KiB
hand_02.txt AC 223 ms 87588 KiB
hand_03.txt AC 264 ms 99360 KiB
hand_04.txt AC 253 ms 70096 KiB
hand_05.txt AC 6 ms 16608 KiB
hand_06.txt AC 6 ms 16708 KiB
random_00.txt AC 7 ms 17204 KiB
random_01.txt AC 7 ms 16980 KiB
random_02.txt AC 7 ms 17056 KiB
random_03.txt AC 7 ms 16828 KiB
random_04.txt AC 353 ms 93988 KiB
random_05.txt AC 354 ms 99168 KiB
random_06.txt AC 393 ms 94160 KiB
random_07.txt AC 413 ms 84076 KiB
random_08.txt AC 314 ms 98212 KiB
random_09.txt AC 373 ms 96512 KiB
random_10.txt AC 294 ms 100296 KiB
random_11.txt AC 423 ms 94164 KiB
random_12.txt AC 383 ms 85728 KiB
random_13.txt AC 353 ms 81716 KiB
random_14.txt AC 393 ms 82656 KiB
random_15.txt AC 413 ms 96456 KiB
random_16.txt AC 243 ms 75664 KiB
random_17.txt AC 303 ms 74212 KiB
random_18.txt AC 293 ms 77452 KiB
random_19.txt AC 303 ms 74452 KiB
random_20.txt AC 253 ms 73616 KiB
random_21.txt AC 273 ms 72304 KiB
random_22.txt AC 273 ms 73752 KiB
random_23.txt AC 213 ms 72096 KiB
random_24.txt AC 182 ms 63920 KiB
random_25.txt AC 212 ms 66676 KiB
random_26.txt AC 222 ms 64148 KiB
random_27.txt AC 242 ms 69388 KiB