Household Ledger (Easy)
InterviewTime limit1sMemory limit512 MB
Maintain a ledger over N days with point additions and range-sum queries, printing the sum of entries from day p to day q.
- Level
Easy3 of 10
- Topics
- Prefix sum, Array
- Solved
- No attempts yet
Problem
Wolgok writes down every bit of income and spending by date. Managing that money matters a lot to him. He tried a ledger app on his phone, but he has lived so long that the app took forever to give him the number he wanted. Re-entering a record he had forgotten and recomputing was slow too, and Wolgok, who has no patience, smashed the phone. Write the program that helps him.
The program handles two operations. The first adds one income or spending entry on day of Wolgok's life. Income arrives as a positive number and spending as a negative number. The second computes and prints how much the balance changed from day through day . Wolgok may be in debt, so the balance on some day can be negative.
Input
The first line contains the number of days that Wolgok has lived and the number of queries . (, )
Each of the next lines holds a query in one of these two forms.
1 p x: add on day . (, )2 p q: print the total 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.