Topic Links:
http://acm.hdu.edu.cn/showproblem.php?pid=3584
Main topic:
Given an n*N*N Cube A, its elements are 0 or 1. A[i,j,k] represents the value of row I, column J and level K in the collection.
First by a[i,j,k] = 0 (1 <= i,j,k <= N).
Given two actions:
1: Change A[i,j,k] for! A[I,J,K].
2: The value of Query A[i,j,k].
Ideas:
Three-dimensional tree array interval update, single point query. When the update interval (a, b) is added 1 at a and b+1, the front represents an increase of 1 and the rear is
Offset plus 1 operation, the last query, with query (x)% 2 is the value of a single point (0 or 1). Three-dimensional interval update, to pay attention to the integration of the interval will
Repeat, so use the repulsion principle, update the interval (X1,Y1,Z1) ~ (X2,y2, Z2), it is necessary to use the repulsion principle. More New as follows:
Update (X1,Y1,Z1); Update (x1,y1,z2+1); Update (X1,Y2+1,Z1); Update (x1,y2+1,z2+1);
Update (X2+1,Y1,Z1); Update (x2+1,y1,z2+1); Update (X2+1,Y2+1,Z1); Update (x2+1,y2+1,z2+1);
AC Code:
#include <iostream> #include <algorithm> #include <cstdio> #include <cstring>using namespace std;const int maxn = 110;int tree[maxn][maxn][maxn],n,m;int lowbit (int i) {return I & (-i);} void Update (int x,int y,int z) {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)) tree[i][j][k]++;} int Query (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 + = Tree[i][j][k]; return sum;} int main () {while (CIN >> N >> M) {memset (tree,0,sizeof (Tree)); int op,x1,y1,z1,x2,y2,z2; while (m--) {cin >> op; if (op = = 0) {cin >> x1 >> Y1 >> Z1; cout << Query (x1,y1,z1)% 2 << Endl; } else {cin >> x1 >> y1 >> z1 >> x2 >> y2 >&G T Z2; Update (X1,Y1,Z1); Update (x1,y1,z2+1); Update (X1,Y2+1,Z1); Update (x1,y2+1,z2+1); Update (X2+1,Y1,Z1); Update (x2+1,y1,z2+1); Update (X2+1,Y2+1,Z1); Update (x2+1,y2+1,z2+1); }}} return 0;}
HDU3584 Cube "tree-like Array" "three-dimensional"