hdu-3584 Cube---三维树状数组+区域更新单点查询

题目链接:

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]的值。

解题思路:

三维树状数组模拟,利用容斥原理

 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 }

原文地址:https://www.cnblogs.com/fzl194/p/8955263.html

时间: 2024-11-08 21:56:09

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

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

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 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便为骑上他的"小飞鸽"

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

HDU3584 Cube【树状数组】【三维】

题目链接: 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]的值. 思路: 三维树状数组区间更新.单点查询.更新区间(a,b)时,在 a 和 b+1 处都加1,前边表