题目链接:
http://acm.hdu.edu.cn/showproblem.php?pid=3584
题目大意:
给定一个N*N*N多维数据集A,其元素是0或是1。A[i,j,k]表示集合中第
i 行,第 j 列与第 k 层的值。
首先由A[i,j,k] = 0(1 <= i,j,k <= N)。
给定两个操作:
1:改变A[i,j,k]为!A[i,j,k]。
2:查询A[i,j,k]的值。
解题思路:
三维树状数组模拟,利用容斥原理
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<string>
#include<cmath>
#include<set>
#include<queue>
#include<map>
#include<stack>
#include<vector>
#include<list>
#include<deque>
#include<sstream>
#include<cctype>
#define REP(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, s, t) for(int i = (s); i < (t); i++)
#define MEM(a, x) memset(a, x, sizeof(a));
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const int maxn = ;
const double eps = 1e-;
const int INF = << ;
const int dir[][] = {,,,,,-,-,};
const double pi = 3.1415926535898;
int T, n, m, cases;
int tree[maxn][maxn][maxn];
int a[maxn][maxn][maxn];
int lowbit(int x)
{
return x&(-x);
}
int sum(int x, int y, int z)
{
int ans = ;
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))
ans += tree[i][j][k];
return ans;
}
void add(int x, int y, int z, int d)
{
for(int i = x; i > ; i -= lowbit(i))
for(int j = y; j > ; j -= lowbit(j))
for(int k = z; k > ; k -= lowbit(k))
tree[i][j][k] += d;
}
int main()
{
while(cin >> n >> m)
{
MEM(tree, );
MEM(a, );
int t, x1, y1, z1, x2, y2, z2;
while(m--)
{
scanf("%d", &t);
if(t)
{
scanf("%d%d%d%d%d%d", &x1, &y1, &z1, &x2, &y2, &z2);
add(x2, y2, z2, );
add(x2, y1 - , z2, );
add(x1 - , y2, z2, );
add(x2, y2, z1 - , );
add(x1 - , y1 - , z2, );
add(x1 - , y2, z1 - , );
add(x2, y1 - , z1 - , );
add(x1 - , y1 - , z1 - , );
}
else
{
scanf("%d%d%d", &x1, &y1, &z1);
cout<<(sum(x1, y1, z1)&)<<endl;
}
}
}
return ;
}