博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
POJ 3264 Balanced Lineup
阅读量:4684 次
发布时间:2019-06-09

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

Balanced Lineup
Time Limit: 5000MS Memory Limit: 65536K
Total Submissions: 28725 Accepted: 13519
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 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>
using namespace std;
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
const int Max=111111;
int maxn[Max<<2];
int minn[Max<<2];
void pushUP(int rt)
{
    maxn[rt]=max(maxn[rt<<1],maxn[rt<<1|1]);
    minn[rt]=min(minn[rt<<1],minn[rt<<1|1]);
}
void build(int l,int r,int rt)
{
    if(l==r)
    {
        int a;
        scanf("%d",&a);
        maxn[rt]=minn[rt]=a;
        return ;
    }
    int m=(r+l)>>1;
    build(lson); build(rson);
    pushUP(rt);
}
int querymax(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return maxn[rt];
    }
    int m=(l+r)>>1;
    int ret=0xdeadbeef;
    if(L<=m)
    {
        ret=max(ret,querymax(L,R,lson));
    }
    if(m<R)
    {
        ret=max(ret,querymax(L,R,rson));
    }
    return ret;
}
int querymin(int L,int R,int l,int r,int rt)
{
    if(L<=l&&r<=R)
    {
        return minn[rt];
    }
    int m=(l+r)>>1;
    int ret=-0xdeadbeef;
    if(L<=m)
    {
        ret=min(ret,querymin(L,R,lson));
    }
    if(R>m)
    {
        ret=min(ret,querymin(L,R,rson));
    }
    return ret;
}
int main()
{
    int n,m;
    scanf("%d%d",&n,&m);
    build(1,n,1);
    while(m--)
    {
        int a,b;
        scanf("%d%d",&a,&b);
        printf("%d\n",querymax(a,b,1,n,1)-querymin(a,b,1,n,1));
    }
    return 0;
}

转载于:https://www.cnblogs.com/CKboss/p/3350913.html

你可能感兴趣的文章
[树形dp] Jzoj P1162 贪吃的九头龙
查看>>
Jquery 相关笔记
查看>>
利用表单发送邮件
查看>>
计算机一族必喝的四杯茶
查看>>
linux 下的ssh免密登陆设置
查看>>
【Hibernate 7】浅谈Hibernate的缓存机制
查看>>
润乾报表 动态控制文本的显示
查看>>
[oracle] 如何使用myBatis在数据库中插入数据并返回主键
查看>>
PHP echo 和 print 语句
查看>>
第一讲 一个简单的Qt程序分析
查看>>
Centos 6.5下的OPENJDK卸载和SUN的JDK安装、环境变量配置
查看>>
poj 1979 Red and Black(dfs)
查看>>
【.Net基础03】HttpWebRequest模拟浏览器登陆
查看>>
UML-画类图与交互图的顺序
查看>>
杭电1060
查看>>
webdriver test1
查看>>
RFC端口号定义
查看>>
Unity Technologies-提供全面的技术支持服务
查看>>
Console-算法[for,if,break]-五个好朋友分苹果
查看>>
ylb: SQL表的高级查询-子查询
查看>>