现在的位置: 首页 > 综合 > 正文

poj-1195 Mobile phones *

2012年02月16日 ⁄ 综合 ⁄ 共 722字 ⁄ 字号 评论关闭
/* 难得的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;
}

【上篇】
【下篇】

抱歉!评论已关闭.