Problem:
Range Sum Query

Write a program which manipulates a sequence A = {a1, a2, . . . , an} with the following operations:

add(i, x): add x to ai.

getSum(s, t): print the sum of as, as+1,...,at.

Note that the initial values of ai (i = 1, 2, . . . , n) are 0.

Input

n q
com1 x1 y1
com2 x2 y2
...
comq xq yq

In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, q queries are given where com represents the type of queries. '0' denotes add(xi, yi) and '1' denotes getSum(xi, yi).

Output

For each getSum operation, print the sum in a line.

Constraints

1 ≤ n ≤ 100000

1 ≤ q ≤ 100000

If comi is 0, then 1 ≤ xi ≤ n, 0 ≤ yi ≤ 1000.

If comi is 1, then 1 ≤ xi ≤ n, 1 ≤ yi ≤ n.

Sample Input 1

3 5
0 1 1
0 2 2
0 3 3
1 1 2
1 2 2

Sample Output 1

3
2

Solution in Haskell (judge verdict: Accepted)

{-# OPTIONS_GHC -O2 -funbox-strict-fields #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE UnboxedTuples #-}
module Main (main) where
import Data.Int (Int32)
import Control.Monad
import Control.Applicative
import Data.Monoid
import Data.Maybe (fromJust)
import qualified Data.ByteString.Char8 as B8
getInts :: IO [Int]
getInts =  map (fst . fromJust . B8.readInt) . B8.words <$> B8.getLine

main :: IO ()
main = do
  n:q:_ <- getInts
  let bst = fromList $ replicate n mempty :: STree (Sum Int)
  loop bst q
  return ()
  where
    loop _ 0 = return ()
    loop !tree !cnt = do
      q:x:y:_ <- getInts
      if q == 0
        then do
        let tree' = add x (fromIntegral y) tree
        loop tree' (cnt-1)
        else do
        print . getSum $ query (x,y) tree
        loop tree (cnt-1)

---------------------------------------------
--- Monoid instance for minimum/maximum query
newtype Min = Min {fromMin :: Int32 } deriving (Ord,Show,Eq,Read)
instance Monoid Min where
  mempty = Min maxBound
  {-# INLINE mempty #-}
  mappend = min
  {-# INLINE mappend #-}

newtype Max = Max Int deriving (Ord,Show,Eq)
instance Monoid Max where
  mempty = Max minBound
  {-# INLINE mempty #-}
  mappend = max
  {-# INLINE mappend #-}

---------------------
--- Segment Tree
data STree v = Leaf {-# UNPACK #-}!Int !v
             | Branch {-# UNPACK #-}!(Int,Int) !v !(STree v) !(STree v)
fromList :: Monoid v => [v] -> STree v
fromList !xs = makeTree 1 (length xs) xs
{-# INLINE fromList #-}

makeTree :: Monoid v => Int -> Int -> [v] -> STree v
makeTree !l !r !es = loop $ map (uncurry f) (zip [l..r] es)
  where
    loop ![x] = x
    loop !xs = loop $ buildTree xs
    f :: Int -> v -> STree v
    f !ix !v = Leaf ix v
    buildTree !(a:b:ys) = let v = val a `mappend` val b
                          in Branch (left a,right b) v a b : buildTree ys
    buildTree !x = x

range :: STree v-> (Int,Int)
range !(Leaf r _) = (r,r)
range !(Branch r _ _ _) = r
{-# INLINE range #-}

val :: STree v -> v
val !(Leaf _ v) = v
val !(Branch _ v _ _) = v
{-# INLINE val #-}

left,right :: STree v -> Int
left (Main.range -> (l,_))  = l
right (Main.range -> (_,r)) = r
{-#INLINE left  #-}
{-#INLINE right #-}

query :: Monoid v =>  (Int, Int) -> STree v -> v
query (ls,rs) t = loop t
  where
    loop (Leaf ix v)
      = if ls <= ix && ix <= rs then v else mempty
    loop (Branch (leftist,rightist) v lt rt)
      | rightist < ls || rs < leftist = mempty
      | ls <= leftist && rightist <= rs = v
      | otherwise = loop lt `mappend` loop rt

add :: Monoid v => Int -> v -> STree v -> STree v
add !ix !v !tree = loop tree
  where loop t = case t of
          Leaf i v0 -> if ix == i
                      then Leaf i (v `mappend` v0)
                      else tree
          Branch rng _ l@(Main.range -> (ll,lr)) r
            | ll <= ix && ix <= lr
              -> let l' = loop l
                 in Branch rng (val l' `mappend` val r) l' r
            | otherwise
              -> let r' = loop r
                 in Branch rng (val l `mappend` val r') l r'
{-# SPECIALIZE INLINE add :: Int -> Sum Int -> STree (Sum Int) -> STree (Sum Int) #-}
update :: Monoid v => (v -> v) -> Int -> STree v -> STree v
update f !ix !tree = loop tree
  where loop t = case t of
          Leaf i v -> if ix == i
                      then Leaf i (f v)
                      else tree
          Branch rng _ l@(Main.range -> (ll,lr)) r
            | ll <= ix && ix <= lr
              -> let l' = loop l
                 in Branch rng (val l' `mappend` val r) l' r
            | otherwise
              -> let r' = loop r
                 in Branch rng (val l `mappend` val r') l r'


insert :: Monoid v => Int -> v -> STree v -> STree v
insert !ix !v !tree = loop tree
  where loop t = case t of
          Leaf i _ -> if ix == i
                      then Leaf i v
                      else tree
          Branch rng _ l@(Main.range -> (ll,lr)) r
            | ll <= ix && ix <= lr
              -> let l' = loop l
                 in Branch rng (val l' `mappend` val r) l' r
            | otherwise
              -> let r' = loop r
                 in Branch rng (val l `mappend` val r') l r'