2D Paper Play
InterviewTime limit3sMemory limit512 MB
For each block, count the sheets with w at least x and h at least y, then answer each query by counting blocks whose count falls in [L, U].
- Level
Medium5 of 10
- Topics
- Sorting, Prefix sum, Implementation
- Solved
- No attempts yet
Problem
Bob plays with colored paper and blocks on a two-dimensional grid plane. Every block is 1x1, and several blocks can be stacked on the same cell. Suppose there are blocks, and let the position of the -th block be . In this problem the top-left cell is (1, 1), and the x axis runs left to right. In the top-left layout of the figure below, block 1 is at (2, 2) and block 3 is at (4, 3).

Bob also has sheets of paper. The -th sheet is cells wide and cells tall. Paper cannot be rotated.
- The top-right layout places one 4 x 4 sheet covering (1, 1) through (4, 4). Blocks 1 and 3 fit entirely on this sheet.
- The bottom-left layout places one 8 x 4 sheet, and it holds 4 blocks.
- The bottom-right layout places one 9 x 3 sheet, and it holds 3 blocks.
Place the -th sheet so that it covers the cells from (1, 1) to . If and , then the -th sheet “includes” the -th block. For each , let be the number of sheets among the sheets that include block , so .
Alice asks questions. The -th question gives two integers and . Find the number of indices that satisfy .
For example, with , , , , , and , the values are , , , , and .
Input
The first line contains the number of test cases .
The first line of each test case contains , , and , separated by spaces. The next lines each contain and . The next lines each contain and . The next lines each contain and .
Output
For each test case, print the answers to the questions on one line, separated by spaces.