您的位置:首页 > 其它

Balanced Lineup(线段树)

2016-07-27 23:32 169 查看
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu

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

http://blog.csdn.net/xieshimao/article/details/6241399这篇文章的代码写的非常好,我直接看懂了,赞!

贴上自己的代码:里面有自己的注释。

#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <algorithm>
#include <queue>
#include <stack>
#include <iomanip>
using namespace std;
void build(int l,int r,int root);
int Max(int a,int b);
int Min(int a,int b);
void Findmax(int l,int r,int root);
void Findmin(int l,int r,int root);
#define inf 0x3f3f3f3f//相当于无穷大
struct node
{
int l;
int r;
int maxn;
int minn;
};
node tree[200005];
int h[50005];
int maxn,minn;
int main()
{
int a,b,N,Q;
scanf("%d%d",&N,&Q);
for(int i=1;i<=N;i++)
scanf("%d",&h[i]);
build(1,N,1);//建立树
while(Q--)
{
maxn=0;
minn=inf;
scanf("%d%d",&a,&b);
Findmax(a,b,1);//找最大值
Findmin(a,b,1);//找最小值
printf("%d\n",maxn-minn);
}
return 0;
}
//建立树,一步一步的自己画图。
void build(int l,int r,int root)
{
tree[root].l=l;
tree[root].r=r;
if(l==r)
{
tree[root].maxn=h[l];
tree[root].minn=h[l];
return;
}
int mid=(l+r)/2;
build(l,mid,root*2);
build(mid+1,r,root*2+1);
tree[root].maxn=Max(tree[2*root].maxn,tree[2*root+1].maxn);
tree[root].minn=Min(tree[2*root].minn,tree[2*root+1].minn);
}
int Max(int a,int b)
{
if(a>b)
return a;
else
return b;
}
int Min(int a,int b)
{
if(a>b)
return b;
else
return a;
}
void Findmax(int l,int r,int root)
{
if(tree[root].l==l&&tree[root].r==r)
{
if(tree[root].maxn>maxn)
maxn=tree[root].maxn;
return;
}
int mid=(tree[root].l+tree[root].r)/2;
if(mid>=r)
Findmax(l,r,root*2);
else if(mid<l)
Findmax(l,r,root*2+1);
else
{
Findmax(l,mid,root*2);
Findmax(mid+1,r,root*2+1);
}
}
void Findmin(int l,int r,int root)
{
if(tree[root].l==l&&tree[root].r==r)
{
if(tree[root].minn<minn)
minn=tree[root].minn;
return;
}
int mid=(tree[root].l+tree[root].r)>>1;
if(mid>=r)
Findmin(l,r,root*2);
else if(mid<l)
Findmin(l,r,root*2+1);
else
{
Findmin(l,mid,root*2);
Findmin(mid+1,r,root*2+1);
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  线段树