POJ 2155 Matrix ( 二维树状数组 ) || HDU 3584 Cube ( 三维树状数组 )
做题感悟:这题只要把一维的树状数组扩展到二维就可以了。
解题思路:树状数组插线问点:先简化一下,如果是一维的树状数组的插线问点让区间 [ a,b ] 同时加 x ,可以先让 [ 1,b ] + 1 ,再让 [ 1 ,a-1 ] -1 ,跟前缀和一样这样区间 [ a,b ] 就实现了 +1 ,但这时数组 c [ ] 代表是它管辖范围内每个点的值为 c [ ] ( 此时c 数组的意义已经变了,不再代表所管辖区间的和) .
很明显如果查询某个点的值需要取所有管辖它的区间的和。好了,现在我们可以考虑这个题了,假设 改变矩形 ( x1 , y1 ) ~ ( x2 , y2 ) 就可以让 ( 1 , 1 ) ~ ( x2 , y2 ) 同时 + 1 ,让( 1 , 1 ) ~ ( x1-1 , y1-1 ) 同时 - 1 ,让( 1 , 1 ) ~ ( x1-1 , y2) 同时 - 1 ,让( 1 , 1 ) ~ ( x2 , y1-1 ) 同时 - 1,查询时和一维数组类似,如果是偶数代表 0 奇数代表 1 . ( 这里有一个小规律,总是让出现 x1 , y1 时减一就可以了)
代码(POJ 2155):
-
#include<stdio.h>
-
#include<iostream>
-
#include<map>
-
#include<stack>
-
#include<string>
-
#include<string.h>
-
#include<stdlib.h>
-
#include<math.h>
-
#include<vector>
-
#include<queue>
-
#include<algorithm>
-
using namespace std ;
-
#define LEN sizeof(struct node)
-
#define lld __int64
-
const double PI = 3.1415926535898 ;
-
const int INF = 99999999 ;
-
const double esp = 1e-8 ;
-
const long long mod= 1000 ;
-
const int MX = 1005 ;
-
int n ;
-
int c[MX][MX] ;
-
int lowbit(int x)
-
{
-
return x & (-x) ;
-
}
-
int get_su(int x,int y) // 求值
-
{
-
int ans=0,y1 ;
-
while(x<=n)
-
{
-
y1=y ;
-
while(y1<=n)
-
{
-
ans+=c[x][y1] ;
-
y1+=lowbit(y1) ;
-
}
-
x+=lowbit(x) ;
-
}
-
return ans ;
-
}
-
void add(int x,int y,int num) // 更新
-
{
-
int y1 ;
-
while(x>0)
-
{
-
y1=y ;
-
while(y1>0)
-
{
-
c[x][y1]+=num ;
-
y1-=lowbit(y1) ;
-
}
-
x-=lowbit(x) ;
-
}
-
}
-
int main()
-
{
-
char s[10] ;
-
int Tx,m,x,y,x1,y1 ;
-
scanf("%d",&Tx) ;
-
while(Tx--)
-
{
-
scanf("%d%d",&n,&m) ;
-
memset(c,0,sizeof(c)) ;
-
for(int i=1 ;i<=m ;i++)
-
{
-
scanf("%s",s) ;
-
scanf("%d%d",&x,&y) ;
-
if(s[0]=='C')
-
{
-
scanf("%d%d",&x1,&y1) ;
-
add(x1,y1,1) ;
-
add(x-1,y-1,1) ;
-
add(x1,y-1,-1) ;
-
add(x-1,y1,-1) ;
-
}
-
else
-
printf("%d\n",get_su(x,y)%2) ;
-
}
-
if(Tx) printf("\n") ;
-
}
-
return 0 ;
-
}
HDU 3584 和二维的差不多,但是更新很麻烦,不多说了看代码。
代码:
-
#include<stdio.h>
-
#include<iostream>
-
#include<map>
-
#include<stack>
-
#include<string>
-
#include<string.h>
-
#include<stdlib.h>
-
#include<math.h>
-
#include<vector>
-
#include<queue>
-
#include<algorithm>
-
using namespace std ;
-
#define LEN sizeof(struct node)
-
#define lld __int64
-
const double PI = 3.1415926535898 ;
-
const int INF = 99999999 ;
-
const double esp = 1e-8 ;
-
const long long mod= 1000 ;
-
const int MX = 105 ;
-
int n ;
-
int c[MX][MX][MX] ;
-
int lowbit(int x)
-
{
-
return x&(-x) ;
-
}
-
int get_su(int x,int y,int z)// 求和
-
{
-
int y1,z1,ans=0 ;
-
while(x<=n)
-
{
-
y1=y ;
-
while(y1<=n)
-
{
-
z1=z ;
-
while(z1<=n)
-
{
-
ans+=c[x][y1][z1] ;
-
z1+=lowbit(z1) ;
-
}
-
y1+=lowbit(y1) ;
-
}
-
x+=lowbit(x) ;
-
}
-
return ans ;
-
}
-
void add(int x,int y,int z,int num)// 更新
-
{
-
int y1,z1 ;
-
while(x>0)
-
{
-
y1=y ;
-
while(y1>0)
-
{
-
z1=z ;
-
while(z1>0)
-
{
-
c[x][y1][z1]+=num ;
-
z1-=lowbit(z1) ;
-
}
-
y1-=lowbit(y1) ;
-
}
-
x-=lowbit(x) ;
-
}
-
}
-
int main()
-
{
-
int m,x,y,z,x1,y1,z1,ch ;
-
while(~scanf("%d%d",&n,&m))
-
{
-
memset(c,0,sizeof(c)) ;
-
for(int i=0 ;i<m ;i++)
-
{
-
scanf("%d%d%d%d",&ch,&x,&y,&z) ;
-
if(ch)
-
{
-
scanf("%d%d%d",&x1,&y1,&z1) ;
-
add(x1,y1,z1,1) ; // 只要将 出现x ,y ,z的坐标统统减一就 ok
-
add(x-1,y1,z-1,1) ; // 其实可以全更新 1 不用更新 -1 道理一样
-
add(x-1,y1,z1,-1) ;
-
add(x1,y1,z-1,-1) ;
-
-
add(x1,y-1,z1,-1) ;
-
add(x-1,y-1,z-1,-1) ;
-
add(x-1,y-1,z1,1) ;
-
add(x1,y-1,z-1,1) ;
-
}
-
else
-
printf("%d\n",get_su(x,y,z)%2) ;
-
}
-
}
-
return 0 ;
-
}
文章来源: blog.csdn.net,作者:Linux猿,版权归原作者所有,如需转载,请联系作者。
原文链接:blog.csdn.net/nyist_zxp/article/details/22882383
- 点赞
- 收藏
- 关注作者
评论(0)