Cube

Cube

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 131072/65536 K (Java/Others)
Total Submission(s): 2614    Accepted Submission(s): 1314


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

 题意:

1.把空间上以(x1,y1,z1),(x2,y2,z2)为端点的区域所有的1变成0,0变成1.

2.求出(1,1,1)->(x,y,z)的区域的值.

转化一下思想,成为前缀和,然后&1,就知道当前位置是1还是0了.

详见代码.

 1 #include <iostream>
 2 #include <cstring>
 3 #define N 105
 4 using namespace std;
 5 
 6 int sum[N][N][N];
 7 
 8 int lowbit(int x){ return x&(-x); }
 9 
10 void update(int a,int b,int c){
11     for(int i = a;i<=N;i+=lowbit(i)){
12         for(int j = b;j<=N;j+=lowbit(j)){
13             for(int k = c; k<=N; k += lowbit(k)){
14                 sum[i][j][k]++;
15             }
16         }
17     }
18 }
19 
20 int query(int a,int b,int c){
21     int ans = 0;
22     for(int i = a;i>0;i-=lowbit(i)){
23         for(int j = b;j>0;j-=lowbit(j)){
24             for(int k = c;k>0;k-=lowbit(k)){
25                 ans += sum[i][j][k];
26             }
27         }
28     }
29     return ans;
30 }
31 
32 int main(){
33     int n,m;
34     while(cin>>n>>m){
35         memset(sum,0,sizeof(sum));
36         int p,x,y,z,x1,y1,z1;
37         while(m--){
38             cin>>p;
39             if(p){
40                 cin>>x>>y>>z>>x1>>y1>>z1;
41                 update(x,y,z);
42                 update(x,y,z1+1);
43                 update(x,y1+1,z);
44                 update(x1+1,y,z);
45                 update(x1+1,y1+1,z);
46                 update(x1+1,y,z1+1);
47                 update(x,y1+1,z1+1);
48                 update(x1+1,y1+1,z1+1);
49             }else{
50                 cin>>x>>y>>z;
51                 cout<<(query(x,y,z)&1)<<endl;
52             }
53         }
54     }
55     return 0;
56 }
原文地址:https://www.cnblogs.com/zllwxm123/p/9343765.html