[洛谷]P2880 [USACO07JAN]平衡的阵容Balanced Lineup (#树状数组)

36 篇文章 0 订阅
31 篇文章 0 订阅

题目背景

题目描述:

每天,农夫 John 的N(1 <= N <= 50,000)头牛总是按同一序列排队. 有一天, John 决定让一些牛们玩一场飞盘比赛. 他准备找一群在对列中为置连续的牛来进行比赛. 但是为了避免水平悬殊,牛的身高不应该相差太大. John 准备了Q (1 <= Q <= 200,000) 个可能的牛的选择和所有牛的身高 (1 <= 身高 <= 1,000,000). 他想知道每一组里面最高和最低的牛的身高差别.

输入:

第1行:N,Q

第2到N+1行:每头牛的身高

第N+2到N+Q+1行:两个整数A和B,表示从A到B的所有牛。(1<=A<=B<=N)

输出:

输出每行一个数,为最大数与最小数的差

题目描述

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.

一个农夫有N头牛,每头牛的高度不同,我们需要找出最高的牛和最低的牛的高度差。

输入格式

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 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.

输入输出样例

输入 #1

6 3
1
7
3
4
2
5
1 5
4 6
2 2

输出 #1

6
3
0

思路

给一个序列,求一段区间最大值和最小值的差。

RMQ问题,一般可以用线段树,树状数组,ST表等求。

树状数组

#include <stdio.h>
#include <iostream>
#include <memory.h>
using namespace std;
int n,m,a[50001],maxn[50001],minx[50001],s;
inline int lowbit(int x)
{
	return x&-x;
}
inline void update(int x,int k)//区间[x,n]修改最值 
{
	while(x<=n)
	{
		maxn[x]=max(maxn[x],k);
		minx[x]=min(minx[x],k);
		x+=lowbit(x);
	}
}
inline int query(int l,int r)//区间[l,r]查询最值 
{//对比一下树状数组的查询区间和,其实还是很像的 
	register int mn(2e9+7),mx(-2e9-7);
	while(l<=r)
	{
		while(r-lowbit(r)>=l)//while(r>0)
		{
			mx=max(mx,maxn[r]);
			mn=min(mn,minx[r]);
			r-=lowbit(r);
		}
		mx=max(a[r],mx);
		mn=min(a[r],mn);
		r--;
	}
	return mx-mn;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	register int i,j;
	memset(minx,2E9+7,sizeof(minx));
	cin>>n>>m;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
		update(i,a[i]);//单点修改 
	}
	for(i=1;i<=m;i++)
	{
		register int l,r;
		cin>>l>>r;
		cout<<query(l,r)<<endl;//区间查询最值 
	}
	return 0;
}

线段树代码

#include <stdio.h>
#include <iostream>
#define ll long long 
#define maxn 50001
using namespace std;
ll int a[maxn],n,s,m,cnt,c,ans;//a是序列
ll int minx[maxn<<2],maxx[maxn<<2];//区间最小值和最大值
inline ll int leftnode(ll int p) {return p<<1;}//左节点(左儿子) 
inline ll int rightnode(ll int p) {return p<<1|1;}//右节点(右儿子) 
inline void push_up(ll int node)//维护父子节点之间的逻辑关系(合并2个儿子节点) 
{
	minx[node]=min(minx[leftnode(node)],minx[rightnode(node)]);//最小值
	maxx[node]=max(maxx[leftnode(node)],maxx[rightnode(node)]);//最大值
}
void build(ll int node,ll int start,ll int end)
{
	if(start==end)
	{
		minx[node]=a[++cnt];//区间最小值
		maxx[node]=a[cnt];//区间最大值
		return; 
	}
	else
	{
		register ll int mid=(start+end)>>1;
		build(leftnode(node),start,mid);
		build(rightnode(node),mid+1,end);
		push_up(node);
	}
}
ll int min_query(ll int node,ll int start,ll int end,ll int cl,ll int cr)//node是当前节点,start和end是范围(是指a数组的范围),L和R是在区间[L,R]里计算和
{//最小值区间查询
	if(start>=cl && end<=cr)//如果修改的区间包括当前遍历的区间 
	{
		return minx[node];//返回这一区间的区间和 
	}
	register ll int mid=(start+end)>>1,s(99999999);
	if(cl<=mid)
	{
		s=min(s,min_query(leftnode(node),start,mid,cl,cr));
	}
	if(mid<cr)
	{
		s=min(s,min_query(rightnode(node),mid+1,end,cl,cr));
	}
	return s;
}
ll int max_query(ll int node,ll int start,ll int end,ll int cl,ll int cr)//node是当前节点,start和end是范围(是指a数组的范围),L和R是在区间[L,R]里计算和
{//最大值区间查询 
	if(start>=cl && end<=cr)//如果修改的区间包括当前遍历的区间 
	{
		return maxx[node];//返回这一区间的区间和 
	}
	register ll int mid=(start+end)>>1,s(-99999999);
	if(cl<=mid)
	{
		s=max(s,max_query(leftnode(node),start,mid,cl,cr));
	}
	if(mid<cr)
	{
		s=max(s,max_query(rightnode(node),mid+1,end,cl,cr));
	}
	return s;
}
signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0);
	cout.tie(0);
	register ll int i;
	cin>>n>>m;
	for(i=1;i<=n;i++)
	{
		cin>>a[i];
	}
	build(1,1,n);
	for(i=1;i<=m;i++)
	{
		int l,r;
		cin>>l>>r;
		cout<<max_query(1,1,n,l,r)-min_query(1,1,n,l,r)<<endl;
	}
	return 0;
}

线段树和树状数组的效率其实差别还是比较大的。树状数组跑了大概800ms,线段树跑了1200ms。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值