Under construction

Still being built. Things may change

2D Array Multi-Update Single Sum

Interview

Time limit3sMemory limit512 MB

Summary
Given an n by n grid and a list of rectangle add and rectangle sum queries, print the one final rectangle sum after all updates are applied.
Level

Medium5 of 10

Topics
Prefix sum, Matrix, Math
Solved
No attempts yet

Problem

You are given an integer 2D array AA of size n×nn \times n. The elements of AA 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 value of every element of AA is given in the input. You are also given an array BB that stores mm queries on AA. Each query in BB is one of two types. Type 1 is written first, and type 2 is written second.

  • 1 i1i_1 j1j_1 i2i_2 j2j_2 kk: Add kk to every A[i][j]A[i][j] where 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: Print the sum of every A[i][j]A[i][j] where 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 print the result of the type 2 query. Note that BB contains only one type 2 query, and it is the last one.

Input

The first line contains nn and mm, separated by a space.

Each of the next nn lines contains the elements of AA. The jj-th number on the ii-th line is A[i−1][j−1]A[i-1][j-1].

Each of the next mm lines contains one query. The numbers of a query are separated by spaces.

Output

Print the result of the type 2 query on the first line.

Constraints

  • 1≤n≤1,0001 \le n \le 1{,}000, 1≤m≤300,0001 \le m \le 300{,}000
  • 1≤A[i][j]≤1,000,0001 \le A[i][j] \le 1{,}000{,}000 (0≤i,j≤n−10 \le i, j \le n-1)
  • BB contains only queries of type 1 and type 2.
  • BB contains exactly one type 2 query, and it is the last 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≤10,0001 \le k \le 10{,}000

Examples1

  1. Example 1

    Input
    4 3
    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 2 2
    
    Expected output
    31