Numbers in a Grid
Time limit3sMemory limit1024 MB
Digits of 1, 2, 3, ... concatenated are written row by row into a grid M wide, and each query asks for the digit sum over a rectangle.
- Level
Hard9 of 10
- Topics
- Matrix, Prefix sum, Math, Implementation
- Solved
- No attempts yet
Problem
Let be the string formed by writing all positive integers in order from 1, with no spaces, from left to right:
12345678910111213141516171819202122232425···
Many problems about already exist. In a recent written exam, a problem asked to find the -th digit of by hand.
The string extends infinitely to the right, but the paper this problem is printed on, or the monitor displaying it, has a fixed width. If is printed in a font where every digit has the same width, or read in a suitable editor, every row contains digits in order, as shown below.

Suppose the first characters of are written in order into a matrix in this way. The figure above shows rows 1 through 7 of when .
Since the data forms a 2D matrix, don't you want to compute a 2D subarray sum? Given a rectangular region of , write a program that computes the sum of the numbers in that region, that is, . Computing it only once feels like a waste, so compute it times.
Input
The first line contains two integers () and (). Each of the next lines contains four integers , , , (, ).
Output
For each query, print the value of on its own line, in the order the queries are given.