Problem 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

思路:与 平衡的阵容(洛谷-P2880)同一题

Source Program

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<string>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<utility>
#include<stack>
#include<queue>
#include<vector>
#include<set>
#include<map>
#include<bitset>
#define EPS 1e-9
#define PI acos(-1.0)
#define INF 0x3f3f3f3f
#define LL long long
#define Pair pair<int,int>const int MOD = 1E9+7;
const int N = 1000000+5;
const int dx[] = {-1,1,0,0,-1,-1,1,1};
const int dy[] = {0,0,-1,1,-1,1,-1,1};
using namespace std;int dpMax[N][20];
int dpMin[N][20];
int a[N];
void initMax(int n){//初始化最大值查询for(int i=1;i<=n;i++)dpMax[i][0]=a[i];for(int j=1;(1<<j)<=n;j++)for(int i=1;i+(1<<j)-1<=n;i++)dpMax[i][j]=max(dpMax[i][j-1],dpMax[i+(1<<(j-1))][j-1]);
}
int getMax(int L,int R){//查询最大值//int k = (int)(log10(R-L+1)/log10(2));int k=0;while((1<<(k+1))<=R-L+1)k++;return max(dpMax[L][k],dpMax[R-(1<<k)+1][k]);
}void initMin(int n){//初始化最小值查询for(int i=1;i<=n;i++)dpMin[i][0]=a[i];for(int j=1;(1<<j)<=n;j++)for(int i=1;i+(1<<j)-1<=n;i++)dpMin[i][j]=min(dpMin[i][j-1],dpMin[i+(1<<(j-1))][j-1]);
}
int getMin(int L,int R){//查询最小值//int k = (int)(log10(R-L+1)/log10(2));int k=0;while((1<<(k+1))<=R-L+1)k++;return min(dpMin[L][k],dpMin[R-(1<<k)+1][k]);
}
int main(){int n,m;scanf("%d%d",&n,&m);for(int i=1;i<=n;i++)scanf("%d",&a[i]);initMax(n);initMin(n);for(int i=1;i<=m;i++){int left,right;scanf("%d%d",&left,&right);int maxx=getMax(left,right);int minn=getMin(left,right);printf("%d\n",maxx-minn);}return 0;
}

Balanced Lineup(POJ-3264)相关推荐

  1. 【POJ 3274】Gold Balanced Lineup (stl map )设计hash表,处理碰撞

    题目链接 题目链接 http://poj.org/problem?id=3274 题意 输入每头牛的特征的10进制,若i~j头牛中每个数位的特征相等则满足要求,求所有满足要求的j-i的最大值. 解题思 ...

  2. POJ3264——Balanced Lineup(线段树)

    本文出自:http://blog.csdn.net/svitter 题意:在1~200,000个数中.取一段区间.然后在区间中找出最大的数和最小的数字.求这两个数字的差. 分析:按区间取值,非常明显使 ...

  3. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  4. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  5. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  6. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  7. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  8. poj3264 - Balanced Lineup(RMQ_ST)

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

  9. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  10. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

最新文章

  1. c语言规定对使用的变量必须,C语言为什么要规定对所用到的变量要“先定义,后使用”...
  2. 【NLP】Kaggle从零到实践:Bert中文文本分类
  3. 用imageMagick的composite合并图片
  4. mysql low_case_MySQL8.0的坑之lower_case_table_names
  5. MTK平台环境搭建---Ubuntu Linux 下执行sudo apt-get install提示“现在没有可用的软件包……...
  6. 【PyQt5】连接 mysql 查询数据 并显示在 tableWidget 表格
  7. 企业可视化大屏如何搭建
  8. 推荐电影电视剧下载最好去处
  9. 阿里平头哥首次交货——玄铁910是个啥?是芯片吗?
  10. “本是青灯不归客,却因浊酒留风尘,星光不问赶路人,岁月不负有心人”,你是怎么理解的?
  11. densefusion代码
  12. 删除word标题前方空格的方法
  13. [ Azure | Az-900 ] 基础知识点总结(二) - 核心组件服务
  14. 自制快速冒烟测试小工具--基于python多线程
  15. 跟着老猫来搞GO——启程
  16. 在Qt Creator中的pro文件添加lib库
  17. DMA漏损管理系统(Axure高保真原型)
  18. PowerPoint2003(1)_模板与母版
  19. 元宇宙是人类文明不可避免的一次内卷
  20. iOS---本地推送通知UILocalNotification(可以用做类似闹钟提醒)

热门文章

  1. 这个事关中国人幸福感的问题,能解决吗?
  2. 盘点20个最好的数据科学Python库(附链接)
  3. Simulink之不可控整流电路
  4. java之父_java之父:被下载达7000万次的编程视频教程,你还没有看过?
  5. Java 数组转 List 的三种方式及对比
  6. 从17 个方面对比 Kafka、RabbitMQ、RocketMQ、ActiveMQ 等分布式消息队列
  7. 专访《程序员的三门课》李伟山:从程序员到技术总监的修炼秘籍!
  8. 行为模型:客户行为智能分析模型
  9. onclick传参数
  10. laravel 命令行输出进度条