Balanced Lineup poj3264(线段树维护max,min)

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 77030 Accepted: 35276
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

题目大意:
有n头牛,现在给你每头牛i的身高(1<=i<=n),有m条询问,问你在[x,y]区间内身高的最大值-最小值等于多少?

分析:O(n)的算法肯定T了,我们要O(logn)的算法,这里采用线段树维护max,min时间复杂度O(logn)

思路:
1:针对此题,题目中只有查询,没有更新。
2:查询是以区间为单位,也就是说是区间查询。
3:有效信息是最大值和最小值,也就是说我们要维护的数据就是最大值和最小值。
我们只需要在结构体中新增一个最大值和最小值,建树的时候顺便求一下。
这题关键难在区间查询中,假如n=6,也就是说区间范围[1,6],根据我画的图:
在这里插入图片描述
我们可以很轻松的求出[1,6],[1,3],[1,2],[1,1],[2,2],[3,3],[4,6],[4,5],[,4,4],[5,5],[6,6]这些区间的最大值和最小值然后做减法就是答案了,但是我们要求[1,5],[3,6],[1,4]等这些分叉的区间就不太好求了。针对这些问题,我另外写了两个函数求max和min的然后对分叉区间求max和min,然后做减法就是答案了,具体看代码吧:

#include<iostream>
#include<algorithm>
#include<cstdio>
using namespace std;

typedef long long ll;
const int maxn=1<<19;
struct segment{
	ll l,r;
	ll _max,_min;
};
struct segment Node[maxn];
void build(ll i,ll l,ll r){
	Node[i].l=l;
	Node[i].r=r;
	if(l==r){
		scanf("%lld",&Node[i]._max);
		Node[i]._min=Node[i]._max;
		return ;
	}
	ll mid=(l+r)>>1;
	build(i<<1,l,mid);
	build(i<<1|1,mid+1,r);
	Node[i]._max=max(Node[i<<1]._max,Node[i<<1|1]._max);
	Node[i]._min=min(Node[i<<1]._min,Node[i<<1|1]._min);
}
ll _query1(ll i,ll l,ll r){
	if(Node[i].l==l&&Node[i].r==r){
		return Node[i]._max; 
	}
	ll mid=(Node[i].l+Node[i].r)>>1;
	if(r<=mid){
		return _query1(i<<1,l,r);
	}else if(l>mid){
		return _query1(i<<1|1,l,r);
	}else{
		return max(_query1(i<<1,l,mid),_query1(i<<1|1,mid+1,r));
	}
}
ll _query2(ll i,ll l,ll r){
	if(Node[i].l==l&&Node[i].r==r){
		return Node[i]._min; 
	}
	ll mid=(Node[i].l+Node[i].r)>>1;
	if(r<=mid){
		return _query2(i<<1,l,r);
	}else if(l>mid){
		return _query2(i<<1|1,l,r);
	}else{
		return min(_query2(i<<1,l,mid),_query2(i<<1|1,mid+1,r));
	}
}
ll query(ll i,ll l,ll r){
	if(Node[i].l==l&&Node[i].r==r){
		return Node[i]._max-Node[i]._min;
	}
	ll mid=(Node[i].l+Node[i].r)>>1;
	if(r<=mid){
		return query(i<<1,l,r);
	}else if(l>mid){
		return query(i<<1|1,l,r);
	}else{
		ll __max=max(_query1(i<<1,l,mid),_query1(i<<1|1,mid+1,r));
		ll __min=min(_query2(i<<1,l,mid),_query2(i<<1|1,mid+1,r));
		return __max-__min;
	}
}
int main(){
	ll n,q,x,y;
	scanf("%lld%lld",&n,&q);
	build(1,1,n);
	while(q--){
		scanf("%lld%lld",&x,&y);
		printf("%lld\n",query(1,x,y));
	}
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
【1】项目代码完整且功能都验证ok,确保稳定可靠运行后才上传。欢迎下载使用!在使用过程中,如有问题或建议,请及时私信沟通,帮助解答。 【2】项目主要针对各个计算机相关专业,包括计科、信息安全、数据科学与大数据技术、人工智能、通信、物联网等领域的在校学生、专业教师或企业员工使用。 【3】项目具有较高的学习借鉴价值,不仅适用于小白学习入门进阶。也可作为毕设项目、课程设计、大作业、初期项目立项演示等。 【4】如果基础还行,或热爱钻研,可基于此项目进行二次开发,DIY其他不同功能,欢迎交流学习。 【注意】 项目下载解压后,项目名字和项目路径不要用中文,否则可能会出现解析不了的错误,建议解压重命名为英文名字后再运行!有问题私信沟通,祝顺利! 基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip基于C语言实现智能决策的人机跳棋对战系统源码+报告+详细说明.zip
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值