H. Hard Demon Problem

H. Hard Demon Problem

Swing is opening a pancake factory! A good pancake factory must be good at flattening things, so Swing is going to test his new equipment on 2D matrices.

Swing is given an n×n matrix M containing positive integers. He has q queries to ask you.

For each query, he gives you four integers x1, y1, x2, y2 and asks you to flatten the submatrix bounded by (x1,y1) and (x2,y2) into an array A. Formally, A=[M(x1,y1),M(x1,y1+1),,M(x1,y2),M(x1+1,y1),M(x1+1,y1+1),,M(x2,y2)].

The following image depicts the flattening of a submatrix bounded by the red dotted lines. The orange arrows denote the direction that the elements of the submatrix are appended to the back of A, and A is shown at the bottom of the image.

Afterwards, he asks you for the value of i=1|A|Aii (sum of Aii over all i).

Input

The first line contains an integer t (1t103) — the number of test cases.

The first line of each test contains two integers n and q (1n2000,1q106) — the length of M and the number of queries.

The following n lines contain n integers each, the i'th of which contains M(i,1),M(i,2),,M(i,n) (1M(i,j)106).

The following q lines contain four integers x1, y1, x2, and y2 (1x1x2n,1y1y2n) — the bounds of the query.

It is guaranteed that the sum of n over all test cases does not exceed 2000 and the sum of q over all test cases does not exceed 106.

Output

For each test case, output the results of the q queries on a new line.

Example

Input

2
4 3
1 5 2 4
4 9 5 3
4 5 2 3
1 5 5 2
1 1 4 4
2 2 3 3
1 2 4 3
3 3
1 2 3
4 5 6
7 8 9
1 1 1 3
1 3 3 3
2 2 2 2

Output

500 42 168 
14 42 5 

Note

In the second query of the first test case, A=[9,5,5,2]. Therefore, the sum is 19+25+35+42=42.

 

解题思路

  纯粹就是把累加转换成前缀和的表示形式然后推式子,关键是需要维护前缀和很多,且式子很复杂易推错。

  先从简单的情况考虑,假设询问的是某一行,即 x1=x2=i,此时的结果为
j=y1y2(jy1+1)ai,j=(1y1)j=y1y2ai,j+j=y1y2jai,j

  记 r˙i,j=k=1jai,kr¨i,j=k=1jkai,k,则有
(1y1)j=y1y2ai,j+j=y1y2jai,j=(1y1)(r˙i,y2r˙i,y11)+r¨i,y2r¨i,y11

  现在考虑询问多行的情况,对于每一行,其实只需在上述结果的基础上再加上 (ix1)(y2y1+1)j=y1y2ai,j,以保证该行每个元素乘以的系数是正确的。

i=x1x2(1y1)(r˙i,y2r˙i,y11)+r¨i,y2r¨i,y11+(ix1)(y2y1+1)j=y1y2ai,j=i=x1x2(1y1)(r˙i,y2r˙i,y11)+r¨i,y2r¨i,y11+(ix1)(y2y1+1)(r˙i,y2r˙i,y11)=(y2y1+1)i=x1x2ir˙i,y2ir˙i,y11+(1y1x1(y2y1+1))i=x1x2r˙i,y2r˙i,y11+i=x1x2r¨i,y2r¨i,y11

  记 c˙i,j=k=1ir˙k,jc¨i,j=k=1ir¨k,jci,j=k=1ikr˙k,j,则有
(y2y1+1)i=x1x2ir˙i,y2ir˙i,y11+(1y1x1(y2y1+1))i=x1x2r˙i,y2r˙i,y11+i=x1x2r¨i,y2r¨i,y11=(y2y1+1)(cx2,y2cx11,y2(cx2,y11cx11,y11))+(1y1x1(y2y1+1))(c˙x2,y2c˙x11,y2(c˙x2,y11c˙x11,y11))+c¨x2,y2c¨x11,y2(c¨x2,y11c¨x11,y11)
  记 f(c)=cx2,y2cx11,y2(cx2,y11cx11,y11),则有
(y2y1+1)(cx2,y2cx11,y2(cx2,y11cx11,y1))+(1y1x1(y2y1+1))(c˙x2,y2c˙x11,y2(c˙x2,y11c˙x11,y1))+c¨x2,y2c¨x11,y2(c¨x2,y11c¨x11,y1)=(y2y1+1)f(c)+(1y1x1(y2y1+1))f(c˙)+f(c¨)
  AC 代码如下,时间复杂度为 O(n2+q)

#include <bits/stdc++.h>
using namespace std;

typedef long long LL;

const int N = 2005;

LL r1[N][N], r2[N][N], c1[N][N], c2[N][N], c3[N][N];

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 1; i <= n; i++) {
        for (int j = 1; j <= n; j++) {
            int x;
            cin >> x;
            r1[i][j] = r1[i][j - 1] + x;
            r2[i][j] = r2[i][j - 1] + j * x;
        }
    }
    for (int j = 1; j <= n; j++) {
        for (int i = 1; i <= n; i++) {
            c1[i][j] = c1[i - 1][j] + r1[i][j];
            c2[i][j] = c2[i - 1][j] + r2[i][j];
            c3[i][j] = c3[i - 1][j] + i * r1[i][j];
        }
    }
    while (m--) {
        int x1, y1, x2, y2;
        cin >> x1 >> y1 >> x2 >> y2;
        auto f = [&](LL c[][N]) {
            return c[x2][y2] - c[x1 - 1][y2] - c[x2][y1 - 1] + c[x1 - 1][y1 - 1];
        };
        cout << (y2 - y1 + 1) * f(c3) + (1 - y1 - x1 * (y2 - y1 + 1)) * f(c1) + f(c2) << ' ';
    }
    cout << '\n';
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int t;
    cin >> t;
    while (t--) {
        solve();
    }
    
    return 0;
}

 

参考资料

  Codeforces Round 993 (Div. 4) Editorial:https://codeforces.com/blog/entry/137306

posted @   onlyblues  阅读(29)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 单线程的Redis速度为什么快?
· 展开说说关于C#中ORM框架的用法!
· Pantheons:用 TypeScript 打造主流大模型对话的一站式集成库
· SQL Server 2025 AI相关能力初探
· 为什么 退出登录 或 修改密码 无法使 token 失效
历史上的今天:
2023-12-19 D. Array Collapse
Web Analytics
点击右上角即可分享
微信分享提示