Problem:
Range Add Query (RAQ)

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

add(s, t, x): add x to as, as+1, ..., at.

get(i): output the value of ai.

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

Input

n q
query1
query2
:
queryq

In the first line, n (the number of elements in A) and q (the number of queries) are given. Then, ith query queryi is given in the following format:

0 s t x

or

1 t

The first digit represents the type of the query. '0' denotes add(s, t, x) and '1' denotes get(i).

Output

For each get operation, print the value.

Constraints

1 ≤ n ≤ 100000

1 ≤ q ≤ 100000

1 ≤ s ≤ t ≤ n

1 ≤ i ≤ n

0 ≤ x ≤ 1000

Sample Input 1

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

Sample Output 1

3
5

Sample Input 2

4 3
1 2
0 1 4 1
1 2

Sample Output 2

0
1

Solution in Haskell (judge verdict: Time Limit Exceeded)

{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE UnboxedTuples #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE ViewPatterns #-}
{-# LANGUAGE TupleSections #-}
{-# LANGUAGE MultiWayIf #-}

import Control.Monad
import Control.Monad.ST
import Control.Monad.State
import Control.Applicative
import Control.Arrow
import Data.List
import Data.Array
import Data.Array.IO
import Data.Maybe
import Data.Bits
import Data.Ord
import Data.Ratio
import Data.Int
import Data.Char
import qualified Data.ByteString.Char8 as BS
import qualified Data.Set as Set
import qualified Data.Vector as V
import qualified Data.Vector.Mutable as VM
import qualified Data.Vector.Unboxed as VU
import qualified Data.Vector.Unboxed.Mutable as VUM
import Data.Tuple
import Data.Function
import Text.Printf
import Debug.Trace
import Numeric

modifyArray arr i f = readArray arr i >>= \x -> writeArray arr i (f x)

listToTuple2 [a,b] = (a,b)
listToTuple3 [a,b,c] = (a,b,c)
listToTuple4 [a,b,c,d] = (a,b,c,d)
listToTuple5 [a,b,c,d,e] = (a,b,c,d,e)

readIntBS = fst . fromJust . BS.readInt

for = flip map

infixr 1 ?
(?) :: Bool -> (a,a) -> a
(?) b t = (if b then fst else snd) t

main :: IO ()
main = do
  [n,q] <- map read . words <$> getLine :: IO [Int]
  queries <- replicateM q $ map read . words <$> getLine :: IO [[Int]]
  let inf = 2^31-1


  seg <- stToIO $ newLazySegmentTree n 0 :: IO (IOLazySegmentTree Int)

  forM_ queries $ \query -> do
    case query of
      [0,s,t,x] -> stToIO $ modifyLazySegmentTree seg (s-1) t 0 (+x) (+)
      [1,i] -> print =<< (stToIO $ findLazySegmentTree seg (i-1) 0 (+))
    
  --mapM_ print =<< stToIO (showLazySegmentTree seg 4)
  
  
type IOLazySegmentTree a = STLazySegmentTree RealWorld a
type STLazySegmentTree s a = VM.MVector s a

newLazySegmentTree :: Num a => Int -> a -> ST s (STLazySegmentTree s a)
newLazySegmentTree n x = V.thaw $ V.replicate m x
  where m = 2 * (2 ^ (ceiling $ logBase 2 (fromIntegral n))) - 1


modifyLazySegmentTree :: Num a => STLazySegmentTree s a -> Int -> Int -> a -> (a -> a) -> (a -> a -> a) -> ST s ()
modifyLazySegmentTree tree x y e g f = do
  aux 0 0 n
  where
    n = (VM.length tree) `div` 2 + 1
    aux i l r
      | r <= x || y <= l = return ()
      | x <= l && r <= y = propag i >> VM.read tree i >>= \a -> VM.write tree i (g a)
      | otherwise = aux (i*2+1) l ((l+r)`div`2) >> aux (i*2+2) ((l+r)`div`2) r

    propag i = do
      a <- VM.read tree i
      when (i<n-1) $ do
        VM.read tree (i*2+1) >>= \b -> VM.write tree (i*2+1) (f a b)
        VM.read tree (i*2+2) >>= \b -> VM.write tree (i*2+2) (f a b)
        VM.write tree i e


findLazySegmentTree :: Num a => STLazySegmentTree s a -> Int -> a -> (a -> a -> a) -> ST s a
findLazySegmentTree tree i e f = do
  aux ((j-1)`div`2)
  VM.read tree j
  where
    n = (VM.length tree + 1) `div` 2
    j = i + n - 1
    aux x = do
      when (x >= 0) $ do
        aux ((x-1)`div`2)
        propag x

    propag i = do
      a <- VM.read tree i
      when (i < n-1) $ do
        VM.read tree (i*2+1) >>= \b -> VM.write tree (i*2+1) (f a b)
        VM.read tree (i*2+2) >>= \b -> VM.write tree (i*2+2) (f a b)
        VM.write tree i e




showLazySegmentTree :: Show a => STLazySegmentTree s a -> Int -> ST s [String]
showLazySegmentTree tree len = do
  let
    n = (VM.length tree) `div` 2 + 1
    xs = init $ scanl (+) 0 (takeWhile (<=n) (iterate (*2) 1))
    ys = reverse xs

  forM (zip xs ys) $ \(s,s') -> do
    concat <$> (forM [s..s*2] $ \i -> (\str -> "[" ++ (take' (len * (n `div` (s+1)) + s'*2) str) ++ "]") . show <$> VM.read tree i)

  where
    take' n xs = if length t <= n then t ++ (replicate (n - length t) ' ') else t
      where t = take n xs