Profit and loss by period
Statement
A shop wrote down, in date order, its profit and loss over the N days from the day it opened. For each day from day 1 to day N, the amount it cleared that day is written as an integer. A positive value means it cleared that much, and a negative value means it lost that much.
The accountant reads this log and runs M settlements. Each settlement is made of two dates i and j, and it means adding up the profit and loss from day i to day j. If i and j are the same, the settlement looks at that one day only.
Write a program that takes the profit and loss log and the M settlements, and computes the result of each settlement in order.
For example, suppose the profit and loss over N=5 days is given by the table below.
| Day | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Profit and loss | 5 | 4 | 3 | 2 | 1 |
Then assume the M=3 settlements are [1, 3], [2, 4] and [5, 5] in that order. The result of each settlement is given by the table below.
| Settlement | 1 | 2 | 3 |
|---|---|---|---|
| Start date | 1 | 2 | 5 |
| End date | 3 | 4 | 5 |
| Values added | 5 and 4 and 3 | 4 and 3 and 2 | 1 |
| Result | 12 | 9 | 1 |
There are up to 100,000 settlements and up to 100,000 recorded days, so adding up a period from scratch for every settlement runs out of time. Adding up the profit and loss from day 1 to each day in advance turns one settlement into one subtraction. The table below holds those running totals for the example above.
| Day | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|
| Total from day 1 | 5 | 9 | 12 | 14 | 15 |
The profit and loss from day 2 to day 4 comes out at once as 9, by taking the total through day 4, which is 14, and subtracting the total through day 1, which is 5. So the answer for this case is 12, 9 and 1 in order.
Input and Output Examples
Example 1
Input
5 5 4 3 2 1 3 1 3 2 4 5 5
Output
12 9 1
Example 2
Input
2 -558 399 2 1 1 1 2
Output
-558 -159
Example 3
Input
1 -38 1 1 1
Output
-38
Input
The first line holds the number of recorded days N. N is a natural number from 1 to 100,000.
The second line holds the profit and loss from day 1 to day N, separated by spaces, all on one line. Each day's profit and loss is an integer whose absolute value is at most 1,000.
The third line holds the number of settlements M. M is a natural number from 1 to 100,000.
The next M lines hold the settlements. Each line holds a start date i and an end date j, separated by a space. i is a natural number from 1 to N, and j is a natural number from i to N.
The whole input is at most about 1.6MB. There are up to 100,003 lines, so read the whole input in one go and split it into lines.
Output
Print the M settlement results in order, one per line. Attach one newline after each settlement result, join them into a single string, and print that string in one go.
Requirements
- A day's profit and loss can be negative, and a period's total can be negative too
- A settlement is always given with i less than or equal to j
- A settlement whose i and j are the same looks at that one day only
- There are up to 100,000 settlements, so adding up a period again for every settlement runs out of time
- You must write a program that reads from standard input and writes to standard output
- Trailing spaces at the end of a line and blank lines at the end of the output are ignored when grading
- The languages you can use are Python and JavaScript
- The memory limit is 256MB. The browser does not measure it, it only states it
- The time limit is set by measuring the setter's reference solution on the same runtime
- On this problem you submit code, not a prompt. That code runs in the browser on this device, and all that goes to the server is a fingerprint of the output and the time it took. Whether it is correct is decided by the server. The run time is a number each device reports, so the code and the device details are posted next to it on the leaderboard.
- The output has to match the expected value down to the character. A space inside a line counts the same as any other character.
- There are 15 cases, and 3 of them show the input together with the expected output. The rest are used only when you submit and are not shown on this page.
- You have to pass all 15 cases to solve this.
Examples
3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.
1 -38 1 1 1
-38
2 -558 399 2 1 1 1 2
-558 -159
5 5 4 3 2 1 3 1 3 2 4 5 5
12 9 1
Solution
Your code runs on this device. Only the fingerprint of the output and the elapsed time go to the server. The server decides whether it is correct.
Submitting requires you to log in. Running against the public cases works without one.
You can test with 3 public cases. Submitting runs the hidden cases too.
People who solved this problem
Nobody yet. You could be the first.