51nod 1174 区间中最大的数
来源:互联网 发布:java视频教程下载 编辑:程序博客网 时间:2024/06/03 05:07
1174 区间中最大的数
基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题
收藏
关注
给出一个有N个数的序列,编号0 - N - 1。进行Q次查询,查询编号i至j的所有数中,最大的数是多少。
例如: 1 7 6 3 1。i = 1, j = 3,对应的数为7 6 3,最大的数为7。(该问题也被称为RMQ问题)
Input
第1行:1个数N,表示序列的长度。(2 <= N <= 10000)第2 - N + 1行:每行1个数,对应序列中的元素。(0 <= S[i] <= 10^9)第N + 2行:1个数Q,表示查询的数量。(2 <= Q <= 10000)第N + 3 - N + Q + 2行:每行2个数,对应查询的起始编号i和结束编号j。(0 <= i <= j <= N - 1)
Output
共Q行,对应每一个查询区间的最大值。
Input示例
51763130 11 33 4
Output示例
773
拿线段树的模板改一下就行
#include<stdio.h>#include<string.h>#include<algorithm>using namespace std;#define MAXN 200010#define lson l,mid,p<<1#define rson mid+1,r,p<<1|1int sum[MAXN<<2];void pushUp(int p){ sum[p]=max(sum[p<<1],sum[p<<1|1]);}void build(int l,int r,int p){ if(l==r){ scanf("%d",&sum[p]); return ; } int mid=(l+r)>>1; build(lson); build(rson); pushUp(p);}int query(int l,int r,int p,int a,int b){ if(l>=a&&b>=r) return sum[p]; int ans=0; int mid=(l+r)>>1; if(mid>=a) ans=max(ans,query(lson,a,b)); if(mid<b) ans=max(ans,query(rson,a,b)); return ans;}void update(int l,int r,int p,int a,int b){ if(l==r){ sum[p]=b; return ; } int mid=(l+r)>>1; if(a<=mid) update(lson,a,b); else update(rson,a,b); pushUp(p);}int main(){ int n,m; char ch[5]; //freopen("in.txt","r",stdin); while(~scanf("%d",&n)) { int a,b; build(1,n,1); scanf("%d",&m); while(m--) { scanf("%d%d",&a,&b); //if(ch[0]=='Q') printf("%d\n",query(1,n,1,a+1,b+1)); } } return 0;}
阅读全文
0 0
- 51nod 1174 区间中最大的数【线段树】
- 51nod 1174 区间中最大的数
- 51nod--1174 区间中最大的数 (RMQ)
- 【51Nod】1174 - 区间中最大的数(RMQ)
- 51nod 1174 区间中最大的数(线段树)
- 51nod 1174 区间中最大的数
- 51nod:1174 区间中最大的数(RMQ)
- 【51Nod】1174 区间中最大的数
- 51Nod 1174区间中最大的数
- 51Nod-1174-区间中最大的数
- 51Nod 1174 区间中最大的数 线段树
- 51Nod-1174-区间中最大的数
- 51Nod-1174 区间中最大的数(RMQ)
- 51Nod 1174:区间中最大的数
- 51Nod 1174 区间中最大的数<线段树>
- 51nod 1174 区间中最大的数(RMQ)
- 51nod 1174 区间中最大的数
- 51Nod-1174-区间中最大的数
- Vue.use源码分析
- 简单的hashmap的实现
- 远程工具-SecureCRT/SecureFX
- C 输入 & 输出
- Ubuntu12.04下搭建tftp服务器最简单方法
- 51nod 1174 区间中最大的数
- vn.trader的Ubuntu运行环境搭建教程(mod)
- Docker:镜像加速器,阿里云,Docker Hub 镜像站点,升级你的Docker客户端
- QT POST/GET 发送/获取网页数据
- SmartTemplate 中文手册
- jni实现返回值为java类型的String[]和List<E>的函数
- Git使用手册:使用SSH Key及配置SSH key公钥
- 【SSD】用caffe-ssd框架MobileNet网络训练自己的数据集
- Android项目代码混淆