POJ 2155 Matrix ( 二维树状数组 ) || HDU 3584 Cube ( 三维树状数组 )

2024-05-29 18:58

本文主要是介绍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) ;}elseprintf("%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的坐标统统减一就 okadd(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) ;}elseprintf("%d\n",get_su(x,y,z)%2) ;}}return 0 ;
}



 

这篇关于POJ 2155 Matrix ( 二维树状数组 ) || HDU 3584 Cube ( 三维树状数组 )的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1014369

相关文章

hdu1240、hdu1253(三维搜索题)

1、从后往前输入,(x,y,z); 2、从下往上输入,(y , z, x); 3、从左往右输入,(z,x,y); hdu1240代码如下: #include<iostream>#include<algorithm>#include<string>#include<stack>#include<queue>#include<map>#include<stdio.h>#inc

poj2576(二维背包)

题意:n个人分成两组,两组人数只差小于1 , 并且体重只差最小 对于人数要求恰好装满,对于体重要求尽量多,一开始没做出来,看了下解题,按照自己的感觉写,然后a了 状态转移方程:dp[i][j] = max(dp[i][j],dp[i-1][j-c[k]]+c[k]);其中i表示人数,j表示背包容量,k表示输入的体重的 代码如下: #include<iostream>#include<

hdu2159(二维背包)

这是我的第一道二维背包题,没想到自己一下子就A了,但是代码写的比较乱,下面的代码是我有重新修改的 状态转移:dp[i][j] = max(dp[i][j], dp[i-1][j-c[z]]+v[z]); 其中dp[i][j]表示,打了i个怪物,消耗j的耐力值,所得到的最大经验值 代码如下: #include<iostream>#include<algorithm>#include<

hdu2241(二分+合并数组)

题意:判断是否存在a+b+c = x,a,b,c分别属于集合A,B,C 如果用暴力会超时,所以这里用到了数组合并,将b,c数组合并成d,d数组存的是b,c数组元素的和,然后对d数组进行二分就可以了 代码如下(附注释): #include<iostream>#include<algorithm>#include<cstring>#include<stack>#include<que

hdu4826(三维DP)

这是一个百度之星的资格赛第四题 题目链接:http://acm.hdu.edu.cn/contests/contest_showproblem.php?pid=1004&cid=500 题意:从左上角的点到右上角的点,每个点只能走一遍,走的方向有三个:向上,向下,向右,求最大值。 咋一看像搜索题,先暴搜,TLE,然后剪枝,还是TLE.然后我就改方法,用DP来做,这题和普通dp相比,多个个向上

usaco 1.3 Mixing Milk (结构体排序 qsort) and hdu 2020(sort)

到了这题学会了结构体排序 于是回去修改了 1.2 milking cows 的算法~ 结构体排序核心: 1.结构体定义 struct Milk{int price;int milks;}milk[5000]; 2.自定义的比较函数,若返回值为正,qsort 函数判定a>b ;为负,a<b;为0,a==b; int milkcmp(const void *va,c

poj 3974 and hdu 3068 最长回文串的O(n)解法(Manacher算法)

求一段字符串中的最长回文串。 因为数据量比较大,用原来的O(n^2)会爆。 小白上的O(n^2)解法代码:TLE啦~ #include<stdio.h>#include<string.h>const int Maxn = 1000000;char s[Maxn];int main(){char e[] = {"END"};while(scanf("%s", s) != EO

hdu 2093 考试排名(sscanf)

模拟题。 直接从教程里拉解析。 因为表格里的数据格式不统一。有时候有"()",有时候又没有。而它也不会给我们提示。 这种情况下,就只能它它们统一看作字符串来处理了。现在就请出我们的主角sscanf()! sscanf 语法: #include int sscanf( const char *buffer, const char *format, ... ); 函数sscanf()和

hdu 2602 and poj 3624(01背包)

01背包的模板题。 hdu2602代码: #include<stdio.h>#include<string.h>const int MaxN = 1001;int max(int a, int b){return a > b ? a : b;}int w[MaxN];int v[MaxN];int dp[MaxN];int main(){int T;int N, V;s

poj 1511 Invitation Cards(spfa最短路)

题意是给你点与点之间的距离,求来回到点1的最短路中的边权和。 因为边很大,不能用原来的dijkstra什么的,所以用spfa来做。并且注意要用long long int 来存储。 稍微改了一下学长的模板。 stack stl 实现代码: #include<stdio.h>#include<stack>using namespace std;const int M