Created
January 5, 2015 14:04
-
-
Save ftiasch/77719027b180aba0d533 to your computer and use it in GitHub Desktop.
Min mod
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 Control.Monad | |
import Control.Monad.State.Lazy | |
import Data.ByteString.Lazy.Char8 (ByteString) | |
import qualified Data.ByteString.Lazy.Char8 as BS | |
import Data.Char | |
import Data.List | |
import Data.Maybe | |
data Operation = Update { index :: Int, multiple :: Int } | |
| Query { beginIndex :: Int, endIndex :: Int } | |
deriving (Show, Eq) | |
type Input = ([Int], [Operation]) | |
main :: IO () | |
main = BS.putStrLn . solve . evalState parseInput =<< BS.getContents | |
parseOperation :: String -> Int -> Int -> Maybe Operation | |
parseOperation "U" index multiple = Just $ Update index multiple | |
parseOperation "Q" beginIndex endIndex' = Just $ Query beginIndex (endIndex' + 1) | |
parseInput :: State ByteString Input | |
parseInput = do | |
n <- readInt | |
a <- replicateM n readInt | |
m <- readInt | |
q <- replicateM m readOperation | |
return (a, q) | |
where readInt = state $ fromJust . BS.readInt . BS.dropWhile isSpace | |
readString = state $ BS.span (not . isSpace) . BS.dropWhile isSpace | |
readOperation = do | |
op <- readString | |
fi <- readInt | |
se <- readInt | |
return . fromJust $ parseOperation (BS.unpack op) fi se | |
unify :: [Int] -> [Operation] | |
unify = map fromJust . zipWith (parseOperation "U") [0..] | |
data Tree | |
solve :: Input -> ByteString | |
solve (a, q') = let q = (unify a) ++ q' | |
in BS.unlines . map (BS.pack . show) . reverse . snd $ foldl' step (undefined, []) q | |
step :: (Tree, [Int]) -> Operation -> (Tree, [Int]) | |
step (tree, _) (Update index multiple) = undefined | |
step (tree, results) (Query beginIndex endIndex) = let newResult = undefined | |
in (tree, newResult : results) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment