您的位置:首页 > 其它

pku 1195 Mobile phones 树状数组 解题报告

2009-11-17 15:09 561 查看
pku 1195 Mobile phones 解题报告
刚刚学完树状数组,拿pku1195来练习就a了,第一道二维树状数组的应用,值得纪念一下.算法就是简单的二维树状数组的基本函数应用,详细见我另一篇文章树状数组小结。
题目链接: http://acm.pku.edu.cn/JudgeOnline/problem?id=1195
AC代码:
#include <stdio.h>
#include <string.h>
#define M 1030

int Tree[M][M];
int Operand, Size;

int Lowbit(int x)
{
return x & -x;
}

void update(int i, int j, int num)
{
while (i <= Size)
{
int temp = j;
while (temp <= Size)
{
Tree[i][temp] += num;
temp += Lowbit(temp);
}
i += Lowbit(i);
}
}

int sum(int x, int y)
{
int temp = 0;

for (int i = x; i > 0; i -= Lowbit(i))
{
for (int j = y; j > 0; j -= Lowbit(j))
{
temp += Tree[i][j];
}
}
return temp;
}

int main()
{
freopen("1.txt", "r", stdin);

int i, j, x, y, Incremental, a, b, c, d, res;
while (scanf("%d", &Operand))
{
if (Operand == 3)
{
break;
}
else if (Operand == 0)
{
scanf("%d", &Size);
for (i = 0; i <= Size; i++)
{
for (j = 0; j <= Size; j++)
{
Tree[i][j] = 0;
}
}
}
else if (Operand == 1)
{
scanf("%d%d%d", &x, &y, &Incremental);
update(x + 1, y + 1, Incremental);
}
else if (Operand == 2)
{
scanf("%d%d%d%d", &a, &b, &c, &d);
a++; b++; c++; d++;
res = sum(c, d) - sum(c, b - 1) - sum(a - 1, d) + sum(a - 1, b - 1);
printf("%d/n", res);
}
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: