Log inSign up
Problems / B117

Change in power output

200 ptsCases 15Set by dongbinna
Added October 30.
1 solved1 attempted1 submissionssolvers took 1.0 tries on average

Statement

The panels of a solar power plant are laid out as an N × M grid. Each section, from row 1 column 1 to row N column M, has an integer written on it giving how much this month's output changed from last month. A positive value means it rose by that much, and a negative value means it fell by that much.

The plant manager wants to take out part of the grid and add up the changes over that area. There are K queries, and each query is made of four numbers i, j, x and y. The query means adding up the values of every section inside the rectangular area whose top-left corner is row i column j and whose bottom-right corner is row x column y.

Write a program that takes the changes written on the grid and the K queries, and computes the result of each query in order.

For example, suppose the values on a grid with N=2 and M=3 are given by the table below.

RowColumn 1Column 2Column 3
Row 1124
Row 281632

Then assume the K=3 queries are [1, 1, 2, 3], [1, 2, 1, 2] and [1, 3, 2, 3] in that order. The result of each query is given by the table below.

Query123
Top-left cornerRow 1 column 1Row 1 column 2Row 1 column 3
Bottom-right cornerRow 2 column 3Row 1 column 2Row 2 column 3
Values added1 and 2 and 4 and 8 and 16 and 3224 and 32
Result63236

There are up to 10,000 queries, so adding up the values inside the rectangle one by one for every query runs out of time. Writing down in advance, for every cell, the total of the rectangle whose top-left corner is row 1 column 1 turns one query into a few additions and subtractions. The table below holds those values for the example above.

RowColumn 1Column 2Column 3
Row 1137
Row 292763

So the answer for this case is 63, 2 and 36 in order.

Input and Output Examples

Example 1

Input

2 3
1 2 4
8 16 32
3
1 1 2 3
1 2 1 2
1 3 2 3

Output

63
2
36

Example 2

Input

2 2
1349 970
2754 -519
2
1 2 1 2
2 2 2 2

Output

970
-519

Example 3

Input

1 1
6042
1
1 1 1 1

Output

6042

Input

  • The first line holds the number of rows N and the number of columns M of the grid, separated by a space. N and M are each a natural number from 1 to 300.

  • The next N lines hold the values written on the grid. Each line holds M numbers separated by spaces, and each value is an integer whose absolute value is at most 10,000.

  • The next line holds the number of queries K. K is a natural number from 1 to 10,000.

  • The next K lines hold the queries. Each line holds four numbers i, j, x and y, separated by spaces. i is a natural number from 1 to N and x is a natural number from i to N. j is a natural number from 1 to M and y is a natural number from j to M.

  • The whole input is at most about 0.6MB. There are up to 10,302 lines, so read the whole input in one go and split it into lines.

Output

  • Print the K query results in order, one per line.

Requirements

  • A query is always given with i less than or equal to x, and j less than or equal to y
  • If the top-left corner and the bottom-right corner are the same cell, add the value of that one cell only
  • A value on the grid can be negative, and the total over an area can be negative too
  • There are up to 10,000 queries, so adding up the rectangle again for every query 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.
Read the requirements carefully before solving. Tampering with the grading to make a submission look like a pass can get you sanctioned if caught. The code you submit is published on the leaderboard along with the result.

Examples

3 public cases. Judging also uses hidden cases. You have to pass all 15 cases to solve this.

Input
Expected output
1 1
6042
1
1 1 1 1
6042
2 2
1349 970
2754 -519
2
1 2 1 2
2 2 2 2
970
-519
2 3
1 2 4
8 16 32
3
1 1 2 3
1 2 1 2
1 3 2 3
63
2
36

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.

0 chars1 lineMemory 256MB (recorded only)Reference 42ms

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

The time shown is the time spent solving. Input reading time is outside the solver's control, so it is excluded. And since each device reports this value itself, we record which device measured it.

1bampal2248ms570 charsPythonWindows NT 10.0 / Chrome/156.0.0.0 / 8코어 / 16GB

Log in and you can see the code once you solve this problem.