Loading...
Loading...
An online store records its sales (in rupees) for n days. The manager then asks q questions. Each question gives two days l and r and asks for the total sales from day l to day r, both days included.
Print the answer to every question.
Example
Input
5 3 4 2 7 1 5 1 3 2 5 4 4
Output
13 15 1
Days 1–3: 4 + 2 + 7 = 13. Days 2–5: 2 + 7 + 1 + 5 = 15. Day 4 alone: 1.
Try the problem first. Open one hint at a time.
Adding the days again for every question costs up to n steps per question, which is 10^10 steps in the worst case.
Before the questions, build an array where position i holds the total of days 1 to i.
The total of days l to r is the total up to r minus the total up to l − 1.
Make the array one longer than n and keep position 0 as 0, so l = 1 needs no special case. Use long.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor