博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
51 Node 1174
阅读量:7088 次
发布时间:2019-06-28

本文共 1099 字,大约阅读时间需要 3 分钟。

基准时间限制:1 秒 空间限制:131072 KB 分值: 0

例如: 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

题意

​ 输出q组查询的最大值。

解题思路

​ 线段树裸题。

代码

#include
using namespace std;const int maxn = 10005;int tree[maxn<<2],arr[maxn];void pushup(int rt){ tree[rt]=max(tree[rt<<1],tree[rt<<1|1]);}void biud(int l,int r,int rt){ if(l==r) { tree[rt]=arr[l]; return; } int m=(l+r)>>1; biud(l,m,rt<<1); biud(m+1,r,rt<<1|1); pushup(rt);}int query(int L,int R,int l,int r,int rt){ if(L <= l && r <= R){ return tree[rt]; } int m=(l+r)>>1; int ans=0; if(L <= m) ans=max(ans,query(L,R,l,m,rt<<1)); if(R > m) ans=max(ans,query(L,R,m+1,r,rt<<1|1)); return ans;}int main(){// freopen("in.txt","r",stdin); int n; scanf("%d",&n); for(int i=0; i

转载于:https://www.cnblogs.com/RefrainLi/p/8861400.html

你可能感兴趣的文章
美国洛杉矶之行
查看>>
教你用深度学习LSTM网络预测流行音乐趋势(附代码)
查看>>
JSP 内置对象Response
查看>>
nginx配置flv服务器
查看>>
开源远程控制RealVNC源代码中的通讯协议RFB(远程帧缓冲)(转)
查看>>
oracle 创建字段自增长——两种实现方式汇总(转)
查看>>
富力•环贸港与阿里云达成战略合作 共同探索“产业+互联网”发展之道
查看>>
如何在websphere 6.1中进行远程调试
查看>>
org.apache.ibatis.binding.BindingException: Invalid bound statement (not found):
查看>>
nginx 301跳转到带www域名方法rewrite(转)
查看>>
OSChina 周日乱弹 —— 超酷炫 58 页年终总结,笑喷!
查看>>
快速了解分布式能源管理系统
查看>>
How to make your issues in GitHub more professional? [Labels feature]
查看>>
修改Android Studio默认的API Level(SDK版本)
查看>>
使用Monitor对资源进行保护(一)
查看>>
关于CSDN 2016博客之星评选活动的感触
查看>>
任务执行器——Executor
查看>>
控件UI性能调优 -- SizeChanged不是万能的
查看>>
leetcode 203 Remove Linked List Elements
查看>>
JavaScript的6个算法实用小技巧
查看>>