poj1195

二维树状数组

View Code
#include <iostream>
#include
<cstdlib>
#include
<cstring>
#include
<cstdio>
using namespace std;

#define maxn 1030

int c[maxn][maxn];
int Row, Col, n;

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

int Sum(int i, int j)
{
int tempj, sum = 0;
while (i > 0)
{
tempj
= j;
while (tempj > 0)
{
sum
+= c[i][tempj];
tempj
-= Lowbit(tempj);
}
i
-= Lowbit(i);
}
return sum;
}

void Update(int i, int j, int num)
{
int tempj;
while (i <= Row)
{
tempj
= j;
while (tempj <= Col)
{
c[i][tempj]
+= num;
tempj
+= Lowbit(tempj);
}
i
+= Lowbit(i);
}
}

int cal(int l, int b, int r, int t)
{
return Sum(r, t) - Sum(l, t) - Sum(r, b) + Sum(l, b);
}

int main()
{
//freopen("t.txt", "r", stdin);
int order, x, y, a, l, r, b, t;
while (scanf("%d", &order), order != 3)
{
if (order == 0)
{
scanf(
"%d", &n);
Col
= Row = n + 2;
memset(c,
0, sizeof(c));
}
else if (order == 1)
{
scanf(
"%d%d%d", &x, &y, &a);
Update(x
+ 2, y + 2, a);
}
else if (order == 2)
{
scanf(
"%d%d%d%d", &l, &b, &r, &t);
printf(
"%d\n", cal(l + 1, b + 1, r + 2, t + 2));
}
}
return 0;
}

你可能感兴趣的:(poj)