poj 1195 - Mobile phones(树状数组)

二维的树状数组,,,

记得矩阵的求和运算要想好在写。。。。

代码如下:


#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <cstdlib>
#include <stack>
#include <queue>
#include <vector>
#include <algorithm>
#include <set>
#include <map>

#define M 1100
#define INF 0x7fffffff
#define eps 1e-8
#define LL long long
#define LLU unsigned long long
#define lowbit(x) (x&-x)

using namespace std;

LL c[M][M];
int n;
void add(int x, int y, int a)
{
	for(int i = x; i <= n; i+=lowbit(i))
		for(int j = y; j <= n; j+=lowbit(j))
			c[i][j] += a;
}
LL sum(int l, int r, int ll, int rr)
{
	LL s1 = 0;
	for(int i = l-1; i > 0; i-=lowbit(i))
		for(int j = r-1; j > 0; j -= lowbit(j))
			s1+=c[i][j];
	LL s2 = 0;
	for(int i = l-1; i > 0; i-=lowbit(i))
		for(int j = rr; j > 0; j -= lowbit(j))
			s2+=c[i][j];
	LL s3 = 0;
	for(int i = ll; i > 0; i-=lowbit(i))
		for(int j = r-1; j > 0; j -= lowbit(j))
			s3+=c[i][j];
	LL s = 0;
	for(int i = ll; i > 0; i-=lowbit(i))
		for(int j = rr; j > 0; j-=lowbit(j))
			s+=c[i][j];
	return s+s1-s2-s3;
}
int main ()
{
	int t, x, y, a, l, r, ll, rr;
	scanf("%d%d", &t, &n);
	while(scanf("%d", &t), t!=3)
	{
		if(t==1)
		{
			scanf("%d%d%d", &x, &y, &a);
			add(x+1, y+1, a);
		}
		else
		{
			scanf("%d%d%d%d", &l, &r, &ll, &rr);
			printf("%lld\n", sum(l+1, r+1, ll+1, rr+1));
		}
	}
	return 0;
}



你可能感兴趣的:(poj 1195 - Mobile phones(树状数组))