UFOs(树状数组)
来源:互联网 发布:像韩国人的淘宝模特 编辑:程序博客网 时间:2024/06/05 06:24
Description
Vasya is a ufologist and his duties include observing Unidentified Flying Objects (UFOs) in the part of space bounded by a cube N × N ×N. The cube is divided into cubic sectors 1 × 1 × 1. During the observation, the following events may happen:
- 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
The first line contains an integer N (1 ≤ N ≤ 128). The coordinates of sectors are integers from 0 to N–1.
Then there are entries describing events, one entry per line. Each entry starts with a number M.
- 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
For each query, output in a separate line the required number of UFOs.
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
解题思路
这题是三维空间上的,看到userluoxuan用三维树状数组做的,但是三维最后求和的式子太麻烦了,还是二维间接求简单些^_^#
就是将不同的z不同的层单独看,更新时更新z相等这一层,最后求和时求出范围内的每一层再相加。
AC代码
#include<stdio.h>#include<string.h>#define maxn 130int xt[maxn][maxn][maxn] ;int add, z, n ;int lowbit( int x ){ return x&(-x) ;}void update(int x, int y ){ int i, j ; for(i=x; i<=maxn; i+=lowbit(i)) for(j=y; j<=maxn; j+=lowbit(j)) { xt[z][i][j] += add ; if( xt[z][i][j] < 0 ) xt[z][i][j] = 0 ; //注意更新,求和时带上第三维 }}int get_sum( int x , int y ){ int i, ans=0 ; for( i=x; i>0; i-=lowbit(i) ) for(int j=y; j>0; j-=lowbit(j) ) { ans += xt[z][i][j] ; } return ans ;}int main(){ int x, y, a; int x1, y1, z1, x2, y2, z2; int ans; scanf("%d", &n); memset(xt, 0, sizeof(xt)); while( scanf("%d", &a ) && a!=3 ) { ans=0; if( a==1 ) { scanf("%d%d%d%d", &x, &y, &z, &add ); update(x+1, y+1); } else if( a==2) { scanf("%d%d%d", &x1, &y1, &z1 ); scanf("%d%d%d", &x2, &y2, &z2 ); for(z=z1; z<=z2; z++) ans += get_sum(x2+1,y2+1) - get_sum(x1,y2+1) - get_sum(x2+1,y1) + get_sum(x1,y1); printf("%d\n",ans); } } return 0;}
0 0
- UFOs(树状数组)
- UFOs(三维树状数组)
- ural - 1470 - UFOs(树状数组)
- ural - 1470 - UFOs(三维树状数组)
- ural 1470 UFOs [树状数组]
- UFOs ——三维树状数组
- UFOs
- UFOs
- (转)树状数组
- 树状数组(转载)
- Stars(树状数组)
- 树状数组(interval)
- (转)树状数组
- 树状数组(2)
- 树状数组(3)
- 树状数组(4)
- 树状数组(5)
- 树状数组(6)
- POJ 2243 Knight Moves
- 利用pycharm的Deployment功能映射本地和远程代码(同步开发)
- 继承Application实现Android数据共享
- 搭建跨订阅的虚拟网络到虚拟网络
- C语言Log输出收藏(转)
- UFOs(树状数组)
- 经纬财富:菏泽如何稳健做非农
- 常用 U-boot命令详解(转载)
- Android 中的Context (Application Context 和Activity Context)
- 链表实现的堆栈程序(仅供自己学习使用)
- python的函数、模块、包
- sharepoint 2013 更改搜索服务器配置
- 将git的变更merge到svn或将svn的变更merge到git.
- 安卓activity捕获返回按钮关闭应用的方法