【bzoj2901】矩阵求和

题目链接

我靠。。博主没有脑子了啊。。这种题都不会写了。。。

观察式子

ans====i=xlxrj=ylyrk=1nA[i][k]B[k][j]k=1ni=xlxrj=ylyrA[i][k]B[k][j]k=1n(i=xlxrA[i][k])(j=ylyrB[k][j])k=1n(SumA[xr][k]SumA[xl1]k])(SumB[k][yr]SumB[k][yl1])(237)(238)(239)(240) (237) a n s = ∑ i = x l x r ∑ j = y l y r ∑ k = 1 n A [ i ] [ k ] ∗ B [ k ] [ j ] (238) = ∑ k = 1 n ∑ i = x l x r ∑ j = y l y r A [ i ] [ k ] ∗ B [ k ] [ j ] (239) = ∑ k = 1 n ( ∑ i = x l x r A [ i ] [ k ] ) ∗ ( ∑ j = y l y r B [ k ] [ j ] ) (240) = ∑ k = 1 n ( S u m A [ x r ] [ k ] − S u m A [ x l − 1 ] k ] ) ∗ ( S u m B [ k ] [ y r ] − S u m B [ k ] [ y l − 1 ] )

呵呵呵呵呵呵呵呵呵呵呵

代码:

#include<cstdio>
#include<vector>
#include<queue>
#include<ctime>
#include<algorithm>
#include<cstdlib>
#include<stack>
#include<cstring>
#include<cmath>
using namespace std;

typedef long long LL;

const int INF = 2147483647;
const int maxn = 2010;

int n,m;
int a[maxn][maxn],b[maxn][maxn];
LL ans;

inline LL getint()
{
    LL ret = 0,f = 1;
    char c = getchar();
    while (c < '0' || c > '9')
    {
        if (c == '-') f = -1;
        c = getchar();
    }
    while (c >= '0' && c <= '9') ret = ret * 10 + c - '0',c = getchar();
    return ret * f;
}

int main()
{
    #ifdef AMC
        freopen("AMC1.txt","r",stdin);
        freopen("AMC2.txt","w",stdout);
    #endif
    n = getint(); m = getint();
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            a[i][j] = getint();
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            b[i][j] = getint();

    for (int j = 1; j <= n; j++)    
        for (int i = 1; i <= n; i++)
            a[i][j] += a[i - 1][j];
    for (int i = 1; i <= n; i++)
        for (int j = 1; j <= n; j++)
            b[i][j] += b[i][j - 1];

    for (int i = 1; i <= m; i++)
    {
        int x = getint(),y = getint();
        int z = getint(),w = getint();
        int xl = min(x,z),xr = max(x,z);
        int yl = min(y,w),yr = max(y,w);

        ans = 0;
        for (int j = 1; j <= n; j++)
            ans += 1ll * (a[xr][j] - a[xl - 1][j]) * (b[j][yr] - b[j][yl - 1]);
        printf("%lld\n",ans);
    }

    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值