Household Ledger
InterviewTime limit1sMemory limit512 MB
Process point additions and range-sum queries on a ledger of N days.
- Level
Medium4 of 10
- Topics
- Prefix sum
- Solved
- No attempts yet
Problem
Wolgok keeps a ledger of income and spending by day, and wants to check how much the balance moved over a chosen period of days. The ledger application on the phone recomputed everything from scratch on every new entry, so it was too slow, and filling in an entry that had been missed earlier was just as slow. Write a program that handles the two operations below quickly.
The first operation adds an amount to the entry for day of Wolgok's life. Income is given as a positive number and spending as a negative number. The same day can be added to more than once, and the amounts accumulate.
The second operation reports the sum of the amounts recorded from day through day , which is the net change of the balance over that period. Wolgok can be in debt, so this value can be negative.
At the start no day has any record, so the amount on every day is .
Input
The first line contains the number of days that Wolgok has lived and the number of queries . (, )
Each of the next lines contains a query in one of the two forms below.
1 p x: add to the entry for day . (, )2 p q: print the net change from day through day . ()
Output
For each 2 query, print the computed value on its own line, in the order the queries are given.