hdu5443 The Water Problem(线段树)
2015-09-14 16:20
239 查看
求区间最大值 写了一颗线段树
比赛的时候第一眼就看见了这个 - -
但是手速太慢了还是写了8分多钟
比赛的时候第一眼就看见了这个 - -
但是手速太慢了还是写了8分多钟
#include <bits/stdc++.h> #define lson num<<1 #define rson num<<1|1 #define gl l,m,lson #define gr m+1,r,rson #define PARA int l=1,int r=n,int num=1 using namespace std; const int MAXN = 1e6+10; int n; struct SegTree { int st[MAXN<<2]; void init(PARA) { if(l==r) scanf("%d",&st[num]); else { int m=l+r>>1; init(gl); init(gr); st[num]=max(st[lson],st[rson]); } } int query(int a,int b,PARA) { if(a<=l&&r<=b) return st[num]; else { int m=l+r>>1; if(b<=m) return query(a,b,gl); else if(a>m) return query(a,b,gr); else return max(query(a,b,gl),query(a,b,gr)); } } }soul; int main() { int T,m; scanf("%d",&T); while(T--) { scanf("%d",&n); soul.init(); int a,b; scanf("%d",&m); while(m--) { scanf("%d%d",&a,&b); printf("%d\n",soul.query(a,b)); } } return 0; }
相关文章推荐
- openwrt 端口转发
- linux下网卡配vlan
- Linux脚本程序
- RabbitMQ三种Exchange模式(fanout,direct,topic)介绍
- MVC图片验证
- java基础教程视频
- css3图片旋转
- 09.14 类,类型
- IOS网络第二天 - 05-黑酷-NSXMLparser解析(略)
- hihocoder Hamiltonian Cycle(记忆化搜索+剪枝)
- 华为OJ平台题目:火车进站
- Java集合面试题小结
- Ado.net参数化插入数据时null值自动转成default导致插入出错问题-----解决方法
- IOS网络第二天 - 04-黑酷-GDataXML 解析
- 浅析C++中的函数与指针
- P2P、P2C 、O2O 、B2C、b2b、C2C、I2CF...各种互联网名词详解
- IOS中延时执行的几种方式的比较和汇总
- 第2周SHH数据结构—【项目3-体验复杂度(1)】
- 通过eclipse启动tomcat设置JAVA_OPTS失败的解决方案
- 我学shell编程2