Last active
August 29, 2015 14:06
-
-
Save berdario/6c33b11a137386af62e1 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Data.Functor.Identity (runIdentity) | |
import Control.Monad (replicateM) | |
import Control.Monad.State.Lazy (state, evalState) | |
getInts :: IO [Int] | |
getInts = fmap (map read . words) getLine | |
f = do | |
n:q:_ <- getInts | |
els <- replicateM n getInts | |
vals <- getInts | |
queries <- replicateM q getInts | |
print (els, vals, queries) | |
readInts :: String -> [Int] | |
readInts = map read . words | |
readIntsLines (x, y) = (map readInts x, y) | |
f2 s = show (els, vals, queries) | |
where | |
(n:q:_, s') = (readInts $ head s, tail s) | |
(els, s'') = readIntsLines $ splitAt n s' | |
(vals, s''' ) = (readInts $ head s'', tail s'') | |
(queries, _) = readIntsLines $ splitAt q s''' | |
f3 s = runIdentity $ do | |
(n:q:_, s) <- return $ (readInts $ head s, tail s) | |
(els, s) <- return $ (map readInts $ take n s, drop n s) | |
(vals, s) <- return $ (readInts $ head s, tail s) | |
queries <- return $ map readInts $ take q s | |
return $ show (els, vals, queries) | |
readLines n = fmap (map readInts) $ state (splitAt n) | |
f4 s = flip evalState s $ do | |
(n:q:_):_ <- readLines 1 | |
els <- readLines n | |
vals:_ <- readLines 1 | |
queries <- readLines q | |
return $ show (els, vals, queries) | |
f'= interact (f4 . lines) | |
main = f | |
-- expected output with dat as input: | |
-- ([[1,2,3],[4,5,6]],[7,8,9],[[10,11,12],[13,14,15],[16,17,18]]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 3 | |
1 2 3 | |
4 5 6 | |
7 8 9 | |
10 11 12 | |
13 14 15 | |
16 17 18 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment