poj 3246 Balanced Lineup(线段树)

Balanced Lineup

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 38942   Accepted: 18247
Case 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 ≤
ABN), 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 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0

Source

USACO 2007 January Silver

题目大意:在一定区间内给定一些数,要求求出在某一区间内最大值和最小值的差。

线段树的题目。对于这道题目,既然是求最大值和最小值的差,那么必然要在区间里面存放最大值和最小值。同时这道题目只是单纯的要求查询区间内的差值,不需要进行更新。

#include<stdio.h>
#include<string.h>
#define max(a,b) a>b?a:b
#define min(a,b) a<b?a:b
#define INF 99999999
#define N 50005
struct tree{
	int l,r,maxi,mini;
	int mid(){
		return l+r>>1;
	}
}tree[N<<2];
int ma=-INF,mi=INF;
void build(int l,int r,int root)
{
	tree[root].l=l;
	tree[root].r=r;
	tree[root].maxi=-INF;
	tree[root].mini=INF;  //初始化最大最小值
	if(l==r){

		return;
	}
	int mid=l+r>>1;
	build(l,mid,root<<1);
	build(mid+1,r,root<<1|1);
}
void update(int i,int z,int root)
{

	if(tree[root].l==tree[root].r){
		tree[root].mini=tree[root].maxi=z;
		return;
	}
	tree[root].maxi=max(tree[root].maxi,z);
	tree[root].mini=min(tree[root].mini,z);      //每次都更新最大和最小值
    if(i<=tree[root].mid())update(i,z,root<<1);   //这里将i以下的节点全部更新。而i与mid 是有关系的。
    else update(i,z,root<<1|1);
}
void Query(int l,int r,int root)
{
	if(tree[root].mini>=mi&&tree[root].maxi<=ma)return;
   if(l==tree[root].l&&r==tree[root].r){
   	mi=min(mi,tree[root].mini);
   	ma=max(ma,tree[root].maxi);
   	return;
   }
   int mid=tree[root].l+tree[root].r>>1;
   if(r<=mid){
   	Query(l,r,root<<1);
   }
   else if(l>mid){
   	Query(l,r,root<<1|1);
   }
   else {
   	Query(l,mid,root<<1);
   	Query(mid+1,r,root<<1|1);
   }
   return ;
}
int main()
{
	int n,Q,cow[200005],a,b;
	int i,j,k;
	while(scanf("%d%d",&n,&Q)!=EOF)
	{
		build(1,n,1);
		for(i=1;i<=n;i++)
		{
		scanf("%d",&cow[i]);
	   update(i,cow[i],1);     //对于第i个数字进行插入
	}

	while(Q--)
	{
		scanf("%d%d",&a,&b);
		ma=-INF;
		mi=INF;
		Query(a,b,1);
		printf("%d\n",ma-mi);
	}
	}
	return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

时间: 2024-08-25 01:14:37

poj 3246 Balanced Lineup(线段树)的相关文章

POJ 3264 Balanced Lineup (线段树单点更新 区间查询)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 36820   Accepted: 17244 Case 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 Joh

POJ 3264 Balanced Lineup (线段树)

Balanced Lineup 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

POJ 3264 Balanced Lineup 线段树 第三题

Balanced Lineup 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

【POJ】3264 Balanced Lineup ——线段树 区间最值

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 Case 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 Joh

poj 3264 Balanced Lineup(线段数求区间最大最小值)

链接:http://poj.org/problem?id=3264 Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32772   Accepted: 15421 Case Time Limit: 2000MS Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order.

POJ3264 Balanced Lineup 线段树 RMQ ST算法应用

Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 36813 Accepted: 17237 Case 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 de

POJ-3264 Balanced Lineup(线段树)

Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 47042   Accepted: 22071 Case 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 Joh

Balanced Lineup(线段树之区间查找最大最小值)

传送门 线段树的区间查找最大最小值模板. 裸的线段树 #include <iostream> #include <cstring> #include <cstdio> #include <cmath> #include <set> #include <queue> #include <vector> #include <cstdlib> #include <algorithm> #define ls

POJ3264 Balanced Lineup 线段树区间最大值 最小值

Q个数 问区间最大值-区间最小值 1 // #pragma comment(linker, "/STACK:1024000000,1024000000") 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <sstream> 6 #include <string> 7 #include <algorithm> 8 #i