PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000166. Tổng trọng số các điểm trong hình chữ nhật (Rectangle Sum)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Tổng trọng số các điểm trong hình chữ nhật (Rectangle Sum)

Rectangle Sum

Source: Library Checker

Version: Phước Hưng OJ Extended.

Problem Statement

There are NN weighted points at integer coordinates (xi,yi)(x_i,y_i), each with weight wiw_i. Several points may occupy the same coordinate; they are separate instances and all their weights contribute. Each of QQ queries specifies the half-open rectangle [l,r)×[d,u)[l,r)\times[d,u). Output the sum of weights of precisely those points satisfying l≤xi<rl\le x_i<r and d≤yi<ud\le y_i<u. The left and lower boundaries are included, while the right and upper boundaries are excluded. Queries do not modify the points.

Input

The first line contains N,QN,Q. Each of the next NN lines gives x y w. Each of the next QQ lines gives one half-open rectangle l d r u.

Output

For each query, print the sum of the included weights as a 64-bit integer.

Subtask

  • Subtask 1 (20 points): 1≤N≤201\le N\le 20; 1≤Q≤401\le Q\le 40; 0≤x,y,w≤1090\le x,y,w\le10^9; each query has 0≤l<r≤1090\le l<r\le10^9, 0≤d<u≤1090\le d<u\le10^9.
  • Subtask 2 (30 points): 1≤N≤20001\le N\le 2000; 1≤Q≤30001\le Q\le 3000; 0≤x,y,w≤1090\le x,y,w\le10^9; each query has 0≤l<r≤1090\le l<r\le10^9, 0≤d<u≤1090\le d<u\le10^9.
  • Subtask 3 (50 points): 1≤N≤2000001\le N\le 200000; 1≤Q≤2000001\le Q\le 200000; 0≤x,y,w≤1090\le x,y,w\le10^9; each query has 0≤l<r≤1090\le l<r\le10^9, 0≤d<u≤1090\le d<u\le10^9.

Examples

Example 1

Input

5 5
0 0 1
3 0 10
1 4 100
2 2 1000
3 2 10000
0 0 4 5
0 0 4 4
3 0 4 3
2 0 4 3
1 2 2 4

Output

11111
11011
10010
11010
0

Explanation

The five points have weights 1,10,100,1000,100001,10,100,1000,10000. Query 1 includes all five, totaling 1111111111. Query 2 excludes (1,4)(1,4) because the top edge is open, giving 1101111011. Query 3 includes the two points with x=3x=3, totaling 10+10000=1001010+10000=10010. Query 4 includes weights 1000+10+10000=110101000+10+10000=11010. Query 5 contains no point, giving 0.

Example 2

Input

4 4
1 1 3
1 1 4
3 0 10
4 4 5
1 1 2 2
1 1 4 5
0 0 5 5
3 0 4 1

Output

7
7
22
10

Explanation

The two distinct points at (1,1)(1,1) contribute 3+4=73+4=7. The second rectangle again contains only these two points because (3,0)(3,0) lies below its lower bound. The third contains all four points with total 2222. The final rectangle contains just (3,0)(3,0), yielding 1010.