poj-1195 Mobile phones *
/* 难得的1A题~~ 标准的树状数组
* 1195.cpp
* 注意坐标从0开始, 应处理成从1开始
* Created on: 2011-7-11
* Author:
*/
#include <cstdio>
using namespace std;
const int MAXS = 1024 + 5;
int map[MAXS][MAXS] = {};
int c[MAXS][MAXS] = {};
int s, x, y , a, l, r, b, t, cmd; //题目中的参数
int lowbit(int x){
return x & (-x);
}
void update(int xx, int yy, int aa){
map[xx][yy] += aa;
for(int i=xx; i<=s; i+=lowbit(i)){
for(int j=yy; j<=s; j+=lowbit(j)){
c[i][j] += aa;
}
}
}
int sum(int xx, int yy){
int ans = 0;
for(int i=xx; i>0; i-=lowbit(i)){
for(int j=yy; j>0; j-=lowbit(j)){
ans += c[i][j];
}
}
return ans;
}
int main(){
scanf("%d %d", &s, &s);
while(scanf("%d", &cmd)){
if(cmd == 3) return 0;
if(cmd == 1){
scanf("%d %d %d", &x, &y, &a);
update(x+1, y+1, a);
}
else if(cmd == 2){
scanf("%d %d %d %d", &l, &b, &r, &t);
int ans = sum(r+1, t+1) - sum(l, t+1) - sum(r+1, b) + sum(l, b);
printf("%d\n", ans);
}
}
return 0;
}
* 1195.cpp
* 注意坐标从0开始, 应处理成从1开始
* Created on: 2011-7-11
* Author:
*/
#include <cstdio>
using namespace std;
const int MAXS = 1024 + 5;
int map[MAXS][MAXS] = {};
int c[MAXS][MAXS] = {};
int s, x, y , a, l, r, b, t, cmd; //题目中的参数
int lowbit(int x){
return x & (-x);
}
void update(int xx, int yy, int aa){
map[xx][yy] += aa;
for(int i=xx; i<=s; i+=lowbit(i)){
for(int j=yy; j<=s; j+=lowbit(j)){
c[i][j] += aa;
}
}
}
int sum(int xx, int yy){
int ans = 0;
for(int i=xx; i>0; i-=lowbit(i)){
for(int j=yy; j>0; j-=lowbit(j)){
ans += c[i][j];
}
}
return ans;
}
int main(){
scanf("%d %d", &s, &s);
while(scanf("%d", &cmd)){
if(cmd == 3) return 0;
if(cmd == 1){
scanf("%d %d %d", &x, &y, &a);
update(x+1, y+1, a);
}
else if(cmd == 2){
scanf("%d %d %d %d", &l, &b, &r, &t);
int ans = sum(r+1, t+1) - sum(l, t+1) - sum(r+1, b) + sum(l, b);
printf("%d\n", ans);
}
}
return 0;
}