Cube

Problem Description

Given an N*N*N cube A, whose elements are either 0 or 1. A[i, j, k] means the number in the i-th row , j-th column and k-th layer. Initially we have A[i, j, k] = 0 (1 <= i, j, k <= N).
We define two operations, 1: “Not” operation that we change the A[i, j, k]=!A[i, j, k]. that means we change A[i, j, k] from 0->1,or 1->0. (x1<=i<=x2,y1<=j<=y2,z1<=k<=z2).
0: “Query” operation we want to get the value of A[i, j, k].

Input

Multi-cases.
First line contains N and M, M lines follow indicating the operation below.
Each operation contains an X, the type of operation. 1: “Not” operation and 0: “Query” operation.
If X is 1, following x1, y1, z1, x2, y2, z2.
If X is 0, following x, y, z.

Output

For each query output A[x, y, z] in one line. (1<=n<=100 sum of m <=10000)

Sample Input

2 5
1 1 1 1 1 1 1
0 1 1 1
1 1 1 1 2 2 2
0 1 1 1
0 2 2 2

Sample Output

1
0
1
題意:在一個(gè)三維長(zhǎng)方體里把0元素改成1或者把1改成0或者詢問(wèn)某個(gè)位置是0還是1.
分析:
    非樹(shù)狀數(shù)組莫屬,其他數(shù)據(jù)結(jié)構(gòu)都太帥了,用不著。
    先說(shuō)說(shuō)一維上的樹(shù)狀數(shù)組,什么問(wèn)題都得成最簡(jiǎn)單的開(kāi)始,理解后才能推廣到多維或復(fù)雜的。
    把對(duì)某個(gè)方體的修改操作看做是對(duì)某個(gè)方體的操作數(shù)+1,那一個(gè)位置操作數(shù)的奇偶性決定了它的值。
樹(shù)狀數(shù)組1位置是管1的,2位置是管1到2的,3是管3的,4是管1到4的......所以對(duì)一個(gè)區(qū)間操作,這個(gè)區(qū)間首先按樹(shù)狀數(shù)組的分區(qū)規(guī)則,會(huì)被分成若干個(gè)不相交的區(qū)間,在樹(shù)狀數(shù)組里對(duì)應(yīng)的就是若干不相交的小樹(shù)。
    比如對(duì)1到3操作,會(huì)分成分別對(duì)[1,2],[3,3]兩段的操作;對(duì)2到4的操作,可以分成[2,2],[3,3],[4,4],但是對(duì)這種情況,按這種方式去修改樹(shù)狀數(shù)組是麻煩的,可以轉(zhuǎn)化成分別對(duì)[1,4]操作,再對(duì)[1,1]操作,這樣[1,1]被修改了兩次,所以相當(dāng)于沒(méi)修改過(guò)(配合這題的奇偶性),也可處理成對(duì)[1,4]的操作數(shù)+1,再對(duì)[1,1]的操作數(shù)-1。
    知道每段的信息保存操作次數(shù),可以通過(guò)疊加就知道每個(gè)位置的操作數(shù)。
比如對(duì)1到3操作了一次,c[2] = 1(對(duì)應(yīng)[1,2]的區(qū)間),c[3] = 1(對(duì)應(yīng)[3,3]區(qū)間);再對(duì)1操作,則c[1] = 1(對(duì)應(yīng)[1,1]區(qū)間)。則1的操作數(shù)是c[1] + c[2]。
    到此還不理解的,建議最好看看樹(shù)狀數(shù)組的詳細(xì)結(jié)構(gòu),然后畫一畫就應(yīng)該清楚多了,表達(dá)上的不便請(qǐng)?jiān)彛@個(gè)算是比較水的,不過(guò)對(duì)樹(shù)狀數(shù)組的理解是有點(diǎn)用處的,還有就是在樹(shù)狀數(shù)組上對(duì)方塊的切割,例如對(duì)對(duì)2到4的操作需要處理成[1,4]操作,再對(duì)[1,1]操作 ,將這個(gè)操作推廣到3維。
    一維的是區(qū)間管區(qū)間,二維是方塊管理方塊,三維是方體管理方體,本質(zhì)跟一維一樣。
#include <stdio.h>
#include 
<stdlib.h>
#include 
<iostream>
#define maxn 110
using namespace std;
int c[maxn][maxn][maxn];
int n;
void set()
{
    
int i, j, k;
    
for (i = 0; i <= n; i++)
    {
        
for (j = 0; j <= n; j++)
        {
            
for (k = 0; k <= n; k++)
            {
                c[i][j][k] 
= 0;
            }
        }
    }
}
inline 
int lowbit(int x)
{
    
return x & (-x);
}
void update(int x, int y, int z)//分塊更新或操作 
{
    
int i, j, k;
    
for (i = x; i > 0; i -= lowbit(i))
    {
        
for (j = y; j > 0; j -= lowbit(j))
        {
            
for (k = z; k > 0; k -= lowbit(k))
            {
                c[i][j][k]
++;
            }
        }
    }
}
int sum(int x, int y, int z)//向上疊加每個(gè)父節(jié)點(diǎn)的操作次數(shù) 
{
    
int i, j, k, var = 0;
    
for (i = x; i <= n; i += lowbit(i))
    {
        
for (j = y; j <= n; j += lowbit(j))
        {
            
for (k = z; k <= n; k += lowbit(k))
            {
                var 
+= c[i][j][k];
            }
        }
    }
    
return var;
}
int main()
{
    
int m, op, x1, y1, z1, x2, y2, z2, ans;
    
while (scanf("%d%d"&n, &m) - EOF)
    {
        
set();
        
while (m--)
        {
            scanf(
"%d%d%d%d"&op, &x1, &y1, &z1);
            
if (op == 1)
            {
                scanf(
"%d%d%d"&x2, &y2, &z2);
                update(x2, y2, z2);
                update(x2, y1 
- 1, z2);
                update(x1 
- 1, y2, z2);
                update(x1 
- 1, y1 - 1, z2);
                
//--------
                update(x2, y2, z1 - 1);
                update(x2, y1 
- 1, z1 - 1);
                update(x1 
- 1, y2, z1 - 1);
                update(x1 
- 1, y1 - 1, z1 - 1);
            }
            
else
            {
                ans 
= sum(x1, y1, z1);
                printf(
"%d\n", ans % 2);
            }
        }
    }
    
return 0;
}
/*
2 100
1 2 2 2 2 2 2
0 1 1 1


1
2 10
1 2 1 2 2
Q 2 2
1 2 1 2 1
Q 1 1
1 1 1 2 1
1 1 2 1 2
1 1 1 2 2
Q 1 1
1 1 1 2 1
Q 2 1
*/