标签:des style http io os ar strong for div
| Time Limit: 5000MS | Memory Limit: 65536K | |
| Total Submissions: 15182 | Accepted: 7013 |
Description
Input

Output
Sample Input
0 4 1 1 2 3 2 0 0 2 2 1 1 1 2 1 1 2 -1 2 1 1 2 3 3
Sample Output
3 4
Source
简单的二维树状数组求区域和,注意xy的坐标,要保证从1开始。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define maxn 1100
int c[maxn][maxn] , n ;
int f(int x)
{
return x & -x ;
}
void add(int x,int y,int a)
{
int i , j ;
for(i = x ; i <= n ; i += f(i))
{
for(j = y ; j <= n ; j += f(j) )
c[i][j] += a ;
}
return ;
}
int sum(int x,int y)
{
int ans = 0 , i , j ;
for(i = x ; i >= 1 ; i -= f(i) )
{
for(j = y ; j >= 1 ; j -= f(j) )
ans += c[i][j];
}
return ans ;
}
int main()
{
int k , i , j , x1 , y1 , x2 , y2 , a , temp ;
scanf("%d %d", &k, &n);
memset(c,0,sizeof(c));
while(1)
{
scanf("%d", &k);
if(k == 3)
break;
if(k == 1)
{
scanf("%d %d %d", &x1, &y1, &a);
x1++ ;
y1++ ;
add(x1,y1,a);
continue ;
}
scanf("%d %d %d %d", &x1, &y1, &x2, &y2);
x1++ ;
y1++ ;
x2++ ;
y2++ ;
temp = sum(x2,y2) + sum(x1-1,y1-1) - sum(x1-1,y2) - sum(x2,y1-1);
printf("%d\n", temp);
}
return 0;
}
poj1195--Mobile phones(二维树状数组)
标签:des style http io os ar strong for div
原文地址:http://blog.csdn.net/winddreams/article/details/39504837