Counting rectangles
Time: 1 s · Memory: 256 MB
- Prefix sums
Statement
You have rectangles; rectangle has height and width . There are queries, each with numbers . Find the total area of all rectangles with and (strict inequalities). Rectangles cannot be rotated.
Input format
The first line contains and (). The next lines contain and (). The next lines contain , , , (, ).
Output format
For every query print one number on a separate line.
Examples
Input
2 1 2 3 3 2 1 1 3 4
Output
6
Input
5 5 1 1 2 2 3 3 4 4 5 5 3 3 6 6 2 1 4 5 1 1 2 10 1 1 100 100 1 1 3 3
Output
41 9 0 54 4
Note
The sizes are small: build a table where is the total area of rectangles with height and width . With its 2D prefix sums every query is the sum over rows and columns , in .
The inequalities are strict — a rectangle on the border is not counted. The answer reaches — a 64-bit type is needed. In the first example only fits: the answer is .
Source: Codeforces, problem 1722E.
