Stars Falling from the Sky: 1, 2, ..., R-L+1 of Them
InterviewTime limit1sMemory limit512 MB
Maintain an array where a range update adds 1,2,...,R-L+1 to positions L..R, and point queries ask the current total at one index.
- Level
Medium5 of 10
- Topics
- Prefix sum, Array, Math, Implementation
- Solved
- No attempts yet
Problem
One of Ukje's secret hobbies is gazing at the night sky every night. 😓 Ukje observed that the stars in the sky fall according to the following rules.
- Stars fall at N points. The points are numbered 1, 2, ..., N in order.
- Each night the stars fall on a contiguous subinterval [L, R] of 1, 2, ..., N.
- When stars fall on [L, R], the points receive 1, 2, ..., R-L+1 stars in order. In other words, L gets 1 star, L+1 gets 2 stars, ..., and R gets R-L+1 stars.
Ukje fell asleep while recording the stars falling from the sky!! You had hoped otherwise, but as expected, you must perform the following queries in Ukje's place. (ㅎㅎ;; ㅈㅅ.. ㅋㅋ!!)
- 1 L R: Stars fall on [L, R]. (1 ≤ L ≤ R ≤ N)
- 2 X: Print the total number of stars that have fallen on point X. (1 ≤ X ≤ N)
Input
The first line gives N, the number of points where stars fall. (1 ≤ N ≤ 105)
The second line gives A1, ..., AN, the counts of stars that have already fallen as counted by Ukje before he fell asleep, separated by spaces. (0 ≤ A1, ..., AN ≤ 106)
The third line gives Q, the number of queries. (1 ≤ Q ≤ 105)
Each of the next Q lines contains one query.
Output
Print the answer to each type 2 query on its own line.