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

#FT0000160. Tổng hình chữ nhật với ma trận thay đổi (Range Sum Query 2D – Mutable)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Tổng hình chữ nhật với ma trận thay đổi (Range Sum Query 2D – Mutable)

Range Sum Query 2D – Mutable

Source: LeetCode

Version: Phuoc Hung OJ – class methods converted to SET/SUM commands.

Problem Statement

You are given a mutable integer matrix with NN rows and MM columns, zero-based coordinates. Process QQ commands in order:

  • SET r c v: replace the current value of cell (r,c)(r,c) by vv (this is not an addition).
  • SUM r1 c1 r2 c2: output the sum of all matrix entries in the closed rectangle from (r1,c1)(r1,c1) through (r2,c2)(r2,c2).

Every query is evaluated after all preceding updates.

Input

The first line contains N,M,QN,M,Q. The next NN lines each contain MM integers. Then follow QQ lines of SET r c v or SUM r1 c1 r2 c2.

Output

For each SUM command, output the rectangle sum on a separate line in query order.

Subtasks

  • Subtask 1 (20 points): 1≤N≤51\le N\le 5; 1≤M≤51\le M\le 5; 1≤Q≤401\le Q\le 40; −1000≤Ai,j,v≤1000-1000\le A_{i,j},v\le1000; SET coordinates 0≤r<N0\le r<N, 0≤c<M0\le c<M; SUM bounds 0≤r1≤r2<N0\le r1\le r2<N, 0≤c1≤c2<M0\le c1\le c2<M.
  • Subtask 2 (30 points): 1≤N≤401\le N\le 40; 1≤M≤401\le M\le 40; 1≤Q≤4001\le Q\le 400; −1000≤Ai,j,v≤1000-1000\le A_{i,j},v\le1000; SET coordinates 0≤r<N0\le r<N, 0≤c<M0\le c<M; SUM bounds 0≤r1≤r2<N0\le r1\le r2<N, 0≤c1≤c2<M0\le c1\le c2<M.
  • Subtask 3 (50 points): 1≤N≤2001\le N\le 200; 1≤M≤2001\le M\le 200; 1≤Q≤50001\le Q\le 5000; −1000≤Ai,j,v≤1000-1000\le A_{i,j},v\le1000; SET coordinates 0≤r<N0\le r<N, 0≤c<M0\le c<M; SUM bounds 0≤r1≤r2<N0\le r1\le r2<N, 0≤c1≤c2<M0\le c1\le c2<M.

Examples

Example 1

Input

5 5 3
3 0 1 4 2
5 6 3 2 1
1 2 0 1 5
4 1 0 1 7
1 0 3 0 5
SUM 2 1 4 3
SET 3 2 2
SUM 2 1 4 3

Output

8
10

Explanation

The first query sums columns 1 through 3 of rows 2, 3 and 4: 3+2+3=83+2+3=8. Updating cell (3,2) from 0 to 2 increases the queried sum by 2, yielding 1010.

Example 2

Input

2 3 5
1 -2 3
4 0 -1
SUM 0 0 1 2
SET 0 1 6
SUM 0 1 1 2
SET 1 2 -5
SUM 1 0 1 2

Output

5
8
-1

Explanation

The initial total is 1−2+3+4+0−1=51-2+3+4+0-1=5. Setting (0,1) to 6 gives 6+3+0−1=86+3+0-1=8 over columns 1–2. The last update makes row 1 equal to (4,0,-5), whose sum is −1-1.