HDU 4819 二维线段树
2016-06-25 13:37
253 查看
点击打开链接
题意:求区间的最大值与最小值的差和单点更新
思路:二维线段树的模版直接用就行,更新就是先找到x所在的区间然后再找y所在的区间,与一维的十分类似,看代码应该可以看懂吧~~~(/ □ \)
题意:求区间的最大值与最小值的差和单点更新
思路:二维线段树的模版直接用就行,更新就是先找到x所在的区间然后再找y所在的区间,与一维的十分类似,看代码应该可以看懂吧~~~(/ □ \)
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <iostream> #include <algorithm> using namespace std; typedef long long ll; const int inf=0x3f3f3f3f; const int maxn=810; int num1[maxn][maxn],num[maxn*4][maxn*4],Max[maxn*4][maxn*4],Min[maxn*4][maxn*4]; int n,max1,min1; void pushup(int nodex,int nodey){ Max[nodex][nodey]=max(Max[nodex][nodey<<1],Max[nodex][nodey<<1|1]); Min[nodex][nodey]=min(Min[nodex][nodey<<1],Min[nodex][nodey<<1|1]); } void buildtreey(int le,int ri,int x,int nodex,int nodey){ if(le==ri){ if(x!=-1) Max[nodex][nodey]=Min[nodex][nodey]=num1[x][le]; else{ Max[nodex][nodey]=max(Max[nodex<<1][nodey],Max[nodex<<1|1][nodey]); Min[nodex][nodey]=min(Min[nodex<<1][nodey],Min[nodex<<1|1][nodey]); } return ; } int t=(le+ri)>>1; buildtreey(le,t,x,nodex,nodey<<1); buildtreey(t+1,ri,x,nodex,nodey<<1|1); pushup(nodex,nodey); } void buildtreex(int le,int ri,int nodex){ if(le==ri){ buildtreey(1,n,le,nodex,1); return ; } int t=(le+ri)>>1; buildtreex(le,t,nodex<<1); buildtreex(t+1,ri,nodex<<1|1); buildtreey(1,n,-1,nodex,1); } void updatey(int le,int ri,int b,int add,int nodex,int nodey,int x){ if(le==ri){ if(x!=-1) Max[nodex][nodey]=Min[nodex][nodey]=add; else{ Max[nodex][nodey]=max(Max[nodex<<1][nodey],Max[nodex<<1|1][nodey]); Min[nodex][nodey]=min(Min[nodex<<1][nodey],Min[nodex<<1|1][nodey]); } return ; } int t=(le+ri)>>1; if(b<=t) updatey(le,t,b,add,nodex,nodey<<1,x); else updatey(t+1,ri,b,add,nodex,nodey<<1|1,x); pushup(nodex,nodey); } void updatex(int a,int b,int add,int le,int ri,int nodex){ if(le==ri){ updatey(1,n,b,add,nodex,1,le); return ; } int t=(le+ri)>>1; if(a<=t) updatex(a,b,add,le,t,nodex<<1); else updatex(a,b,add,t+1,ri,nodex<<1|1); updatey(1,n,b,add,nodex,1,-1); } void queryy(int b,int d,int le,int ri,int nodex,int nodey){ if(b<=le&&ri<=d){ max1=max(max1,Max[nodex][nodey]); min1=min(min1,Min[nodex][nodey]); return ; } int t=(le+ri)>>1; if(b<=t) queryy(b,d,le,t,nodex,nodey<<1); if(d>t) queryy(b,d,t+1,ri,nodex,nodey<<1|1); } void queryx(int a,int b,int c,int d,int le,int ri,int nodex){ if(a<=le&&ri<=c){ queryy(b,d,1,n,nodex,1); return ; } int t=(le+ri)>>1; if(a<=t) queryx(a,b,c,d,le,t,nodex<<1); if(c>t) queryx(a,b,c,d,t+1,ri,nodex<<1|1); } int main(){ int T,m,a,b,c,t=1; scanf("%d",&T); while(T--){ scanf("%d",&n); for(int i=1;i<=n;i++){ for(int j=1;j<=n;j++){ scanf("%d",&num1[i][j]); } } int xL,xR,yL,yR; buildtreex(1,n,1); scanf("%d",&m); printf("Case #%d:\n",t++); while(m--){ scanf("%d%d%d",&a,&b,&c); max1=-inf;min1=inf; c=(c+1)/2; xL=max(1,a-c+1),xR=min(n,a+c-1); yL=max(1,b-c+1),yR=min(n,b+c-1); queryx(xL,yL,xR,yR,1,n,1); int ans=(max1+min1)>>1; printf("%d\n",ans); updatex(a,b,ans,1,n,1); } } return 0; }
相关文章推荐
- php环境的搭建
- kindeditor编辑器图片水印
- Android 二维码 生成和识别(附Demo源码)
- SpringMVC总结帖
- Timer 异常
- 虚拟机安装centos 7
- sn 密钥注册
- 80老翁谈人生(15):考生被录取的过程是怎样的?
- 亲测——pycharm下运行第一个scrapy项目 ©seven_clear
- 异常聚合方法
- AndroidStudio初始化adb失败,无法显示Genymotion模拟器设备解决方法
- springboot-热部署
- WebForm控件多字段绑定
- JavaScript ----------- 组合继承
- Linux内核NAPI机制分析
- 让幻灯片暂停时自动黑屏或白屏的设置方法
- LCP287 LeetCode 287. Find the Duplicate Number
- 接口是否可以有实现方法
- Linux 内核中断内幕
- UITableView在Plain类型下,HeaderView和FooterView不悬浮和不停留的方法