The Legend of Dakgangjeong
InterviewTime limit4sMemory limit512 MB
Given an N by N grid, answer Q rectangle queries by summing interior cells as +1 and border cells as -1.
- Level
Easy3 of 10
- Topics
- Prefix sum, Matrix
- Solved
- No attempts yet
Problem
Arim lives in Changwon and is a 'beer-only drinker', so beer never makes Arim drunk. Boseon is a 'soju-only drinker' and is sure that drinking beer with Arim would leave Boseon completely drunk. So Boseon decided to trust the legend of the chicken near the Gyeongsang National University back gate.
According to the legend, a special fried chicken sold near the back gate of Gyeongsang National University has a certain level of intoxication-blocking power. Boseon went to that shop. Its sales system is unlike that of an ordinary chicken shop.
The system works as follows.
- There is an grid, and each cell holds 1 g of boneless chicken.
- The chicken in row , column has a constant .
- A buyer picks two points and . After the two points are chosen, all chicken in every cell with and is packed together and fried.
- The intoxication-blocking level of the fried chicken is computed as follows.
The machine that calculates this for each pair of points broke down on the day Boseon visited. Boseon asked the people nearby to compute the level for every pair of points chosen.
Let's help Boseon.
Input
The first line contains the grid size . ()
From the second line onward, the next lines each contain space-separated values, where the -th value on the -th line is the constant . ()
The line contains the number of queries . ()
From line onward, the next lines each contain , , , , separated by spaces. (; ; )
All numbers in the input are integers.
Output
For each of the queries, print on its own line the intoxication-blocking level of the fried chicken chosen by , , , .
Hint
Python 3 users are recommended to submit with PyPy3.