Table Range Sum with Updates

Time limit1sMemory limit256 MB

Summary
Apply point updates on an N by N table and answer each rectangle sum query in order.
Level

Medium4 of 10

Topics
Segment tree
Solved
No attempts yet

Problem

An N×NN \times N table is filled with natural numbers. The cell in row ii and column jj is written as (i,j)(i, j). The sum from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2) is the sum of the numbers in every cell (x,y)(x, y) with x1≤x≤x2x_1 \le x \le x_2 and y1≤y≤y2y_1 \le y \le y_2.

Two kinds of operations arrive mixed together: one replaces the number in a single cell, the other asks for the sum of a rectangle. Consider N=4N = 4 with the table below.

1234
2345
3456
4567

The sum from (2,2)(2, 2) to (3,4)(3, 4) is 3+4+5+4+5+6=273 + 4 + 5 + 4 + 5 + 6 = 27. If the number in (2,3)(2, 3) is then replaced with 77, the sum over the same rectangle becomes 3+7+5+4+5+6=303 + 7 + 5 + 4 + 5 + 6 = 30.

Given the initial table and the operations in order, write a program that prints the value for each sum operation.

Input

The first line contains the table size NN and the number of operations MM. (1≤N≤10241 \le N \le 1024, 1≤M≤100 0001 \le M \le 100\,000)

Each of the next NN lines contains one row of the table, starting from row 1. Each line holds NN numbers separated by spaces, and every number in the table is a natural number at most 10001000.

Each of the following MM lines contains one operation, given either as four integers ww, xx, yy, cc or as five integers ww, x1x_1, y1y_1, x2x_2, y2y_2. If w=0w = 0, replace the number in (x,y)(x, y) with cc. If w=1w = 1, compute the sum from (x1,y1)(x_1, y_1) to (x2,y2)(x_2, y_2). (1≤x≤N1 \le x \le N, 1≤y≤N1 \le y \le N, 1≤c≤10001 \le c \le 1000, 1≤x1≤x2≤N1 \le x_1 \le x_2 \le N, 1≤y1≤y2≤N1 \le y_1 \le y_2 \le N)

Output

For each operation with w=1w = 1, print the computed sum on its own line, in the order the operations are given.

Examples2

  1. Example 1

    Input
    4 5
    1 2 3 4
    2 3 4 5
    3 4 5 6
    4 5 6 7
    1 2 2 3 4
    0 2 3 7
    1 2 2 3 4
    0 3 4 5
    1 3 4 3 4
    
    Expected output
    27
    30
    5
    
  2. Example 2

    Input
    1 3
    7
    1 1 1 1 1
    0 1 1 1000
    1 1 1 1 1
    
    Expected output
    7
    1000