一个二维树状数组的问题。
POJ1195 mobile phone
我的二维数组是按照题给的一样0,0开始编码,其他的都相同(源代码如下),把题目中x,y=0的情况映射成x,y=-1的情况,直接返回0,结果永远TLE。然后我把它全部改成从1,1开始编码就AC了。
答案是因为,树状数组的定义就是从1开始编码的。记住了啊,好好看书(和PPT)啊。这也是题目中的坑啊
#include <iostream>
#include <stdio.h>
using namespace std;
const int maxs = 1100;
int c[maxs][maxs];
int s;
inline int lowbit(int x)
{
return x & (-x);
}
inline void change(int x, int y, int d)
{
for (int xi = x; xi < s; xi += lowbit(xi)) {
for (int yi = y; yi < s; yi += lowbit(yi)) {
c[xi][yi] += d;
}
}
}
inline int getsum(int x, int y)
{
int sum = 0;
if (x < 0 || y < 0)
return 0;
for (int xi = x; xi >= 0; xi -= lowbit(xi)) {
for (int yi = y; yi >= 0; yi -= lowbit(yi)) {
sum += c[xi][yi];
}
}
return sum;
}
int main()
{
int ins;
while (scanf("%d", &ins) == 1 && ins != 3)
{
switch (ins)
{
case 0:
{
scanf("%d", &s);
//cin >> s;
break;
}
case 1:
{
int x, y, a;
scanf("%d%d%d", &x,&y,&a);
//cin >> x >> y >> a;
change(x, y, a);
break;
}
case 2:
{
int l, r, b, t;
scanf("%d%d%d%d",&l,&b,&r,&t);
printf("%d\n", getsum(r, t) + getsum(l - 1, b - 1) - getsum(l - 1, t) - getsum(r, b - 1));
//cin >> l >> b >> r >> t;
//cout << getsum(r, t) + getsum(l - 1, b - 1) - getsum(l - 1, t) - getsum(r, b - 1) << endl;
break;
}
}
}
//system("pause");
return 0;
}
来源:https://blog.csdn.net/weixin_44288817/article/details/101145262