Skip to content

Instantly share code, notes, and snippets.

@dfyz
Created January 24, 2013 22:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dfyz/4628909 to your computer and use it in GitHub Desktop.
Save dfyz/4628909 to your computer and use it in GitHub Desktop.
import Control.Applicative
import Control.Monad
import Data.Array
readInt :: IO Int
readInt = readLn
readIntPair :: IO [Int]
readIntPair = parseLine <$> getLine
where
parseLine = (map read) . words
getAnswer prefixSums (from:to:_) = (prefixSums ! to) - (prefixSums ! (from - 1))
main = do
n <- readInt
numbers <- replicateM n readInt
let prefixSums = listArray (0, n) (scanl (+) 0 numbers)
m <- readInt
queries <- replicateM m readIntPair
mapM_ (print . getAnswer prefixSums) queries
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment