Submission #66903242
Source Code Expand
import Control.Monad
import qualified Data.ByteString.Char8 as BS
import Data.Char
import Data.List
import qualified Data.Vector as V
import qualified Data.Vector.Unboxed as UV
import qualified Data.Vector.Unboxed.Mutable as MUV
import Control.Monad.ST
main :: IO ()
main = do
[t] <- bsGetLnInts
replicateM_ t $ do
[h,w] <- bsGetLnInts
ss <- replicateM h $ BS.take w <$> BS.getLine
let ans = abc410f h w ss
print ans
bsGetLnInts :: IO [Int]
bsGetLnInts = unfoldr (BS.readInt . BS.dropWhile isSpace) <$> BS.getLine
abc410f :: Int -> Int -> [BS.ByteString] -> Int
abc410f h w bss
| h <= w = sub h w sN
| otherwise = sub w h sT
where
c2i '#' = 1
c2i _ = -1
-- そのまま累積和を作る
sN = V.scanl (UV.zipWith (+)) (UV.replicate (succ w) 0) $ V.fromListN h $
map (UV.scanl (+) 0 . UV.fromListN w . map c2i . BS.unpack) bss
-- 転置して累積和を作る
sT = V.scanl (UV.zipWith (+)) (UV.replicate (succ h) 0) $ V.fromListN w $
[UV.scanl (+) 0 $ UV.fromListN h [c2i $ BS.index bs i | bs <- bss] | i <- [0 .. pred w]]
sub :: Int -> Int -> V.Vector (UV.Vector Int) -> Int
sub h w s = countEm (h * w) [UV.zipWith (-) (s V.! d) (s V.! u) | u <- [0 .. pred h], d <- [succ u .. h]]
countEm :: Int -> [UV.Vector Int] -> Int
countEm hw vs = runST $ do
cnt <- MUV.new (hw + succ hw) :: ST s (MUV.MVector s Int)
flip div 2 . sum <$> forM vs (\v -> do
UV.mapM_ (\x -> MUV.modify cnt succ (x + hw)) v
UV.foldM (\acc x -> do
y <- MUV.read cnt (x + hw)
if y == 0 then return acc else do
MUV.write cnt (x + hw) 0
return $! acc + y * pred y
) 0 v
)
Submission Info
| Submission Time | |
|---|---|
| Task | F - Balanced Rectangles |
| User | joetheshootingst |
| Language | Haskell (GHC 9.4.5) |
| Score | 525 |
| Code Size | 1708 Byte |
| Status | AC |
| Exec Time | 482 ms |
| Memory | 96056 KiB |
Judge Result
| Set Name | Sample | All | ||||
|---|---|---|---|---|---|---|
| Score / Max Score | 0 / 0 | 525 / 525 | ||||
| Status |
|
|
| Set Name | Test Cases |
|---|---|
| Sample | sample_01.txt |
| All | hand_01.txt, hand_02.txt, hand_03.txt, hand_04.txt, sample_01.txt, test_01.txt, test_02.txt, test_03.txt, test_04.txt, test_05.txt, test_06.txt, test_07.txt, test_08.txt, test_09.txt, test_10.txt, test_11.txt, test_12.txt, test_13.txt, test_14.txt, test_15.txt, test_16.txt, test_17.txt, test_18.txt, test_19.txt, test_20.txt, test_21.txt, test_22.txt, test_23.txt, test_24.txt, test_25.txt, test_26.txt, test_27.txt, test_28.txt, test_29.txt, test_30.txt, test_31.txt, test_32.txt, test_33.txt, test_34.txt, test_35.txt, test_36.txt, test_37.txt, test_38.txt |
| Case Name | Status | Exec Time | Memory |
|---|---|---|---|
| hand_01.txt | AC | 114 ms | 96056 KiB |
| hand_02.txt | AC | 21 ms | 26108 KiB |
| hand_03.txt | AC | 114 ms | 95956 KiB |
| hand_04.txt | AC | 21 ms | 26080 KiB |
| sample_01.txt | AC | 2 ms | 7136 KiB |
| test_01.txt | AC | 1 ms | 6688 KiB |
| test_02.txt | AC | 1 ms | 6924 KiB |
| test_03.txt | AC | 1 ms | 7012 KiB |
| test_04.txt | AC | 1 ms | 7100 KiB |
| test_05.txt | AC | 1 ms | 7184 KiB |
| test_06.txt | AC | 2 ms | 8104 KiB |
| test_07.txt | AC | 2 ms | 8152 KiB |
| test_08.txt | AC | 4 ms | 11188 KiB |
| test_09.txt | AC | 5 ms | 11252 KiB |
| test_10.txt | AC | 8 ms | 11648 KiB |
| test_11.txt | AC | 8 ms | 12040 KiB |
| test_12.txt | AC | 41 ms | 12396 KiB |
| test_13.txt | AC | 31 ms | 12408 KiB |
| test_14.txt | AC | 21 ms | 24876 KiB |
| test_15.txt | AC | 63 ms | 58720 KiB |
| test_16.txt | AC | 291 ms | 31408 KiB |
| test_17.txt | AC | 241 ms | 29652 KiB |
| test_18.txt | AC | 291 ms | 31280 KiB |
| test_19.txt | AC | 241 ms | 28876 KiB |
| test_20.txt | AC | 41 ms | 12444 KiB |
| test_21.txt | AC | 472 ms | 35516 KiB |
| test_22.txt | AC | 381 ms | 27188 KiB |
| test_23.txt | AC | 482 ms | 35480 KiB |
| test_24.txt | AC | 312 ms | 32964 KiB |
| test_25.txt | AC | 191 ms | 26384 KiB |
| test_26.txt | AC | 111 ms | 16456 KiB |
| test_27.txt | AC | 162 ms | 30052 KiB |
| test_28.txt | AC | 101 ms | 20820 KiB |
| test_29.txt | AC | 171 ms | 26028 KiB |
| test_30.txt | AC | 141 ms | 29428 KiB |
| test_31.txt | AC | 201 ms | 28664 KiB |
| test_32.txt | AC | 121 ms | 24940 KiB |
| test_33.txt | AC | 312 ms | 35444 KiB |
| test_34.txt | AC | 241 ms | 30488 KiB |
| test_35.txt | AC | 302 ms | 35444 KiB |
| test_36.txt | AC | 201 ms | 29464 KiB |
| test_37.txt | AC | 262 ms | 35468 KiB |
| test_38.txt | AC | 91 ms | 26500 KiB |