POJ 1195 Mobile phones 二维树状数组
题意:有一个N * N广场,广场里面有一些手机,某个格子是可以改变的,增加或者减少,问一个小矩阵内有多少个手机。
思路 :裸的二维树状数组。
代码:
[cpp]
#include <iostream>
#include <cstdio>
#include <string.h>
using namespace std;
typedef long long LL;
const int N = 1030;
int num[N][N];
int lowbit(int x){
return x & (-x);
}
void update(int x,int y,int add){
int t = y;
while(x < N){
y = t;
while(y < N){
num[x][y] += add;
if(num[x][y] < 0)
num[x][y] = 0;
y += lowbit(y);
}
x += lowbit(x);
}
}
LL sum(int x,int y){
int t = y;
LL s = 0;
while(x > 0){
y = t;
while(y > 0){
s += num[x][y];
y -= lowbit(y);
}
x -= lowbit(x);
}
return s;
}
int main(){
//freopen("1.txt","r",stdin);
int id,n;
while(scanf("%d%d",&id,&n) != EOF){
memset(num,0,sizeof(num));
int x1,y1,x2,y2,value;
while(1){
scanf("%d",&id);
if(id == 3)
break;
else if(id == 1){
scanf("%d%d%d",&x1,&y1,&value);
update(x1+1,y1+1,value);
}
else if(id == 2){
scanf("%d%d%d%d",&x1,&y1,&x2,&y2);
printf("%lld\n",sum(x2+1,y2+1) - sum(x1,y2+1) - sum(x2+1,y1) + sum(x1,y1));
}
}
}
return 0;
}
补充:软件开发 , C++ ,