BUZZ95 - Submatrix Sum of a Sparse Matrix

no tags 

You are given a sparse matrix of dimensions N x M. There K cells in the matrix {(x1, y1), (x2, y2) ... (xK, yK)} with non-zero values {v1, v2 ... vK}. All the other cells except these K cells contain value = 0. You are asked Q queries of the form sx sy fx fy, you need to print the sum of submatrix bounded by (sx, sy) and (fx, fy).

Input

First line contains two space separated integers N, M. (1 <= N, M <= 10^9)

Second line contains the integer K (1 <= K <= 10^5)

Next K lines contain three space separated integers xi, yi, vi. (1 <= xi <= N, 1 <= yi <= M, 1 <= vi <= 10^9).

Next line contains Q. (1 <= Q <= 10^5)

Next Q lines contain four space separated integers sx, sy, fx, fy. (1 <= sx <= fx <= N, 1 <= sy <= fy <= M).

Output

For each query, print a single integer representing the sum of submatrix.

Example

Input:
10 10 
2
1 1 5
2 2 15
1
1 1 3 3

Output:
20

hide comments
Francky: 2020-05-21 15:08:28

Back to classical, the problem was commented as broken and to be rewritten before 5th April. Warning send to admin 5th April. Maybe some changes after that ; comments were deleted and maybe description updated and/or IO fixed. There were AC the 6th. Admins hide the problem few days after that, although the problems fixed. Now the problem is back.
If you have any memories of the comments (or description) before the 6th April...
psolvers who tried before 6th, you could try again !


Added by:buzz_95
Date:2020-03-31
Time limit:1s
Source limit:50000B
Memory limit:1536MB
Cluster: Cube (Intel G860)
Languages:All