HDU 3584 Cube(三维树状数组)
2013-12-03 12:16
423 查看
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3584
这个题目树状数组比较明显
由于只是两种变化,所以只要统计变化的次数就可以了
每次变化的是一个区间,更新的时候就按照点一样的更新方法
求和时求某个点的,其实就是求变化次数
任何一段在往下加的时候总能加到你更新的那个位置,如果加不到你也一定会加到你的某个
上司,没影响的也会加,不过更新的时候考虑到这个问题,把重复位置再次更新了一次,因为
奇偶变化,都+就行了!
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 105;
int tree[maxn][maxn][maxn];
int n,m;
int lowbit(int x){
return (-x)&x;
}
int sum(int i,int j,int k){
int ans=0,now,then;
while(i>0){
now=j;
while(now>0){
then=k;
while(then>0){
ans+=tree[i][now][then];
then-=lowbit(then);
}
now-=lowbit(now);
}
i-=lowbit(i);
}
return ans;
}
int update(int i,int j,int k){
int now,then;
while(i<=n){
now=j;
while(now<=n){
then=k;
while(then<=n){
tree[i][now][then]++;
then+=lowbit(then);
}
now+=lowbit(now);
}
i+=lowbit(i);
}
return 0;
}
int main(){
int i,j,k;
int no,x1,y1,z1,x2,y2,z2;
while(scanf("%d%d",&n,&m)!=EOF){
memset(tree,0,sizeof(tree));
for(i=0;i<m;i++){
scanf("%d",&no);
if(1==no){
scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);
update(x1,y1,z1);
update(x1,y1,z2+1);
update(x1,y2+1,z1);
update(x2+1,y1,z2+1);
update(x2+1,y2+1,z1);
update(x1,y2+1,z2+1);
update(x2+1,y1,z1);
update(x2+1,y2+1,z2+1);
}else{
scanf("%d%d%d",&x1,&y1,&z1);
printf("%d\n",sum(x1,y1,z1)%2);
}
}
}
return 0;
}
这个题目树状数组比较明显
由于只是两种变化,所以只要统计变化的次数就可以了
每次变化的是一个区间,更新的时候就按照点一样的更新方法
求和时求某个点的,其实就是求变化次数
任何一段在往下加的时候总能加到你更新的那个位置,如果加不到你也一定会加到你的某个
上司,没影响的也会加,不过更新的时候考虑到这个问题,把重复位置再次更新了一次,因为
奇偶变化,都+就行了!
#include <string.h>
#include <stdio.h>
#include <algorithm>
#include <iostream>
using namespace std;
const int maxn = 105;
int tree[maxn][maxn][maxn];
int n,m;
int lowbit(int x){
return (-x)&x;
}
int sum(int i,int j,int k){
int ans=0,now,then;
while(i>0){
now=j;
while(now>0){
then=k;
while(then>0){
ans+=tree[i][now][then];
then-=lowbit(then);
}
now-=lowbit(now);
}
i-=lowbit(i);
}
return ans;
}
int update(int i,int j,int k){
int now,then;
while(i<=n){
now=j;
while(now<=n){
then=k;
while(then<=n){
tree[i][now][then]++;
then+=lowbit(then);
}
now+=lowbit(now);
}
i+=lowbit(i);
}
return 0;
}
int main(){
int i,j,k;
int no,x1,y1,z1,x2,y2,z2;
while(scanf("%d%d",&n,&m)!=EOF){
memset(tree,0,sizeof(tree));
for(i=0;i<m;i++){
scanf("%d",&no);
if(1==no){
scanf("%d%d%d%d%d%d",&x1,&y1,&z1,&x2,&y2,&z2);
update(x1,y1,z1);
update(x1,y1,z2+1);
update(x1,y2+1,z1);
update(x2+1,y1,z2+1);
update(x2+1,y2+1,z1);
update(x1,y2+1,z2+1);
update(x2+1,y1,z1);
update(x2+1,y2+1,z2+1);
}else{
scanf("%d%d%d",&x1,&y1,&z1);
printf("%d\n",sum(x1,y1,z1)%2);
}
}
}
return 0;
}
相关文章推荐
- HDU 1405
- HDU 1297
- hdu 1205
- hdu 2087
- hdu 1016
- HDU 1622 Trees On The Level
- HDU 1063 Exponentiation
- hdu 1202
- HDU 4332(状态压缩dp+矩阵连乘)
- HDU 3065(ac自动机)
- HDU 4714 Tree2cycle
- HDU 4709 Herding
- HDU 4717 The Moving Points
- HDU 4722 Good Numbers
- hdu 1007 Quoit Design
- hdu 1241 Oil Deposits
- 字符串匹配自动机
- HDU 3003
- HDU 3006
- HDU 3008