码迷,mamicode.com
首页 > 其他好文 > 详细

URAL 1470. UFOs 三维树状数组

时间:2014-08-15 16:02:59      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:http   os   io   for   ar   html   amp   htm   

题目来源:URAL 1470. UFOs

题意:求三维区间和

思路:

#include <cstdio>
#include <cstring>
using namespace std;
const int maxn = 130;
int a[maxn][maxn][maxn];
int b[maxn][maxn][maxn];
int n, q;

int lowbit(int x)
{
	return x & -x;
}
void update(int x, int y, int z, int num)
{
	for(int i = x; i <= n; i += lowbit(i))
		for(int j = y; j <= n; j += lowbit(j))
			for(int k = z; k <= n; k += lowbit(k))
			{
				a[i][j][k] += num;
			}
				
}
int sum(int x, int y, int z)
{
	int ans = 0;
	for(int i = x; i > 0; i -= lowbit(i))
		for(int j = y; j > 0; j -= lowbit(j))
			for(int k = z; k > 0; k -= lowbit(k))
			{
				ans += a[i][j][k];
			}
	return ans;
}
int main()
{
	
	scanf("%d", &n);
	while(scanf("%d", &q) && q != 3)
	{
		if(q == 1)
		{
			int x, y, z, k;
			scanf("%d %d %d %d", &x, &y, &z, &k);
			x++, y++, z++;
			if(b[x][y][z]+k >= 0)
		 	{
			 	update(x, y, z, k);
				b[x][y][z] += k;
		 	}
		 	else
		 	{
		 		update(x, y, z, b[x][y][z]);
		 		b[x][y][z] = 0;
		 	}
		}
		else if(q == 2)
		{
			int x1, y1, z1, x2, y2, z2;
			scanf("%d %d %d %d %d %d", &x1, &y1, &z1, &x2, &y2, &z2);
			x1++, y1++, z1++, x2++, y2++, z2++;
			int ans = sum(x2, y2, z2)-sum(x1-1, y2, z2)-sum(x2, y1-1, z2)+sum(x1-1, y1-1, z2)
				-(sum(x2, y2, z1-1)-sum(x1-1, y2, z1-1)-sum(x2, y1-1, z1-1)+sum(x1-1, y1-1, z1-1));
			printf("%d\n", ans);
		}
	}
	return 0;
}


 

URAL 1470. UFOs 三维树状数组,布布扣,bubuko.com

URAL 1470. UFOs 三维树状数组

标签:http   os   io   for   ar   html   amp   htm   

原文地址:http://blog.csdn.net/u011686226/article/details/38585915

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!