poj3264 Balanced Lineup(求区间的最大值与最小值之差)
来源:互联网 发布:mac安装卸载软件 编辑:程序博客网 时间:2024/06/05 06:06
Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536KTotal Submissions: 37869 Accepted: 17751Case Time Limit: 2000MS
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
Line 1: Two space-separated integers, N and Q.
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.
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
Lines 1..Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.
Sample Input
6 31734251 54 62 2
Sample Output
630
解题思路:先求最大值,再求最小值
/******************************************************************************* * Author : jinbao * Email : dongjinbao913106840144@gmail.com * Last modified : 2015-05-11 20:30 * Filename : poj3264.cpp * Description : * *****************************************************************************/#include <iostream>#include <stdio.h>using namespace std;#define min(x,y) x<y?x:y#define max(x,y) x>y?x:yconst int MAX = 50000+1;struct cow{int Min,Max;}p[4*MAX];int a[MAX];void build(int node,int begin,int end){if (begin==end){scanf("%d",&a[begin]);p[node].Min=p[node].Max=a[begin];}else{build(node*2,begin,(begin+end)/2);build(node*2+1,(begin+end)/2+1,end);p[node].Min=min(p[node*2].Min,p[node*2+1].Min);p[node].Max=max(p[node*2].Max,p[node*2+1].Max);}}int query(int node,int begin,int end,int left,int right,int flag){if (end<left || begin>right){if (flag==0)return 0xffffff;return -1;}if (begin>=left && end<=right){if (flag==0)return p[node].Min;return p[node].Max;}int m=query(2*node,begin,(begin+end)/2,left,right,flag);int n=query(2*node+1,(begin+end)/2+1,end,left,right,flag);if (flag==0)return min(m,n);return max(m,n);}int main(){int n,q,l,r,Min,Max;while (~scanf("%d%d",&n,&q)){build(1,1,n);while (q--){scanf("%d%d",&l,&r);int ans = query(1,1,n,l,r,1) - query(1,1,n,l,r,0);printf("%d\n",ans);}}return 0;}
0 0
- poj3264 Balanced Lineup(求区间的最大值与最小值之差)
- poj3264——Balanced Lineup(线段树+区间最大值与最小值)
- poj 3264 Balanced Lineup(查询区间最大值与最小值的差)
- POJ 3264 Balanced Lineup(线段树—求区间最大值与最小值差)
- 线段树求区间最大值与最小值的差
- hdu3264 Balanced Lineup (RMQ查询最大值和最小值的差)
- Balanced Lineup(线段树+最大值,最小值)
- 【POJ 3264】【RMQ 或者线段树】Balanced Lineup 【查询区间内最大最小值的差】
- POJ3264(线段树求区间最大值和最小值)
- NYOJ 119 士兵杀敌(三)(线段树—求区间最大值与最小值差)
- poj--3264Balanced Lineup+ST算法求区间最大最小值
- POJ3264----区间最值Balanced Lineup
- POJ3264 Balanced Lineup(RMQ)
- poj3264--Balanced Lineup(RMQ求最大最小)
- 【HDU3530】【单调队列(双)】Subsequence 【长度为n的数列,求最长子区间的长度,使得区间的最大值与最小值的差满足一个范围】
- hdu 5289 Assignment(给一个数组,求有多少个区间,满足区间内的最大值和最小值之差小于k)
- Balanced Lineup(线段树之区间查找最大最小值)
- 求点1 到点n 所有路径中 最大值与最小值之差 最小的那个
- 3-39(fun 函数求x的n次幂)
- 《程序员》2013年5期精彩内容:Mobile, GO!
- 数据库三大范式详解
- Codeforces Round #302 (Div. 1)B. Destroying Roads 最短路
- OC中简易通讯录框架
- poj3264 Balanced Lineup(求区间的最大值与最小值之差)
- 什么是openstack的 metadata
- jsp页面<input value=<%= %>标签获取数据库值的问题
- AOP:java动态代理+反射机制
- poj 1325 && zoj 1364 Machine Schedule
- 最原始的sql语句的数据封装
- VMware Workstation利用host-only也可以上网
- What does the distance attribute in DMatches mean?
- Spring框架IOC(控制反转)的核心