标签:des style blog http color java os io
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1559
Time Limit: 30000/10000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2901 Accepted Submission(s): 1454
1 /*二维树状数组:同样不要忘记c的初始化, 2 modify 的功能是改变元素(x, y), 3 sum的功能则是求从元素(1, 1)开始到(x, y)的总和, 4 同样,可以求出任意一个子矩阵内的所有元素之和, 5 即sum(x2, y2) - sum(x1-1, y2) - sum(x2, y1-1) + sum(x1-1, y1-1) 6 */ 7 8 9 #include<iostream> 10 11 using namespace std; 12 13 int N,M; 14 int c[1005][1005]; 15 16 int lowbit( int x ) 17 { 18 return x & (-x); 19 } 20 21 void modify( int x, int y, int delta ) 22 { 23 int i, j; 24 for(i=x; i<=N; i+=lowbit(i)) 25 { 26 for(j=y; j<=M; j+=lowbit(j)) 27 { 28 c[i][j] += delta; 29 } 30 } 31 } 32 33 int sum( int x, int y ) 34 { 35 int res = 0, i, j; 36 for(i=x; i>0; i-=lowbit(i)) 37 { 38 for(j=y; j>0; j-=lowbit(j)) 39 { 40 res += c[i][j]; 41 } 42 } 43 return res; 44 } 45 46 void init () 47 { 48 int i,j; 49 for(i=0;i<=N;i++) 50 for(j=0;j<=M;j++) 51 c[i][j]=0; 52 53 } 54 55 int main() 56 { 57 int t; 58 scanf("%d",&t); 59 while(t--) 60 { 61 int x,y,i,j,k,temp; 62 int max = 0 ; 63 // memset(c,0,sizeof(c)); 64 // c[1005][1005]= {{0}}; 65 scanf("%d%d%d%d",&N,&M,&x,&y); 66 init (); 67 68 for(i=1;i<=N;i++) 69 for(j=1;j<=M;j++) 70 { 71 scanf("%d",&k); 72 modify(i,j,k); 73 } 74 for(i=1;i+x-1<=N;i++) 75 for(j=1;j+y-1<=M;j++) 76 { 77 temp=sum(i+x-1,j+y-1)-sum(i-1,j+y-1)-sum(i+x-1,j-1)+sum(i-1,j-1); 78 79 if(temp>max)max= temp ; 80 } 81 printf("%d\n",max); 82 83 84 } 85 return 0 ; 86 87 }
HDU1559 最大子矩阵 (二维树状数组),布布扣,bubuko.com
标签:des style blog http color java os io
原文地址:http://www.cnblogs.com/ws5167/p/3915527.html