代码改变世界

POJ 3264 简单线段树

2012-04-17 14:48  javaspring  阅读(186)  评论(0编辑  收藏  举报

        纠结了一天半的时间,写出来一道线段树+lazy的题目后,再看这道题,当真是水之又水啊,,基本就是道模板题啊。。建树的时候存下来区间内的最大值,最小值,查找的时候,找到最大值,最小值,直接相减就可以了。。题目:

Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 21778   Accepted: 10114
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 ≤ 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 3
1
7
3
4
2
5
1 5
4 6
2 2

Sample Output

6
3
0
ac代码:

#include <iostream>
#include <cstdio>
using namespace std;
const int N=50010;
struct tree{
	int left,right,maxvalue,minvalue;
}tt[N*4];
int n,numask,height[N];
int max(int x,int y){
	return x>y?x:y;
}
int min(int x,int y){
	return x<y?x:y;
}
void built_tree(int lp,int rp,int pos){
	tt[pos].left=lp;
	tt[pos].right=rp;
	if(lp==rp){
		tt[pos].maxvalue=height[lp];
		tt[pos].minvalue=height[rp];
		return ;
	}
	int mid=(tt[pos].left+tt[pos].right)/2;
	built_tree(lp,mid,2*pos);
	built_tree(mid+1,rp,2*pos+1);
	tt[pos].maxvalue=max(tt[2*pos].maxvalue,tt[2*pos+1].maxvalue);
	tt[pos].minvalue=min(tt[2*pos].minvalue,tt[2*pos+1].minvalue);
}
int findmax(int lp,int rp,int pos){
	if(tt[pos].left==lp&&tt[pos].right==rp){
		return tt[pos].maxvalue;
	}
	int mid=(tt[pos].left+tt[pos].right)/2;
	if(rp<=mid){
	  return findmax(lp,rp,2*pos);
	}
	else if(lp>mid)
		return findmax(lp,rp,2*pos+1);
	else{
	  return max( findmax(lp,mid,2*pos), findmax(mid+1,rp,2*pos+1));
	}
}
int findmin(int lp,int rp,int pos){
	if(tt[pos].left==lp&&tt[pos].right==rp){
		return tt[pos].minvalue;
	}
	int mid=(tt[pos].left+tt[pos].right)/2;
	if(rp<=mid)
		return findmin(lp,rp,2*pos);
	else if(lp>mid)
		return findmin(lp,rp,2*pos+1);
	else
		return min( findmin( lp,mid,2*pos ),findmin( mid+1,rp,2*pos+1 ) );
}
int main(){
	//freopen("1.txt","r",stdin);
	while(~scanf("%d%d",&n,&numask)){
	  for(int i=1;i<=n;++i)
		  scanf("%d",&height[i]);
	  built_tree(1,n,1);
	  int x,y;
	  while(numask--){
	    scanf("%d%d",&x,&y);
		int mmax=findmax(x,y,1);
		int mmin=findmin(x,y,1);
		printf("%d\n",mmax-mmin);
	  }
	}
	return 0;
}