UFOs

来源:互联网 发布:js后退事件 编辑:程序博客网 时间:2024/06/06 09:14

1470. UFOs

Time limit: 5.0 second
Memory limit: 64 MB
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.
At the moment when Vasya starts his observations there are no UFOs in the whole space.

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 x1y1z1x2y2z2 (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 (xyz) belonging to the volume: x1 ≤ x ≤ x2y1 ≤ y ≤ y2z1 ≤ z ≤z2.
  • If M is 3, it means that Vasya is tired and goes to sleep. This entry is always the last one.
The number of entries does not exceed 100002.

Output

For each query, output in a separate line the required number of UFOs.

Sample

inputoutput
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

感觉和一维的没什么太大区别,难度也不大,只要理解lowbit就可以轻松写出~~三维,只是比一维多了两个限制条件而已~~
/**三维树状数组**/#include <map>#include <set>#include <list>#include <queue>#include <stack>#include <cmath>#include <ctime>#include <vector>#include <bitset>#include <cstdio>#include <string>#include <numeric>#include <cstring>#include <cstdlib>#include <iostream>#include <algorithm>#include <functional>using namespace std;typedef long long  ll;typedef unsigned long long ull;int dx[4]= {-1,1,0,0};int dy[4]= {0,0,-1,1}; //up down left rightbool inmap(int x,int y,int n,int m){    if(x<1||x>n||y<1||y>m)return false;    return true;}int hashmap(int x,int y,int m){    return (x-1)*m+y;}#define eps 1e-8#define inf 0x7fffffff#define debug puts("BUG")#define lowbit(x) (-x)&x#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1#define read freopen("in.txt","r",stdin)#define write freopen("out.txt","w",stdout)#define maxn 200long long sum[maxn][maxn][maxn]= {0};int N;void change(int x,int y,int z,int add)//更新空间中的某一个点{    //debug;    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))            {                sum[i][j][k]+=add;                //debug;            }    // debug;}long long query(int x,int y,int z)//查询从(1,1,1)到(x,y,z)这块立方体区域中ufo的个数{    long long 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+=sum[i][j][k];    return ans;}int main(){    int M;    scanf("%d",&N);    while(scanf("%d",&M)!=EOF&&M!=3)    {        if(M==1)        {            int x,y,z,add;            scanf("%d%d%d%d",&x,&y,&z,&add);            change(x+1,y+1,z+1,add);        }        else if(M==2)        {            int x1,x2,y1,y2,z1,z2;            scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);            long long v= query(x2+1,y2+1,z2+1)                       - query(x2+1,y2+1,z1) - query(x2+1,y1,z2+1) - query(x1,y2+1,z2+1)                       + query(x1,y1,z2+1)   + query(x1,y2+1,z1)   + query(x2+1,y1,z1)                       - query(x1,y1,z1);/**关键点:要求任意区域的ufo个数,需要将多余的部分减去,多减去的部分重新加回来**/            printf("%lld\n",v);        }    }    return 0;}