51nod1174【基于线段树的RMQ】
很基础啊~
#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int INF=-0x3f3f3f3f;
const int N=1e4+10;
struct asd{
int left;
int right;
int w;
};
asd q[N*4];
void Build(int num,int L,int R)
{
q[num].left=L;
q[num].right=R;
if(L==R)
{
scanf("%d",&q[num].w);
return;
}
Build(2*num,L,(L+R)/2);
Build(2*num+1,(L+R)/2+1,R);
q[num].w=max(q[2*num].w,q[2*num+1].w);
}
int query(int num,int s,int t)
{
if(s<=q[num].left&&t>=q[num].right)
return q[num].w;
if(q[num].right==q[num].left)
return q[num].w;
int mid=(s+t)/2;
if(mid>=t)
return query(2*num,s,t);
else if(mid<s)
return query(2*num+1,s,t);
else
return max(query(2*num,s,mid),query(2*num+1,mid+1,t));
}
int get_maxa(int num,int s,int t)
{
if(s<=q[num].left&&t>=q[num].right)
return q[num].w;
int mid=(q[num].left+q[num].right)/2;
if(mid>=t)
return get_maxa(2*num,s,t);
else if(mid<s)
return get_maxa(2*num+1,s,t);
else
return max(get_maxa(2*num,s,mid),get_maxa(2*num+1,mid+1,t));
}
int main()
{
int x,y,n,q;
scanf("%d",&n);
Build(1,1,n);
scanf("%d",&q);
while(q--)
{
scanf("%d%d",&x,&y);
printf("%d\n",get_maxa(1,x+1,y+1));
}
return 0;
}
51nod1174【基于线段树的RMQ】的更多相关文章
- 基于线段树的RMQ
RMQ(Range Minimum/Maximum Query)区间最值查询,即给出长度为n的数组A,以及m组询问s.t(s<=t<=n),返回区间[s,t]中的最值. 基于线段树的方法实 ...
- 线段树+RMQ问题第二弹
线段树+RMQ问题第二弹 上篇文章讲到了基于Sparse Table 解决 RMQ 问题,不知道大家还有没有印象,今天我们会从线段树的方法对 RMQ 问题再一次讨论. 正式介绍今天解决 RMQ 问题的 ...
- POJ 3368 Frequent values 线段树与RMQ解法
题意:给出n个数的非递减序列,进行q次查询.每次查询给出两个数a,b,求出第a个数到第b个数之间数字的最大频数. 如序列:-1 -1 1 1 1 1 2 2 3 第2个数到第5个数之间出现次数最多的是 ...
- POJ-3264 Balanced Lineup(区间最值,线段树,RMQ)
http://poj.org/problem?id=3264 Time Limit: 5000MS Memory Limit: 65536K Description For the daily ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- tyvj 1038 忠诚 区间最小值 线段树或者rmq
P1038 忠诚 时间: 1000ms / 空间: 131072KiB / Java类名: Main 描述 老管家是一个聪明能干的人.他为财主工作了整整10年,财主为了让自已账目更加清楚.要求管家每天 ...
- Codeforces Round #278 (Div. 1) Strip (线段树 二分 RMQ DP)
Strip time limit per test 1 second memory limit per test 256 megabytes input standard input output s ...
- V-Parenthesis 前缀+ZKW线段树或RMQ
Bobo has a balanced parenthesis sequence P=p 1 p 2…p n of length n and q questions. The i-th questio ...
- POJ - 3264 Balanced Lineup(线段树或RMQ)
题意:求区间最大值-最小值. 分析: 1.线段树 #include<cstdio> #include<cstring> #include<cstdlib> #inc ...
随机推荐
- ZT:三十个好习惯
- photoshop 前端常用技巧
1.将图片转换成 png 格式 并且 使背景透明 (1)用矩形选框工具选取一块区域 (2)右键 选择 ‘变换选区’ 进行微调 F8 查看尺寸 (3)复制图层(ctrl+c) ->新建文件(ctr ...
- 三联动 支持ie6,ie7 省,市,区
三联动 支持ie6,ie7 省,市,区 <html xmlns="http://www.w3.org/1999/xhtml"> <head> <tit ...
- [iOS]APP代码实践:建立一个辅助的APP类,降低对AppDelegate的改动
郝萌主倾心贡献.尊重作者的劳动成果,请勿转载. [UIApplication sharedApplication].delegate 可是时间长了还是认为这样不太好,AppDelegate本身有其自己 ...
- 腾讯云图片鉴黄集成到C# SQL Server 怎么在分页获取数据的同时获取到总记录数 sqlserver 操作数据表语句模板 .NET MVC后台发送post请求 百度api查询多个地址的经纬度的问题 try{}里有一个 return 语句,那么紧跟在这个 try 后的 finally {}里的 code 会 不会被执行,什么时候被执行,在 return 前还是后? js获取某个日期
腾讯云图片鉴黄集成到C# 官方文档:https://cloud.tencent.com/document/product/641/12422 请求官方API及签名的生成代码如下: public c ...
- 让你的eclipse实现写JAVA代码,HTML,CSS,JAVASCRIPT代码提示
1.打开eclipse→Windows→Preferences→Java→Editor→Content Assist 改动Auto Activation triggers for java的值为:zj ...
- Intel processor brand names-Xeon,Core,Pentium,Celeron----Quark
http://en.wikipedia.org/wiki/Intel_Quark Intel Quark From Wikipedia, the free encyclopedia Intel ...
- Struts2实现空表单信息的提示
须要的jar包文件: index.jsp源代码: <%@ page language="java" contentType="text/html; charset= ...
- Python - colour-science库
http://nbviewer.jupyter.org/github/colour-science/colour-ipython/blob/master/notebooks/colour.ipynb# ...
- npm的安装和更新
https://nodejs.org官网下载软件安装 验证是否安装,进入命令行 输入npm -v 这个是安装node自动带的工具 npm install npm@xxx 自动更新自己,后边跟版本号 n ...