POJ 3264 Balanced Lineup (线段树)

来源:互联网 发布:midi软件哪个好 编辑:程序博客网 时间:2024/05/23 01:29

Balanced Lineup
Time Limit:5000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
Submit

Status

Practice

POJ 3264
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

线段树水题。节点里维护一个区间最大值和最小值。最后统计一下做个差就好了。

#include "iostream"#include "string.h"#include "cstdio"#include "algorithm"using namespace std;int num[50005];struct node{    int maxx,minn;    int left,right;}tree[50005*4];int mymax,mymin;void build(int root,int lf,int rt){    tree[root].maxx=0;    tree[root].minn=1000005;    tree[root].left=lf;    tree[root].right=rt;    if(lf==rt)    {        tree[root].maxx=tree[root].minn=num[lf];        return;    }    int mid=(lf+rt)/2;    build(root*2,lf,mid);    build(root*2+1,mid+1,rt);    tree[root].minn=min(tree[root].minn,tree[root*2].minn);    tree[root].minn=min(tree[root].minn,tree[root*2+1].minn);    tree[root].maxx=max(tree[root].maxx,tree[root*2].maxx);    tree[root].maxx=max(tree[root].maxx,tree[root*2+1].maxx);    return;}void find(int root,int lf,int rt){    if(mymin<=tree[root].minn&&mymax>=tree[root].maxx)        return;    if(lf<=tree[root].left&&rt>=tree[root].right)    {        mymax=max(mymax,tree[root].maxx);        mymin=min(mymin,tree[root].minn);        return;    }    if(tree[root].left==tree[root].right)        return;    int mid=(tree[root].left+tree[root].right)/2;    if(rt<=mid)        find(root*2,lf,rt);    else if(lf>mid)        find(root*2+1,lf,rt);    else    {        find(root*2,lf,mid);        find(root*2+1,mid+1,rt);    }    return;}int main(){    int n,q;    while(~scanf("%d%d",&n,&q))    {        for(int i=1;i<=n;i++)        {            scanf("%d",&num[i]);        }        build(1,1,n);        int a,b;        for(int i=1;i<=q;i++)        {            mymax=0;            mymin=1000005;            scanf("%d%d",&a,&b);            find(1,a,b);            printf("%d\n",mymax-mymin);        }    }}
0 0
原创粉丝点击