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

#FT0000171. Tổng trong khối lập phương (Cube Summation)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng trong khối lập phương (Cube Summation)

Cube Summation

Source: HackerRank

Version: Phước Hưng OJ Extended

Problem Statement

An N×N×NN\times N\times N cube consists of cells indexed from 1 to NN on each axis. All cells initially contain 0. Process QQ operations:

  • UPDATE x y z w: assign ww to cell (x,y,z)(x,y,z), replacing its previous value, even if the cell has been updated before.
  • QUERY x1 y1 z1 x2 y2 z2: find the sum over the inclusive box satisfying x1≤x≤x2x1\le x\le x2, y1≤y≤y2y1\le y\le y2, and z1≤z≤z2z1\le z\le z2.

Operations take effect in the order they are given.

Input

The first line contains N,QN,Q. The next QQ lines contain either UPDATE x y z w or QUERY x1 y1 z1 x2 y2 z2.

Output

For each QUERY, print the box sum as a signed 64-bit integer on its own line.

Subtask

  • Subtask 1 (20 points): 1≤N≤61\le N\le 6; 1≤Q≤301\le Q\le 30; all coordinates lie in [1,N][1,N]; −109≤w≤109-10^9\le w\le10^9; every query box has lower bounds no greater than upper bounds along all three axes.
  • Subtask 2 (30 points): 1≤N≤301\le N\le 30; 1≤Q≤3001\le Q\le 300; all coordinates lie in [1,N][1,N]; −109≤w≤109-10^9\le w\le10^9; every query box has lower bounds no greater than upper bounds along all three axes.
  • Subtask 3 (50 points): 1≤N≤1001\le N\le 100; 1≤Q≤10001\le Q\le 1000; all coordinates lie in [1,N][1,N]; −109≤w≤109-10^9\le w\le10^9; every query box has lower bounds no greater than upper bounds along all three axes.

Examples

Example 1

Input

4 5
UPDATE 2 2 2 4
QUERY 1 1 1 3 3 3
UPDATE 1 1 1 23
QUERY 2 2 2 4 4 4
QUERY 1 1 1 3 3 3

Output

4
4
27

Explanation

Initially only (2,2,2)(2,2,2) has value 4, so the first box sum is 4. Assigning 23 to (1,1,1)(1,1,1) does not affect the second box, whose lower corner is (2,2,2)(2,2,2), so its answer stays 4. The final box contains both nonzero cells and totals 4+23=274+23=27.

Example 2

Input

2 5
UPDATE 2 2 2 1
QUERY 1 1 1 2 2 2
UPDATE 2 2 2 8
QUERY 2 2 2 2 2 2
QUERY 1 1 1 1 1 1

Output

1
8
0

Explanation

The first assignment sets (2,2,2)(2,2,2) to 1. The second assignment replaces 1 by 8 rather than adding 8. Therefore, the single-cell query returns 8. Cell (1,1,1)(1,1,1) was never modified and remains 0.