2D Array Multi-Update Single Sum
InterviewTime limit3sMemory limit512 MB
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 of size . The elements of are . The initial value of every element of is given in the input. You are also given an array that stores queries on . Each query in is one of two types. Type 1 is written first, and type 2 is written second.
- 1 : Add to every where and .
- 2 : Print the sum of every where and .
Process the queries in from the first to the -th in order, and print the result of the type 2 query. Note that contains only one type 2 query, and it is the last one.
Input
The first line contains and , separated by a space.
Each of the next lines contains the elements of . The -th number on the -th line is .
Each of the next 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
- ,
- ()
- contains only queries of type 1 and type 2.
- contains exactly one type 2 query, and it is the last query.