Time Limit: 30000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 3906 Accepted Submission(s): 1994
#include<stdio.h> #include<algorithm> #include<string.h> #include<iostream> #define N 1005 using namespace std; int C[N][N]; int n,m,x,y; int lowbit(int x1){ return x1&(-x1); } int update(int x1,int y1,int value){ for(int i=x1;i<=n;i+=lowbit(i)){ for(int j=y1;j<=m;j+=lowbit(j)){ C[i][j]+=value; } } } int getSum(int x1,int y1){ int value = 0; for(int i=x1;i>=1;i-=lowbit(i)){ for(int j=y1;j>=1;j-=lowbit(j)){ value+=C[i][j]; } } return value; } int main() { int tcase; scanf("%d",&tcase); while(tcase--){ memset(C,0,sizeof(C)); scanf("%d%d%d%d",&n,&m,&x,&y); int num; for(int i=1;i<=n;i++){ for(int j=1;j<=m;j++){ scanf("%d",&num); update(i,j,num); } } int mx = -1; for(int i=1;i<=n;i++){ if(i+x-1>n) continue; for(int j=1;j<=m;j++){ if(j+y-1>m) continue; int x1 = i; int x2 = i+x-1; int y1 = j; int y2 = j+y-1; int sum = getSum(x2,y2)-getSum(x1-1,y2)-getSum(x2,y1-1)+getSum(x1-1,y1-1); //printf("%d\n",sum); if(sum>mx) mx = sum; } } printf("%d\n",mx); } return 0; }
原文:http://www.cnblogs.com/liyinggang/p/5399600.html