Under construction

Still being built. Things may change

Numbers in a Grid

Time limit3sMemory limit1024 MB

Summary
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 SS be the string formed by writing all positive integers in order from 1, with no spaces, from left to right:

12345678910111213141516171819202122232425···

Many problems about SS already exist. In a recent written exam, a problem asked to find the 2×1062 \times 10^6-th digit of SS by hand.

The string SS extends infinitely to the right, but the paper this problem is printed on, or the monitor displaying it, has a fixed width. If SS is printed in a font where every digit has the same width, or read in a suitable editor, every row contains MM digits in order, as shown below.

numbergrid

Suppose the first 109×M10^9 \times M characters of SS are written in order into a 109×M10^9 \times M matrix AA in this way. The figure above shows rows 1 through 7 of AA when M=31M = 31.

Since the data forms a 2D matrix, don't you want to compute a 2D subarray sum? Given a rectangular region [r1,r2]×[c1,c2][r_1, r_2] \times [c_1, c_2] of AA, write a program that computes the sum of the numbers in that region, that is, ∑i=r1r2∑j=c1c2Ai,j\sum_{i=r_1}^{r_2} \sum_{j=c_1}^{c_2} A_{i,j}. Computing it only once feels like a waste, so compute it QQ times.

Input

The first line contains two integers MM (1≤M≤100,0001 \le M \le 100,000) and QQ (1≤Q≤1,0001 \le Q \le 1,000). Each of the next QQ lines contains four integers r1r_1, c1c_1, r2r_2, c2c_2 (1≤r1≤r2≤1091 \le r_1 \le r_2 \le 10^9, 1≤c1≤c2≤M1 \le c_1 \le c_2 \le M).

Output

For each query, print the value of ∑i=r1r2∑j=c1c2Ai,j\sum_{i=r_1}^{r_2} \sum_{j=c_1}^{c_2} A_{i,j} on its own line, in the order the queries are given.

Examples1

  1. Example 1

    Input
    31 3
    1 1 7 31
    4 4 5 5
    5 25 6 30
    
    Expected output
    946
    23
    79