{-# LANGUAGE OverloadedStrings #-} import Control.Monad (replicateM) import Data.Text (Text) import qualified Data.Text as T import qualified Data.Text.IO as T import Data.Text.Read (Reader) import qualified Data.Text.Read as T main :: IO () main = do [rr, _] <- f as <- f q <- g ans <- replicateM q $ do [l, r] <- f return $ solve rr as l r mapM_ print ans where f = map (readtx T.decimal) <$> T.words <$> T.getLine g = readtx T.decimal <$> T.getLine solve :: Int -> [Int] -> Int -> Int -> Int solve rr as l r = sum $ map (\a -> f r a - f (l - 1) a) as where f x a | x < a = 0 | otherwise = (x - a) `div` rr + 1 readtx :: Num a => Reader a -> Text -> a readtx f = either (const 0) fst . f