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

#FT0000170. Cộng hình chữ nhật và truy vấn tổng (Rectangle Add Rectangle Sum)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Cộng hình chữ nhật và truy vấn tổng (Rectangle Add Rectangle Sum)

Rectangle Add Rectangle Sum

Source: Luogu

Version: Phước Hưng OJ Extended

Problem Statement

Initially, an N×MN\times M matrix contains zeros. Rows and columns are 1-indexed. Process QQ operations in order:

  • L a b c d delta: add deltadelta to every cell with a≤i≤ca\le i\le c and b≤j≤db\le j\le d. Previous values remain and are incremented.
  • k a b c d: print the sum of the cells in the inclusive rectangle from (a,b)(a,b) to (c,d)(c,d). The command letter k is lowercase.

Updates may overlap, and every query reflects all updates performed earlier.

Input

The first line contains N,M,QN,M,Q, followed by exactly QQ operations, either L a b c d delta or k a b c d.

Output

For each k command, print the sum of the requested rectangle after all preceding updates.

Subtask

  • Subtask 1 (10 points): 1≤N≤161\le N\le 16; 1≤M≤161\le M\le 16; 1≤Q≤2001\le Q\le 200; 1≤a≤c≤N1\le a\le c\le N, 1≤b≤d≤M1\le b\le d\le M; L has −500≤delta≤500-500\le delta\le500; each queried sum fits in a signed 32-bit integer.
  • Subtask 2 (50 points): 1≤N≤5121\le N\le 512; 1≤M≤5121\le M\le 512; 1≤Q≤2000001\le Q\le 200000; 1≤a≤c≤N1\le a\le c\le N, 1≤b≤d≤M1\le b\le d\le M; L has −500≤delta≤500-500\le delta\le500; each queried sum fits in a signed 32-bit integer.
  • Subtask 3 (40 points): 1≤N≤20481\le N\le 2048; 1≤M≤20481\le M\le 2048; 1≤Q≤2000001\le Q\le 200000; 1≤a≤c≤N1\le a\le c\le N, 1≤b≤d≤M1\le b\le d\le M; L has −500≤delta≤500-500\le delta\le500; each queried sum fits in a signed 32-bit integer.

Examples

Example 1

Input

3 3 6
k 1 1 3 3
L 1 1 2 2 5
k 1 1 3 3
L 2 2 3 3 -2
k 2 2 3 3
k 1 1 2 2

Output

0
20
-3
18

Explanation

Initially all cells are zero. Adding 5 to a 2×22\times2 square increases the whole-grid sum by 4⋅5=204\cdot5=20. The next operation adds −2-2 to the bottom-right 2×22\times2 square; only (2,2)(2,2) previously held 5, so its queried sum becomes 5−8=−35-8=-3. The upper-left square originally totaled 20, and only its cell (2,2)(2,2) decreases by 2, yielding 18.

Example 2

Input

1 2 6
L 1 1 1 2 3
k 1 1 1 2
L 1 2 1 2 -5
k 1 1 1 2
k 1 2 1 2
k 1 1 1 1

Output

6
1
-2
3

Explanation

After the first update the single row is (3,3)(3,3), totaling 6. The second update changes its second cell to 3−5=−23-5=-2, leaving the first at 3. Thus the row sum is 1, and the final individual-cell queries return −2-2 and 3.