博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
poj 3264 Balanced Lineup线段树区间最值差
阅读量:4205 次
发布时间:2019-05-26

本文共 3518 字,大约阅读时间需要 11 分钟。

Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 50919   Accepted: 23849
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 ≤ 
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 31734251 54 62 2

Sample Output

63 0 题目链接:     题目大意:   给出初始化的区间值,m次查询                   每次查询区间[a,b]的最大值-最小值 解题思路:   线段树    更新: 无更新    查询:区间查询                   建立线段树的时候,每个结点存储左右子树的最大值和最小值                   查询时直接访问区间最大值和最小值,不需要查找到最低                   查询时间复杂度O(logN) 代码:  
[cpp]   
#include 
  
#include 
  
#include 
  
#define MAXN 70000  
#define INF 0x3f3f3f3f  
#define MAX(a,b) a>b?a:b  
#define MIN(a,b) a
>1  
#define L(a) a<<1  
#define R(a) (a<<1)+1  
  
typedef struct snode{  
    int left,right;  
    int max,min;  
}Node;  
  
Node Tree[MAXN<<1];  
int num[MAXN],minx,maxx;  
  
void Build(int t,int l,int r)    //以t为根结点建立左子树为l,右子树为r的线段树  
{  
    int mid;  
    Tree[t].left=l,Tree[t].right=r;  
    if(Tree[t].left==Tree[t].right)  
    {  
        Tree[t].max=Tree[t].min=num[l];  
        return ;  
    }  
    mid=MID(Tree[t].left,Tree[t].right);  
    Build(L(t),l,mid);  
    Build(R(t),mid+1,r);  
    Tree[t].max=MAX(Tree[L(t)].max,Tree[R(t)].max);  //更新结点的最大值=MAX(左子树,右子树)  
    Tree[t].min=MIN(Tree[L(t)].min,Tree[R(t)].min);  //更新结点的最小时=MIN(左子树,右子树)  
}  
  
void Query(int t,int l,int r)    //查询结点为t,左子树为l,右子树为r的最大值和最小值  
{  
    int mid;  
    if(Tree[t].left==l&&Tree[t].right==r)  
    {  
        if(maxx
Tree[t].min)  
            minx=Tree[t].min;  
        return ;  
    }  
    mid=MID(Tree[t].left,Tree[t].right);  
    if(l>mid)  
    {  
        Query(R(t),l,r);  
    }  
    else if(r<=mid)  
    {  
        Query(L(t),l,r);  
    }  
    else  
    {  
        Query(L(t),l,mid);  
        Query(R(t),mid+1,r);  
    }  
}  
  
int main()  
{  
    int n,m,a,b,i;  
    memset(Tree,0,sizeof(Tree));  
    scanf("%d%d",&n,&m);  
    for(i=1;i<=n;i++)  
        scanf("%d",&num[i]);  
    Build(1,1,n);            //建立以1为根结点区间为[1,n]的线段树  
    while(m--)  
    {  
        scanf("%d%d",&a,&b);  
        maxx=0;minx=INF;     //初始化最大值为0,最小值为INF  
        Query(1,a,b);        //查询区间[a,b]的最大值和最小值  
        printf("%d\n",maxx-minx);  
    }  
    return 0;  
}  

转载地址:http://nsali.baihongyu.com/

你可能感兴趣的文章
【Error】make LKM时 找不到符号
查看>>
【转载】【C语言】浅析C语言之uint8_t / uint16_t / uint32_t /uint64_t
查看>>
【转载】yum update 自动忽略内核更新
查看>>
【maven】打包jar上传到服务器运行
查看>>
关闭centos wayland
查看>>
【Error】chsh: PAM: Authentication failure
查看>>
【Error】zsh历史记录丢失
查看>>
解析漏洞总结
查看>>
有趣的二进制 读书笔记
查看>>
记一次vmware磁盘扩容part2:真正扩展根目录
查看>>
【Error】zsh: corrupt history file /home/myusername/.zsh_history
查看>>
记一次编译linux 2.6 和4.10内核源码
查看>>
【Error】couldn't be accessed by user '_apt'. - pkgAcquire::Run (13: Permission denied) [duplicate]
查看>>
qemu 文件系统制作:自己制作根目录和应用程序 + busybox
查看>>
关闭CSDN广告必备插件:adblock plus
查看>>
【pwnable.kr】fd
查看>>
【pwnable.kr】 collision
查看>>
【pwnable.kr】bof
查看>>
【pwnable.kr】flag
查看>>
【pwnable.kr】 passcode
查看>>