poj 3264 Balanced Lineup

线段树水题,寻找区间最大最小值。

只要建树还有查询操作就好了,更新没必要写。。

#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <iostream>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <stack>
#include <climits>
#define MID(x,y) ((x+y)>>1)
#define L(x) (x<<1)
#define R(x) (x<<1|1)

using namespace std;

const int MAX = 50010;
struct Tnode{int min,max,l,r;};
Tnode node[MAX*4];
int v[MAX];
int mmin,mmax;

void init()
{
	memset(node,0,sizeof(node));
}

void Build(int t,int l,int r)
{
	node[t].l = l;
	node[t].r = r;
	if( l == r - 1 )
	{
		node[t].min = node[t].max = v[l];
		return ;
	}
	int mid = MID(l,r);
	Build(L(t),l,mid);
	Build(R(t),mid,r);
	node[t].max = max(node[L(t)].max,node[R(t)].max);
	node[t].min = min(node[L(t)].min,node[R(t)].min);
}

void Query(int t,int l,int r)
{
	if( node[t].l == l && node[t].r == r )
	{
		mmax = max(node[t].max,mmax);
		mmin = min(node[t].min,mmin);
		return ;
	}
	int mid = MID(node[t].l,node[t].r);
	if( l >= mid )
		Query(R(t),l,r);
	else
		if( r <= mid )
			Query(L(t),l,r);
		else
		{
			Query(R(t),mid,r);
			Query(L(t),l,mid);
		}
}
int main()
{
	int n,m,x,y;
	
	while( ~scanf("%d%d",&n,&m) )
	{
		init();
		for(int i=0; i<n; i++)
			scanf("%d",&v[i]);
		Build(1,0,n);
		while( m-- )
		{
			scanf("%d%d",&x,&y);
			mmin = INT_MAX; mmax = 0;
			Query(1,x-1,y);
			printf("%d\n",mmax - mmin);
		}
	}

return 0;
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值