POJ 3264 Balanced Lineup 求线段树区间最值
来源:互联网 发布:java面试简历模板下载 编辑:程序博客网 时间:2024/06/10 22:31
Description
For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.
Input
Lines 2..N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2..N+Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.
Output
Sample Input
6 31734251 54 62 2
Sample Output
630
题意:求区间最大值和最小值的差。
思路:线段树(适合线段树刚入门的人)。
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#define maxx 200100using namespace std;struct node{int l,r,mi,ma;int mid(){return (l+r)>>1;}}t[maxx];int p;int max(int a,int b){ return a>b?a:b;}int min(int a,int b){ return a>b?b:a;}void build(int rt,int l,int r){ t[rt].l=l,t[rt].r=r; if(l==r) { scanf("%d",&p); t[rt].mi=t[rt].ma=p; return ; } int mid=(l+r)>>1; build(rt<<1,l,mid); build(rt<<1|1,mid+1,r); t[rt].mi=min(t[rt<<1].mi,t[rt<<1|1].mi); t[rt].ma=max(t[rt<<1].ma,t[rt<<1|1].ma); return ;}int querymi(int rt,int a,int b){ if(t[rt].l==a&&t[rt].r==b) { return t[rt].mi; } int mid=t[rt].mid(); if(b<=mid) return querymi(rt<<1,a,b); else if(a>mid) return querymi(rt<<1|1,a,b); else return min(querymi(rt<<1,a,mid),querymi(rt<<1|1,mid+1,b));}int queryma(int rt,int a,int b){ if(t[rt].l==a&&t[rt].r==b) { return t[rt].ma; } int mid=t[rt].mid(); if(b<=mid) return queryma(rt<<1,a,b); else if(a>mid) return queryma(rt<<1|1,a,b); else return max(queryma(rt<<1,a,mid),queryma(rt<<1|1,mid+1,b));}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { build(1,1,n); int a,b; while(m--) { scanf("%d%d",&a,&b); printf("%d\n",queryma(1,a,b)-querymi(1,a,b)); } }}
- POJ 3264 Balanced Lineup 求线段树区间最值
- poj 3264 Balanced Lineup(线段树 区间最值)
- POJ 3264 Balanced Lineup 线段树 单点更新 求区间最值
- poj Balanced Lineup (线段树区间最值+优化)
- poj 3264 Balanced Lineup(RMQ求区间最值)
- POJ - 3264 Balanced Lineup (RMQ问题求区间最值)
- POJ 3264 Balanced Lineup(RMQ求区间最值)
- poj 3264 -- Balanced Lineup (区间最值,线段树/RMQ)
- POJ 3264 Balanced Lineup【简单线段树,区间最值,无更新】
- POJ 3264 Balanced Lineup (区间最值 RMQ模板/线段树)
- POJ 3264 Balanced Lineup(线段树—求区间最大值与最小值差)
- POJ3264 Balanced Lineup(线段树,区间最值)
- Balanced Lineup 线段树 rmq 区间最值
- POJ 3264 Balanced Lineup 线段树的构建+区间查询
- POJ 3264 Balanced Lineup (线段树单点更新 区间查询)
- POJ 3264-Balanced Lineup详解(线段树区间求值)
- poj 3264 Balanced Lineup线段树区间最值差
- POJ 3264 Balanced Lineup(线段树区间查询)
- 深入理解Java虚拟机总结
- PAT
- 文件的创建,打开与关闭
- 手机上出现点击事件不生效的一种解决方案
- xib,约束,监听键盘时间改变frame失败
- POJ 3264 Balanced Lineup 求线段树区间最值
- C++初次告白
- PHP 中的call_user_func_array() 和call_user_func()
- 冠军杜兰特和Deta比赛
- 理解SerDes 之一
- zookeeper原理
- 三、swift3.0字符串和集合的基本使用
- java/JSP 清空Session
- 解决ntp的错误 no server suitable for synchronization found