标签:lin ^c div bzoj blog for fine ros lld
题目描述
输入
输出
样例输入
3 2
1 9 8
3 2 0
1 8 3
9 8 4
0 5 15
1 9 6
1 1 3 3
2 3 1 2
样例输出
661
388
题解
前缀和
不妨设a<=c,b<=d,那么$\ \ \ \sum\limits_{i=a}^c\sum\limits_{j=b}^dR[i][j]\\=\sum\limits_{i=a}^c\sum\limits_{j=b}^d\sum\limits_{k=1}^nP[i][k]·Q[k][j]\\=\sum\limits_{k=1}^n(\sum\limits_{i=a}^cP[i][k])·(\sum\limits_{j=b}^dQ[k][j])\\=\sum\limits_{k=1}^n(sumP[c][k]-sumP[a-1][k])(sumQ[k][d]-sumQ[k][b-1])$
时间复杂度$O(nm)$,可过。
#include <cstdio> #include <algorithm> using namespace std; #define N 2010 int p[N][N] , q[N][N] , sp[N][N] , sq[N][N]; inline int read() { int ret = 0; char ch = getchar(); while(ch < ‘0‘ || ch > ‘9‘) ch = getchar(); while(ch >= ‘0‘ && ch <= ‘9‘) ret = (ret << 3) + (ret << 1) + ch - ‘0‘ , ch = getchar(); return ret; } int main() { int n , m , i , j , a , b , c , d; long long ans; n = read() , m = read(); for(i = 1 ; i <= n ; i ++ ) for(j = 1 ; j <= n ; j ++ ) a = read() , sp[i][j] = sp[i - 1][j] + a; for(i = 1 ; i <= n ; i ++ ) for(j = 1 ; j <= n ; j ++ ) a = read() , sq[i][j] = sq[i][j - 1] + a; while(m -- ) { a = read() , b = read() , c = read() , d = read(); if(a > c) swap(a , c); if(b > d) swap(b , d); ans = 0; for(i = 1 ; i <= n ; i ++ ) ans += (long long)(sp[c][i] - sp[a - 1][i]) * (sq[i][d] - sq[i][b - 1]); printf("%lld\n" , ans); } return 0; }
标签:lin ^c div bzoj blog for fine ros lld
原文地址:http://www.cnblogs.com/GXZlegend/p/7110298.html