hdu-3584 Cube---3D tree array + area update single point query

Topic link:

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

Topic meaning:

Given an N *N *N cube A whose elements are either 0 or 1. A[i, j, k] represents the value of the
i-th row, j-th column and k-th layer in the set.

First by A[i, j, k] = 0 (1 <= i, j, k <= N).

Given two operations:

1: Change A[i, j, k] to ! A[i, j, k].

2: Query the value of A[i, j, k].

Problem solving ideas:

Three-dimensional tree-like array simulation, using the principle of inclusion and exclusion

 

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<iostream>
 4 #include<algorithm>
 5 #include<string>
 6 #include<cmath>
 7 #include<set>
 8 #include<queue>
 9 #include<map>
10 #include<stack>
11 #include<vector>
12 #include<list>
13 #include<deque>
14 #include<sstream>
15 #include<cctype>
16 #define REP(i, n) for(int i = 0; i <(n); i++)
17 #define FOR(i, s, t) for(int i = (s); i < (t); i++)
18 #define MEM(a, x) memset(a, x, sizeof(a));
19 using namespace std;
20 typedef long long ll;
21 typedef unsigned long long ull;
22 const int maxn = 105;
23 const double eps = 1e-10;
24 const int INF = 1 << 30;
25 const int dir[4][2] = {1,0,0,1,0,-1,-1,0};
26 const double pi = 3.1415926535898;
27 int T, n, m, cases;
28 int tree[maxn][maxn][maxn];
29 int a[maxn][maxn][maxn];
30 int lowbit(int x)
31 {
32     return x&(-x);
33 }
34 int sum(int x, int y, int z)
35 {
36     int ans = 0;
37     for(int i = x; i <= n; i += lowbit(i))
38         for(int j = y; j <= n; j += lowbit(j))
39             for(int k = z; k <= n; k += lowbit(k))
40             ans += tree[i][j][k];
41     return ans;
42 }
43 void add(int x, int y, int z, int d)
44 {
45     for(int i = x; i > 0; i -= lowbit(i))
46         for(int j = y; j > 0; j -= lowbit(j))
47             for(int k = z; k > 0; k -= lowbit(k))
48             tree[i][j][k] += d;
49 }
50 int main()
51 {
52     while(cin >> n >> m)
53     {
54         MEM(tree, 0);
55         MEM(a, 0);
56         int t, x1, y1, z1, x2, y2, z2;
57         while(m--)
58         {
59             scanf("%d", &t);
60             if(t)
61             {
62                 scanf("%d%d%d%d%d%d", &x1, &y1, &z1, &x2, &y2, &z2);
63                 add(x2, y2, z2, 1);
64                 add(x2, y1 - 1, z2, 1);
65                 add(x1 - 1, y2, z2, 1);
66                 add(x2, y2, z1 - 1, 1);
67                 add(x1 - 1, y1 - 1, z2, 1);
68                 add(x1 - 1, y2, z1 - 1, 1);
69                 add(x2, y1 - 1, z1 - 1, 1);
70                 add(x1 - 1, y1 - 1, z1 - 1, 1);
71             }
72             else
73             {
74                 scanf("%d%d%d", &x1, &y1, &z1);
75                 cout<<(sum(x1, y1, z1)&1)<<endl;
76             }
77         }
78     }
79     return 0;
80 }

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324895967&siteId=291194637