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

#FT0000167. Thêm điểm và hỏi tổng trọng số hình chữ nhật (Point Add Rectangle Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Thêm điểm và hỏi tổng trọng số hình chữ nhật (Point Add Rectangle Sum)

Point Add Rectangle Sum

Source: Library Checker

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

Problem Statement

Initially, there are NN weighted points. Process QQ operations in order:

  • 0 x y w: append a new point at (x,y)(x,y) with weight ww. Even if the coordinate is already occupied, the new point remains a separate instance.
  • 1 l d r u: return the sum of weights of all points currently in [l,r)×[d,u)[l,r)\times[d,u), so l≤x<rl\le x<r and d≤y<ud\le y<u.

Existing weights never change. A query includes the initial points and only the points inserted by earlier operations.

Input

The first line contains N,QN,Q, followed by NN lines x y w for initial points and then QQ operations 0 x y w or 1 l d r u.

Output

For every type-1 operation, print the weight sum in the half-open rectangle on its own line.

Subtask

  • Subtask 1 (20 points): 1≤N≤201\le N\le 20; 1≤Q≤401\le Q\le 40; initial and inserted point coordinates and weights lie in [0,109][0,10^9]; queries have 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; initial and inserted point coordinates and weights lie in [0,109][0,10^9]; queries have 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; initial and inserted point coordinates and weights lie in [0,109][0,10^9]; queries have 0≤l<r≤1090\le l<r\le10^9, 0≤d<u≤1090\le d<u\le10^9.

Examples

Example 1

Input

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

Output

11
1111
111000

Explanation

The first rectangle includes weights 11 and 1010, giving 1111. The second includes all four initial points, giving 11111111. The two insertions add weights 1000010000 at (2,2)(2,2) and 100000100000 at (1,1)(1,1). The final rectangle includes these two new points and the original weight 10001000, totaling 111000111000.

Example 2

Input

1 6
1 1 5
1 1 1 2 2
0 1 1 7
1 1 1 2 2
0 2 2 10
1 1 1 3 3
1 2 2 3 3

Output

5
12
22
10

Explanation

Initially, (1,1)(1,1) contributes 5. Adding another point at the same coordinate increases its total to 5+7=125+7=12 rather than replacing 5. Adding weight 10 at (2,2)(2,2) makes the larger rectangle sum 2222. The final rectangle includes only (2,2)(2,2), so it returns 10.