HDU - 3584 Cube (三维树状数组 + 区间改动 + 单点求值)

HDU - 3584

Cube

Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u

Submit Status

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 
/*
Author: 2486
Memory: 5944 KB		Time: 202 MS
Language: G++		Result: Accepted
VJ RunId: 4328542		Real RunId: 14413386
*/
//三维的与二维的一样,而二维的与一维的一样
//具体解释。本博客中解答了题目这么做的原理
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <queue>
using namespace std;
const int MAXN = 100 + 5;
int C[MAXN][MAXN][MAXN];
int N, M;

int lowbit(int x) {
    return x & (-x);
}

void add(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)) {
                C[i][j][k] ++;
            }
        }
    }
}
int query(int x, int y, int z) {
    int ret = 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)) {
                ret += C[i][j][k];
            }
        }
    }
    return ret & 1;
}
int X, x, y, z, x1, y1, z1, x2, y2, z2;
int main() {
    while(~ scanf("%d%d", &N, &M)) {
        memset(C, 0, sizeof(C));
        while(M --) {
            scanf("%d", &X);
            if(X) {
                scanf("%d%d%d%d%d%d", &x1, &y1, &z1, &x2, &y2, &z2);
                x2 ++;
                y2 ++;
                z2 ++;
                add(x1, y1, z1);
                add(x1, y2, z1);
                add(x1, y1, z2);
                add(x1, y2, z2);
                add(x2, y1, z1);
                add(x2, y2, z1);
                add(x2, y1, z2);
                add(x2, y2, z2);
            } else {
                scanf("%d%d%d", &x, &y, &z);
                printf("%d\n", query(x, y, z));
            }
        }
    }
    return 0;
}
时间: 2024-10-16 06:31:13

HDU - 3584 Cube (三维树状数组 + 区间改动 + 单点求值)的相关文章

HDU - 3584 Cube (三维树状数组 + 区间修改 + 单点求值)

HDU - 3584 Cube Time Limit: 1000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status 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. I

POJ - 2155 Matrix (二维树状数组 + 区间改动 + 单点求值 或者 二维线段树 + 区间更新 + 单点求值)

POJ - 2155 Matrix Time Limit: 3000MS   Memory Limit: 65536KB   64bit IO Format: %I64d & %I64u Submit Status Description Given an N*N matrix A, whose elements are either 0 or 1. A[i, j] means the number in the i-th row and j-th column. Initially we ha

HDU 3584 Cube --三维树状数组

题意:给一个三维数组n*n*n,初始都为0,每次有两个操作: 1. 翻转(x1,y1,z1) -> (x2,y2,z2) 0. 查询A[x][y][z] (A为该数组) 解法:树状数组维护操作次数,一个数被操作偶数次则相当于没被操作. 每次更新时在8个位置更新: .相当于8个二进制数:000,001,010,011,100,101,110,111. (我是由二维推过来的) 其实不用有的为-1,直接1也行,因为反正会改变奇偶性. 代码: #include <iostream> #inclu

HDU - 1556 Color the ball (一维树状数组 + 区间修改 + 单点求值)

HDU - 1556 Color the ball Time Limit: 3000MS   Memory Limit: 32768KB   64bit IO Format: %I64d & %I64u Submit Status Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"牌电动车从气球a开始到气球b依次给每个气球涂一次颜色.但是N次以后lele已经忘记了第I个气

hdu 4031 Attack(树状数组区间更新单点求值&amp;暴力)

Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1890    Accepted Submission(s): 554 Problem Description Today is the 10th Annual of "September 11 attacks", the Al Qaeda is about to

hdu 4031 Attack(树状数组区间更新单点求值&amp;amp;暴力)

Attack Time Limit: 5000/3000 MS (Java/Others)    Memory Limit: 65768/65768 K (Java/Others) Total Submission(s): 1890    Accepted Submission(s): 554 Problem Description Today is the 10th Annual of "September 11 attacks", the Al Qaeda is about to

【树状数组区间修改单点查询】HDU 4031 Attack

http://acm.hdu.edu.cn/showproblem.php?pid=4031 [题意] 有一个长为n的长城,进行q次操作,d为防护罩的冷却时间,Attack表示区间a-b的墙将在1秒后受到攻击, 询问表示计算第a块墙受到攻击的次数,被防护罩抵消的不算 [思路] 总的攻击次数-防护罩抵消的次数 总的攻击次数可以树状数组维护 防护罩抵消的模拟 [AC] 1 #include<bits/stdc++.h> 2 using namespace std; 3 typedef long l

【树状数组区间修改单点查询+分组】HDU 4267 A Simple Problem with Integers

http://acm.hdu.edu.cn/showproblem.php?pid=4267 [思路] 树状数组的区间修改:在区间[a, b]内更新+x就在a的位置+x. 然后在b+1的位置-x 树状数组的单点查询:求某点a的值就是求数组中1~a的和. (i-a)%k==0把区间分隔开了,不能直接套用树状数组的区间修改单点查询 这道题的K很小,所以可以枚举k,对于每个k,建立k个树状数组,所以一共建立55棵树 所以就可以多建几棵树..然后就可以转换为成段更新了~~ [AC] 1 #include

HDU 1556-Color the ball(树状数组-区间修改 单点查询)

Color the ball Time Limit: 9000/3000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 15491    Accepted Submission(s): 7731 Problem Description N个气球排成一排,从左到右依次编号为1,2,3....N.每次给定2个整数a b(a <= b),lele便为骑上他的"小飞鸽"