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;
}
刚刚学完树状数组,拿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;
}
相关文章推荐
- 【原】 POJ 1195 Mobile phones 2D树状数组 解题报告
- PKU1195 Mobile phones - 二维树状数组
- [POJ 1195] Mobile phones · 树状数组
- POJ 1195:Mobile phones 二维树状数组
- POJ 1195 Mobile phones 二维树状数组
- poj 1195 mobile phones 二维树状数组
- POJ 1195 - Mobile phones 二维树状数组(单点更新..区间查询)
- POJ 1195 Mobile phones 二维树状数组
- poj 1195 Mobile phones 【二维树状数组】
- POJ 1195:Mobile phones 二维树状数组
- poj 1195 Mobile phones 二维树状数组
- [树状数组]POJ 1195 Mobile phones
- POJ1195 Mobile phones 【二维树状数组】
- poj 1195 Mobile phones 解题报告
- POJ-1195 Mobile phones (二维树状数组 单点修改 区块查询)
- poj 1195 Mobile phones 二维树状数组
- poj1195 Mobile phones 二维线段树和二维树状数组两种做法 树套树
- POJ-1195 Mobile phones 二维树状数组
- POJ1195 Mobile phones —— 二维树状数组(二维BIT)
- 二维树状数组 —— POJ 1195 Mobile phones