UFOs
来源:互联网 发布:office2011for mac 编辑:程序博客网 时间:2024/06/06 03:47
Description
- several new UFOs emerge in a certain sector;
- several UFOs disappear in a certain sector;
- Vasya's boss may ask him how many UFOs there are in a part of space consisting of several sectors.
Input
- If M is 1, then this number is followed by four integers x (0 ≤ x < N), y (0 ≤ y < N), z (0 ≤ z < N), K (–20000 ≤ K ≤ 20000), which are coordinates of a sector and the change in the number of UFOs in this sector. The number of UFOs in a sector cannot become negative.
- If M is 2, then this number is followed by six integers x1, y1, z1, x2, y2, z2 (0 ≤ x1 ≤ x2 < N, 0 ≤ y1 ≤ y2 < N, 0 ≤ z1 ≤ z2 < N), which mean that Vasya must compute the total number of UFOs in sectors (x, y, z) belonging to the volume: x1 ≤ x ≤ x2, y1 ≤ y ≤ y2, z1 ≤ z≤ z2.
- If M is 3, it means that Vasya is tired and goes to sleep. This entry is always the last one.
Output
Sample Input
22 1 1 1 1 1 11 0 0 0 11 0 1 0 32 0 0 0 0 0 02 0 0 0 0 1 01 0 1 0 -22 0 0 0 1 1 13
0142
描述:Vasya 不明飞行物研究家,他的职责是观察UFO在一个N × N ×N的空间里的存在情况,这个空间被分成1 × 1 × 1的小区域,在这些小区域中会发生下列事件:
一些小区域会出现一些新的UFO;
一些UFO会在一些小区域消失;
Vasya 的上司会询问他某个区域的UFO有多少;
当Vasya开始观察UFO时,在整个区域内没有UFO。
输入:
第一行包含一个整数N(1 ≤ N ≤ 128).。区域的坐标是从0到N-1。
然后又多行描述,每行的第一个数是M。
如果M是1,接着就有四个整数,分别为 x (0 ≤ x < N), y (0 ≤ y < N), z (0 ≤ z < N), K (–20000 ≤ K ≤ 20000),x,y,z表示UFO出现的小区域,K表示UFO出现的数量。
如果M是2,接着就有六个整数 x1, y1, z1, x2, y2, z2 (0 ≤ x1 ≤ x2 < N, 0 ≤ y1 ≤ y2 < N, 0 ≤ z1 ≤ z2 < N),(x1,y1,z1)与(x2,y2,z2)分别表示所求区域的体对角线上的点。
如果M是3,那么Vasya就可以结束任务,回家睡觉了。
输出:
对Vasya上司的每句询问,都输出当前该区域UFO的数量,并且结果占一行。
本题可以利用三维树状数组,其实和一维树状数组,二维树状数组差不多,只是在更新(update)函数和求和(SUM)函数中相对一维、二维树状数组多了两、一个for循环而已,在最后的调用SUM函数是稍复杂些罢了。
代码如下:
//三维树状数组#include<stdio.h>#include<string.h>#define max 130int N;//三维树状数组(s[ ][ ][ ])用来储存出现UFO的数量,由于数量值较大,采用long long型long long s[max][max][max]={0};int lowbit(int x){ return x&(-x);}//更新函数,对树状数组某一元素(x,y,z)加减某一num值void update(int x,int y,int z,int num){ int i,j,k; for(i=x;i<=N;i+=lowbit(i)) { for(j=y;j<=N;j+=lowbit(j)) { for(k=z;k<=N;k+=lowbit(k)) s[i][j][k]+=num; } }}//求和函数,对树状数组某区域内的元素求和,注意数值较大,采用long long型long long SUM(int x,int y,int z){ int i,j,k; long long ans=0; for(i=x;i>0;i-=lowbit(i)) { for(j=y;j>0;j-=lowbit(j)) { for(k=z;k>0;k-=lowbit(k)) ans+=s[i][j][k]; } } return ans;}int main(){ long long sum; int x,y,z,x1,x2,y1,y2,z1,z2,M,K; scanf("%d",&N); while(scanf("%d",&M)&&M!=3) { if(M==1) { //输入要更新的元素以及更新的值 scanf("%d%d%d%d",&x,&y,&z,&K); /*对相应元素更新,注意树状数组是 从1开始储存的,输入的元素是从0开始的*/ update(x+1,y+1,z+1,K); } if(M==2) { //输入需要求和的区域 scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2); //求和 sum=SUM(x2+1,y2+1,z2+1)-SUM(x2+1,y2+1,z1)-SUM(x2+1,y1,z2+1)-SUM(x1,y2+1,z2+1)+SUM(x1,y1,z2+1)+SUM(x1,y2+1,z1)+SUM(x2+1,y1,z1)-SUM(x1,y1,z1); printf("%lld\n",sum); } } return 0;}
- UFOs
- UFOs
- Ural 1470. UFOs
- UFOs(树状数组)
- ural 1470 UFOs [树状数组]
- UFOs(三维树状数组)
- ural - 1470 - UFOs(树状数组)
- UFOs ——三维树状数组
- ural - 1470 - UFOs(三维树状数组)
- 英国国防部在5月15的新闻中承认UFO的存在(UFOs exist - it's official!)
- Elastix如何监听其它分机(图文)
- CSDN无语+恶心的待审核
- spring下载方法
- 开放录像服务器SDK接口(AnyChat Record Server SDK)
- 一个数组中,存在两个只出现一次的数字,其余的数字均出现两次。要求在时间复杂度o(n),空间复杂度为o(1)的情况下找出这两个数字
- UFOs
- OC没有实现方法警告
- poj-3253 Fence Repair(哈夫曼树)
- myeclipse 安装phpeclipse插件
- 无题
- ural1109(二分图模板)
- Android中View更新方法Invalidate()和postInvalidate()
- check style配置详解
- 在Red Hat linux下安装Eclipse,然后登陆到hadoop进行开发