Under construction

Still being built. Things may change

2D Array Range Add and Range Sum

Interview

Time limit3sMemory limit512 MB

Summary
Given an n by n matrix, apply all rectangle add updates in order, then print the sum of each rectangle query.
Level

Medium4 of 10

Topics
Matrix, Prefix sum
Solved
No attempts yet

Problem

You are given an integer 2D array AA of size n×nn \times n. The elements are A[0][0],A[0][1],…,A[n−1][n−1]A[0][0], A[0][1], \ldots, A[n-1][n-1]. The initial values of all elements are given as input. Array BB stores mm queries on AA, and each query is one of two types. Type 1 is the first kind, and type 2 is the second kind.

  • 1 i1i_1 j1j_1 i2i_2 j2j_2 kk: Add kk to every A[i][j]A[i][j] with i1≤i≤i2i_1 \le i \le i_2 and j1≤j≤j2j_1 \le j \le j_2.
  • 2 i1i_1 j1j_1 i2i_2 j2j_2: Output the sum of every A[i][j]A[i][j] with i1≤i≤i2i_1 \le i \le i_2 and j1≤j≤j2j_1 \le j \le j_2.

Process the queries in BB from the first to the mm-th in order, and output the results of the type 2 queries. All type 1 queries are stored before all type 2 queries in BB.

Input

The first line contains nn and mm, separated by a space. The next nn lines contain the elements of AA. The jj-th number on the ii-th line is A[i−1][j−1]A[i-1][j-1]. The next mm lines each contain one query, given in order. The numbers in each line are separated by spaces.

Output

Print the results of the type 2 queries in order, one per line.

Constraints

  • 1≤n≤10001 \le n \le 1000, 1≤m≤3000001 \le m \le 300000
  • 1≤A[i][j]≤10000001 \le A[i][j] \le 1000000 (0≤i,j≤n−10 \le i, j \le n-1)
  • BB contains only type 1 and type 2 queries.
  • All type 1 queries are stored before all type 2 queries in BB.
  • BB contains at least one type 2 query.
  • 0≤i1≤i2≤n−10 \le i_1 \le i_2 \le n-1
  • 0≤j1≤j2≤n−10 \le j_1 \le j_2 \le n-1
  • 1≤k≤100001 \le k \le 10000

Examples1

  1. Example 1

    Input
    4 5
    1 1 1 1
    1 1 1 1
    1 1 1 1
    1 1 1 1
    1 0 0 1 1 1
    1 0 0 2 2 2
    2 0 0 1 1
    2 0 0 2 2
    2 1 1 2 3
    
    Expected output
    16
    31
    15