首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

HDU 3584 Cube 三维立体空间树状数组

2012-09-23 
HDU3584Cube三维树状数组来源:http://acm.hdu.edu.cn/showproblem.php?pid3584题意:有一个立方体,初始每

HDU 3584 Cube 三维树状数组

来源:http://acm.hdu.edu.cn/showproblem.php?pid=3584

题意:有一个立方体,初始每个格子都为0,可以对格子操作,把0变为1,把1变为0,最后询问某个格子最后的值 是多少。

思路:三维树状数组的应用,插线问点。

代码:

#include <iostream>#include <cstdio>#include <string.h>using namespace std;const int N = 110;int num[N][N][N];int inline lowbit(int x){return x & (-x);}void inline update(int x,int y,int z,int add){int tempy = y,tempz = z;while(x > 0){   y = tempy;   while(y > 0){     z = tempz; while(z > 0){    num[x][y][z] += add;z -= lowbit(z); } y -= lowbit(y);   }   x -= lowbit(x);}}int inline sum(int x,int y,int z){int tempy = y,tempz = z, s = 0;while(x < N){   y = tempy;   while(y < N){     z = tempz; while(z < N){   s += num[x][y][z];   z += lowbit(z); } y += lowbit(y);   }   x += lowbit(x);}return s;}int main(){int n,m;while(scanf("%d%d",&n,&m) != EOF){   int x1,y1,z1,x2,y2,z2;   memset(num,0,sizeof(num));   int id;   while(m--){      scanf("%d",&id);  if(id == 1){    scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);update(x2,y2,z2,1);update(x1-1,y2,z2,-1);update(x2,y1-1,z2,-1);update(x2,y2,z1-1,-1);update(x1-1,y1-1,z2,1);update(x1-1,y2,z1-1,1);update(x2,y1-1,z1-1,1);update(x1-1,y1-1,z1-1,-1);  }  else{    scanf("%d%d%d",&x1,&y1,&z1);if(sum(x1,y1,z1) % 2)   printf("1\n");elseprintf("0\n");  }   }}return 0;}


热点排行