Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 49061   Accepted: 22975
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 题目大意:给出一个数字序列,对于每次提问,输出区间(i,j)内的最大值数与最小值数的差。ST算法
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 50010
#define ll long long
using namespace std;
int f[N][],fi[N][],a[N];
int n,m;
void RMQ()
{
for (int i=;i<=n;i++) f[i][]=a[i],fi[i][]=a[i];
for (int i=;i<=int(log(n)/log());i++)
for (int j=;j<=n+-(<<i);j++)
{
f[j][i]=max(f[j][i-],f[j+(<<i-)][i-]);//区间最大值
fi[j][i]=min(fi[j][i-],fi[j+(<<i-)][i-]);//区间最小值
} }
int main()
{
while (scanf("%d%d",&n,&m)!=EOF)
{
for (int i=;i<=n;i++) scanf("%d",&a[i]);
RMQ();
for (int i=,x,y;i<=m;i++)
{
scanf("%d%d",&x,&y);
int k;
k=(int)(log(y-x+)/log());
int ans1=max(f[x][k],f[y-(<<k)+][k]),//两个区间存在重叠
ans2=min(fi[x][k],fi[y-(<<k)+][k]);
printf("%d\n",ans1-ans2);
}
} return ;
}

ST


模板题。

 
 

Balanced Lineup的更多相关文章

  1. poj 3264:Balanced Lineup(线段树,经典题)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 32820   Accepted: 15447 ...

  2. Balanced Lineup(树状数组 POJ3264)

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 40493 Accepted: 19035 Cas ...

  3. 三部曲一(数据结构)-1022-Gold Balanced Lineup

    Gold Balanced Lineup Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Othe ...

  4. poj 3264 Balanced Lineup (RMQ)

    /******************************************************* 题目: Balanced Lineup(poj 3264) 链接: http://po ...

  5. poj3264 - Balanced Lineup(RMQ_ST)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 45243   Accepted: 21240 ...

  6. bzoj 1637: [Usaco2007 Mar]Balanced Lineup

    1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MB Description Farmer John ...

  7. BZOJ-1699 Balanced Lineup 线段树区间最大差值

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 41548 Accepted: 19514 Cas ...

  8. POJ3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 44720   Accepted: 20995 ...

  9. POJ 3274 Gold Balanced Lineup

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 10924 Accepted: 3244 ...

  10. 哈希-Gold Balanced Lineup 分类: POJ 哈希 2015-08-07 09:04 2人阅读 评论(0) 收藏

    Gold Balanced Lineup Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 13215 Accepted: 3873 ...

随机推荐

  1. [置顶] Linux下的截图小工具

    Linux下的截图工具scrot 基于命令行 先下载:scrot apt-get install scrot 对该工具的操作: 分为以下几个部分 1.抓取整个桌面 scrot pic.jpg 2.抓取 ...

  2. oracle本月、上月、去年同月第一天最后一天

    select trunc(sysdate, 'month') 本月第一天,        trunc(last_day(sysdate)) 本月最后一天,        trunc(add_month ...

  3. Context

    Context,中文直译为“上下文”,SDK中对其说明如下: Interface to global information about an application environment. Thi ...

  4. IOPS QPS TPS

    杨奇龙: http://blog.itpub.net/22664653/viewspace-767265/ http://blog.itpub.net/22664653/viewspace-76726 ...

  5. Linux下查看系统配置

    CPU 1. lscpu:显示cpu架构信息 [xxx@localhost ~]$ lscpu Architecture: x86_64 CPU op-mode(s): -bit, -bit Byte ...

  6. 测试cnblogs是否存在链接引用的bug

    如果来源页弹出了一个对话框,或者跳转到了本页面,说明来源页面存在链接引用的bug

  7. 使用CSS的类名交集复合选择器

    首先先看一下基本定义: 复合选择器就是两个或多个基本选择器,通过不同方式连接而成的选择器,主要包括“交集”选择器.“并集”选择器.“后代”选择器. 交集选择器 “交集”复合选择器是由两个选择器直接连接 ...

  8. Property工具类,Properties文件工具类,PropertiesUtils工具类

    Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...

  9. 多维数组遍历PHP

    原文出处 <?php /* * ------------------------------------------------- * Author : nowamagic * Url : ww ...

  10. 1.RABBITMQ 入门 - WINDOWS - 获取,安装,配置

    一. 背景:     公司项目有所改动,要求微信(移动端调用的接口),日志接口换位log4net,全部改成以rabbitMQ作为服务支持, 二.本地环境:     windows 10 enterpr ...