POJ 3264 Balanced Lineup 【线段树】
<题目链接>
题目大意:
求给定区间内最大值与最小值之差。
解题分析:
线段树水题,每个节点维护两个值,分别代表该区间的最大和最小值即可。
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define Lson rt<<1,l,mid
#define Rson rt<<1|1,mid+1,r
#define INF 0x3f3f3f3f
+;
int n,m;
int a[N];
struct Tree{
int mn,mx;
}tr[N<<];
void Pushup(int rt){
tr[rt].mx=max(tr[rt<<].mx,tr[rt<<|].mx);
tr[rt].mn=min(tr[rt<<].mn,tr[rt<<|].mn);
}
void build(int rt,int l,int r){
if(l==r){
tr[rt].mn=tr[rt].mx=a[l];
return;
}
;
build(Lson);
build(Rson);
Pushup(rt);
}
int query(int rt,int l,int r,int L,int R,int c){
if(L<=l&&r<=R){
if(!c)return tr[rt].mn;
else return tr[rt].mx;
}
;
int mxval=-INF,mnval=INF;
if(L<=mid){
if(!c)mnval=min(mnval,query(Lson,L,R,c)); //注意这里最大和最小都是向左儿子递归
else mxval=max(mxval,query(Lson,L,R,c));
}
if(R>mid){
if(!c)mnval=min(mnval,query(Rson,L,R,c));
else mxval=max(mxval,query(Rson,L,R,c));
}
if(!c)return mnval;
else return mxval;
}
int main(){
while(scanf("%d%d",&n,&m)!=EOF){
;i<=n;i++){
scanf("%d",&a[i]);
}
build(,,n);
;i<=m;i++){
int u,v;
scanf("%d%d",&u,&v);
,,n,u,v,);
,,n,u,v,);
printf("%d\n",mx-mn);
}
}
;
}
2018-09-23
POJ 3264 Balanced Lineup 【线段树】的更多相关文章
- [POJ] 3264 Balanced Lineup [线段树]
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34306 Accepted: 16137 ...
- poj 3264 Balanced Lineup(线段树、RMQ)
题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...
- POJ 3264 Balanced Lineup 线段树RMQ
http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...
- POJ 3264 Balanced Lineup 线段树 第三题
Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...
- POJ 3264 Balanced Lineup (线段树)
Balanced Lineup For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the s ...
- POJ - 3264 Balanced Lineup 线段树解RMQ
这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...
- 【POJ】3264 Balanced Lineup ——线段树 区间最值
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 34140 Accepted: 16044 ...
- Poj 3264 Balanced Lineup RMQ模板
题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...
- POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】
Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 53703 Accepted: 25237 ...
- POJ 3264 Balanced Lineup 【ST表 静态RMQ】
传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total S ...
随机推荐
- 使用Eclipse进行Makefile项目
最近在MCU on Eclipse网站上看到Erich Styger所写的一篇有关在Eclipse中使用Makefile创建项目的文章,文章讲解清晰明了非常不错,所以呢没人将其翻译过来供各位同仁参考. ...
- Confluence 6 代理和 HTTPS 详细配置指南链接
详细配置指南 更多有关连接器示例,我们提供了一些按步骤配置的指南来帮助你启用 HTTPS 并正确配置你的代理. HTTPS: Running Confluence Over SSL or HTTPS ...
- Jquery无刷新实时更新表格数据
html代码: <style> .editbox { display:none } .editbox { font-size:14px; width:70px; background-co ...
- ionic3 国际化
http://www.cnblogs.com/huangenai/p/6868173.html 按上面这个网站的步骤整一遍, 但是ionic3 会报错 所以 在 import { NgModule ...
- select下拉框使用完毕后,重置按钮使其清空
需求描述:select下拉框后边有两个按钮,一个查询,一个重置,点击重置,select会清空之前选择的那个查询条件 解决思路:卧槽,这不so easy 么,用那个jQ封装的trigger函数搞定啊,对 ...
- PHP Warning: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in /usr/local/php/CreateDB.php on line 5
原因:php还不支持mysql8.0最新的密码加密方式 ALTER USER 'root'@'localhost' IDENTIFIED WITH mysql_native_password BY ' ...
- ERROR 1045 (28000): Access denied for user 'mysql'@'localhost' (using password: YES
一.有时可以直接输入命令: mysql进入数据库 启动数据库:# mysqld_safe & 二.查看用户命令: mysql> use mysql; Reading table info ...
- Python判断字符串是否xx开始或结尾
判断是否xx开始 使用startswith 示例代码: String = "12345 上山打老虎" if str(String).startswith('1'): #判断Stri ...
- Windows批处理命令用法
阅读下面文字需要一定的dos基础概念,象:盘符.文件.目录(文件夹).子目录.根目录.当前目录 每个命令的完整说明请加 /? 参数参考微软的帮助文档可以看到,在 /? 帮助里,"命令扩展名& ...
- python 利用split读取文本文件中每一行的数字并保存至相应文件夹
import re from numpy import * def getStr(file_path,file_path1): fp = open(file_path, 'r') op = open( ...