3264 Balanced Lineup 线段树 求区间的最大、小值

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

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int max_int=(1<<31)-1;
const int min_int=(1<<31);
struct node
{
    int left,right,maxn,minc;
};
node tree[1000000];//构造线段树
int n;//点数
int nmax,nmin;//当前最大值和最小者,查询是用
void buildtree(int id,int l,int r)//建树
{
    tree[id].left=l;tree[id].right=r;//初始化左右端点
    tree[id].maxn=min_int;tree[id].minc=max_int;//初始化最大,小值
    if(l!=r)//是根节点
    {
        buildtree(2*id,l,(l+r)/2);//左子树
        buildtree(2*id+1,(l+r)/2+1,r);//右子树
    }
}
void insert(int id,int i,int val)
{
    if(tree[id].left==i&&tree[id].right==i)//叶子节点
    {
        tree[id].minc=tree[id].maxn=val;
        return ;
    }
    tree[id].maxn=max(tree[id].maxn,val);//更新次节点的最大值
    tree[id].minc=min(tree[id].minc,val);//更新次节点的最小值
    if(i<=(tree[id].left+tree[id].right)/2)  insert(2*id,i,val);//进去左子树
    else insert(2*id+1,i,val);//进去右子树
}
void query(int id,int s,int e)//查询
{
    if(tree[id].maxn<=nmax&&tree[id].minc>=nmin) return ;//剪枝
    if(tree[id].left==s&&tree[id].right==e)//叶子节点
    {
        nmin=min(tree[id].minc,nmin);
        nmax=max(tree[id].maxn,nmax);
        return ;
    }
    if(e<=(tree[id].left+tree[id].right)/2)//左子树
    {
        query(2*id,s,e);
    }
    else if(s>=(tree[id].left+tree[id].right)/2+1)//右子树
    {
        query(2*id+1,s,e);
    }
    else//横跨左右子树
    {
        query(2*id,s,(tree[id].left+tree[id].right)/2);//左
        query(2*id+1,(tree[id].left+tree[id].right)/2+1,e);//右
    }
}
int main()
{
    int p;
    while(scanf("%d%d",&n,&p)==2)
    {
        buildtree(1,1,n);//第一个1表示当前的节点标号,第2个1表示树的左端点,n表示树的右端点;
        for(int i=1;i<=n;i++)
        {
            int x;scanf("%d",&x);
            insert(1,i,x);//插入:1表示当前的节点标号,i表示当前区间的右端点;
        }
        while(p--)
        {
            int s,e;
            scanf("%d%d",&s,&e);
            nmax=min_int;nmin=max_int;//初始化当前的最大,最小之
            query(1,s,e);//查询:1表示当前的节点标号,s表示左端点,e表示右端点
            printf("%d/n",nmax-nmin);
        }
    }
    return 0;
}
用指针做:



#include <iostream>
#include <algorithm>
#include <numeric>
using namespace std;
#define MY_MIN  99999999
#define MY_MAX  -99999999




struct CNode
{
int L,R;
int nMin,nMax;
CNode * pLeft, * pRight;
};
int nMax, nMin;
CNode Tree[1000000];
//CNode Tree[20];
int nCount = 0;
void BuildTree(CNode * pRoot, int L,int R)
{
pRoot->L = L;
pRoot->R = R;


pRoot->nMin = MY_MIN;
pRoot->nMax = MY_MAX;


if ( L != R) {
nCount ++;
pRoot->pLeft = Tree + nCount;
nCount ++;
pRoot->pRight = Tree + nCount;
BuildTree( pRoot->pLeft, L, ( L + R )/2);
BuildTree( pRoot->pRight, (L + R) / 2 + 1,R);
}
}
void Insert( CNode * pRoot , int i,int v)
{
if( pRoot->L == i &&  pRoot->R == i ) {
pRoot->nMin = pRoot->nMax = v;
return;
}
pRoot->nMin = _cpp_min(pRoot->nMin,v);
pRoot->nMax = _cpp_max(pRoot->nMax,v);
if( i <= (pRoot->L + pRoot->R )/2 )
Insert( pRoot->pLeft,i,v);
else
Insert( pRoot->pRight,i,v);
}
void Query(CNode * pRoot, int s, int e)
{
if( pRoot->nMin >= nMin && pRoot->nMax <= nMax )
return;
if( s == pRoot->L && e == pRoot->R) {
nMin = _cpp_min(pRoot->nMin,nMin);
nMax = _cpp_max(pRoot->nMax,nMax);
return ;
}
if( e <=  (pRoot->L + pRoot->R) / 2 )
Query(pRoot->pLeft, s,e);
else if( s >= (pRoot->L + pRoot->R) / 2 + 1)
Query(pRoot->pRight, s,e);
else {
Query(pRoot->pLeft, s,(pRoot->L + pRoot->R) / 2);
Query(pRoot->pRight, (pRoot->L + pRoot->R) / 2 + 1 ,e);
}
}
int main()
{
int n,q,h;
int i,j,k;
scanf("%d%d",&n,&q);
nCount = 0;
BuildTree(Tree,1,n);
for( i = 1;i <= n;i ++ ) {
scanf("%d",&h);
Insert( Tree,i,h);
}
for( i = 0;i < q;i ++ ) {
int s,e;
scanf("%d%d", &s,&e);
nMax = MY_MAX;
nMin = MY_MIN;
Query(Tree,s,e);
printf("%d/n",nMax - nMin);
}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值