Give a three-dimensional array n * n, which is initially 0. Each operation has two operations:
1. Flip (x1, Y1, Z1)-> (X2, Y2, Z2)
0. query a [x] [y] [Z] (a is the array)
Solution: Number of maintenance operations in a tree array. an even number of operations is equivalent to no operation.
Update each update at eight locations:
. 8 binary numbers: 000,001,010,011,100,101,110,111 (I pushed it from two dimensions)
In fact, you don't need to use-1, because it will change parity.
Code:
#include <iostream>#include <cstdio>#include <cstring>#include <cstdlib>#include <cmath>#include <algorithm>using namespace std;int c[105][105][105];int n;int lowbit(int x) { return x & (-x); }void modify(int x,int y,int z,int val){ 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)) c[i][j][k] += val;}int getsum(int x,int y,int z){ int sum = 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)) sum += c[i][j][k]; return sum;}int main(){ int q,op; int x,y,z,x1,y1,z1,x2,y2,z2; while(scanf("%d%d",&n,&q)!=EOF) { memset(c,0,sizeof(c)); while(q--) { scanf("%d",&op); if(op == 1) { scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2); modify(x1,y1,z1,1); modify(x1,y1,z2+1,-1); modify(x1,y2+1,z1,-1); modify(x1,y2+1,z2+1,1); modify(x2+1,y1,z1,-1); modify(x2+1,y1,z2+1,1); modify(x2+1,y2+1,z1,1); modify(x2+1,y2+1,z2+1,-1); } else { scanf("%d%d%d",&x,&y,&z); int sum = getsum(x,y,z); printf("%d\n",sum%2); } } } return 0;}View code
HDU 3584 cube-Three-dimensional tree Array