Counting Rectangles (Codeforces 1722E)
Tags: Implement, data structures

You have $$$n$$$ rectangles, the $$$i$$$-th rectangle has height $$$h_i$$$ and width $$$w_i$$$.You are asked $$$q$$$ queries of the form $$$h_s \ w_s \ h_b \ w_b$$$. For each query output, the total area of rectangles you own that can fit a rectangle of height $$$h_s$$$ and width $$$w_s$$$ while also fitting in a rectangle of height $$$h_b$$$ and width $$$w_b$$$. In other words, print $$$\sum h_i \cdot w_i$$$ for $$$i$$$ such that $$$h_s < h_i < h_b$$$ and $$$w_s < w_i < w_b$$$. Please note, that if two rectangles have the same height or the same width, then they cannot fit inside each other. Also note that you cannot rotate rectangles.Please note that the answer for some test cases won't fit into 32-bit integer type, so you should use at least 64-bit integer type in your programming language (like long long for C++).

Input: The first line of the input contains an integer $$$t$$$ ($$$1 \leq t \leq 100$$$) — the number of test cases.The first line of each test case two integers $$$n, q$$$ ($$$1 \leq n \leq 10^5$$$; $$$1 \leq q \leq 10^5$$$) — the number of rectangles you own and the number of queries.Then $$$n$$$ lines follow, each containing two integers $$$h_i, w_i$$$ ($$$1 \leq h_i, w_i \leq 1000$$$) — the height and width of the $$$i$$$-th rectangle.Then $$$q$$$ lines follow, each containing four integers $$$h_s, w_s, h_b, w_b$$$ ($$$1 \leq h_s < h_b,\ w_s < w_b \leq 1000$$$) — the description of each query.The sum of $$$q$$$ over all test cases does not exceed $$$10^5$$$, and the sum of $$$n$$$ over all test cases does not exceed $$$10^5$$$.

Output: For each test case, output $$$q$$$ lines, the $$$i$$$-th line containing the answer to the $$$i$$$-th query.

Note:   In the first test case, there is only one query. We need to find the sum of areas of all rectangles that can fit a $$$1 \times 1$$$ rectangle inside of it and fit into a $$$3 \times 4$$$ rectangle.Only the $$$2 \times 3$$$ rectangle works, because $$$1 < 2$$$ (comparing heights) and $$$1 < 3$$$ (comparing widths), so the $$$1 \times 1$$$ rectangle fits inside, and $$$2 < 3$$$ (comparing heights) and $$$3 < 4$$$ (comparing widths), so it fits inside the $$$3 \times 4$$$ rectangle. The $$$3 \times 2$$$ rectangle is too tall to fit in a $$$3 \times 4$$$ rectangle. The total area is $$$2 \cdot 3 = 6$$$.

Sample Inputs:
3
2 1
2 3
3 2
1 1 3 4
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
3 1
999 999
999 999
999 998
1 1 1000 1000

Sample Outputs:
6
41
9
0
54
4
2993004

Solution (Python):
def lowbit(n):
    return n & -n

def prefix_sum(tree_list, n):
    _sum = 0
    while n > 0:
        _sum += tree_list[n]
        n -= lowbit(n)
    return _sum

def addwith(tree_list, n, num):
    while n < len(tree_list):
        tree_list[n] += num
        n += lowbit(n)
NMAX = 1001
t = int(input())
for _ in range(t):
    (n, q) = input().split()
    (n, q) = (int(n), int(q))
    recs = [[] for i in range(NMAX)]
    qs_in = [[] for i in range(NMAX)]
    qs_out = [[] for i in range(NMAX)]
    for i in range(n):
        (hi, wi) = input().split()
        (hi, wi) = (int(hi), int(wi))
        recs[hi].append(wi)
    for i in range(q):
        (hs, ws, hb, wb) = input().split()
        (hs, ws, hb, wb) = (int(hs), int(ws), int(hb), int(wb) - 1)
        qs_in[hs].append([ws, wb, i])
        qs_out[hb - 1].append([ws, wb, i])
    _ans = [0 for i in range(q)]
    tree_list = [0 for _ in range(NMAX)]
    for h in range(1, NMAX):
        for wi in recs[h]:
            addwith(tree_list, wi, wi * h)
        for (ws, wb, idx) in qs_in[h]:
            val = prefix_sum(tree_list, wb) - prefix_sum(tree_list, ws)
            _ans[idx] -= val
        for (ws, wb, idx) in qs_out[h]:
            val = prefix_sum(tree_list, wb) - prefix_sum(tree_list, ws)
            _ans[idx] += val
    for ans in _ans:
        print(ans)

Solution (Python):
import sys
input = sys.stdin.readline
I = lambda : int(input())
L = lambda : list(map(int, input().split()))
T = lambda : map(int, input().split())
mod = int(1000000000.0) + 7
t = I()
for _ in range(t):
    (n, q) = T()
    grid = [[0] * 1002 for i in range(1002)]
    for i in range(n):
        (a, b) = T()
        grid[a + 1][b + 1] += a * b
    for i in range(1, 1002):
        for j in range(1, 1002):
            grid[i][j] += grid[i - 1][j] + grid[i][j - 1] - grid[i - 1][j - 1]

    def get(a, b, x, y):
        return grid[x][y] - grid[x][b + 1] - grid[a + 1][y] + grid[a + 1][b + 1]
    for i in range(q):
        (h1, w1, h2, w2) = T()
        print(get(h1, w1, h2, w2))

Solution (Python):
import sys
import copy
from collections import deque
input = sys.stdin.readline
debug = print
print = sys.stdout.write
flush = sys.stdout.flush
T = int(input())
for _ in range(T):
    (N, Q) = map(int, input().split())
    prefix = [[0] * 1001 for _ in range(1001)]
    for _ in range(N):
        (r, c) = map(int, input().split())
        prefix[r][c] += r * c
    for r in range(1, 1001):
        for c in range(1, 1001):
            prefix[r][c] += prefix[r - 1][c] + prefix[r][c - 1] - prefix[r - 1][c - 1]
    for _ in range(Q):
        (sr, sc, br, bc) = map(int, input().split())
        print(f'{prefix[br - 1][bc - 1] - prefix[br - 1][sc] - prefix[sr][bc - 1] + prefix[sr][sc]}\n')