B. Balanced Lineup

Time Limit: 5000ms
Case Time Limit: 5000ms
Memory Limit: 65536KB

64-bit integer IO format: %lld      Java class name: Main

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

解题:RMQ

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <cstdlib>
 5 #include <vector>
 6 #include <climits>
 7 #include <algorithm>
 8 #include <cmath>
 9 #define LL long long
10 using namespace std;
11 int mn[50010][32],mx[50010][32],d[50010];
12 int main(){
13     int n,m,x,y,i,j;
14     while(~scanf("%d %d",&n,&m)){
15         for(i = 0; i < n; i++){
16             scanf("%d",d+i);
17         }
18         memset(mn,0,sizeof(mn));
19         memset(mx,0,sizeof(mx));
20         for(i = n-1; i >= 0; i--){
21             mn[i][0] = mx[i][0] = d[i];
22             for(j = 1; i+(1<<j)-1 < n; j++){
23                 mn[i][j] = min(mn[i][j-1],mn[i+(1<<(j-1))][j-1]);
24                 mx[i][j] = max(mx[i][j-1],mx[i+(1<<(j-1))][j-1]);
25             }
26         }
27         for(i = 0; i < m; i++){
28             scanf("%d %d",&x,&y);
29             if(x > y) swap(x,y);
30             int r = y - x + 1;
31             r = log2(r);
32             int theMax,theMin;
33             theMax = max(mx[x-1][r],mx[y-(1<<r)][r]);
34             theMin = min(mn[x-1][r],mn[y-(1<<r)][r]);
35             printf("%d\n",theMax-theMin);
36         }
37     }
38     return 0;
39 }

View Code

转载于:https://www.cnblogs.com/crackpotisback/p/3841068.html

B. Balanced Lineup相关推荐

  1. poj3264 - Balanced Lineup(RMQ_ST)

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

  2. POJ 3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53629   Accepted: 25223 ...

  3. poj 3264 Balanced Lineup RMQ问题 线段树

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  4. [BZOJ] 1637: [Usaco2007 Mar]Balanced Lineup

    1637: [Usaco2007 Mar]Balanced Lineup Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 697  Solved: 463 ...

  5. POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53703   Accepted: 25237 ...

  6. 1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列

    1702: [Usaco2007 Mar]Gold Balanced Lineup 平衡的队列 Time Limit: 5 Sec  Memory Limit: 64 MB Submit: 510   ...

  7. POJ 3264 Balanced Lineup(RMQ)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 24349   Accepted: 11348 ...

  8. 专题突破一之分块——Untitled Problem II,Balanced Lineup,[ioi2009]Regions

    文章目录 SP2940 UNTITLE1 - Untitled Problem II source solution code Balanced Lineup source code Count on ...

  9. POJ3274Gold Balanced Lineup(哈希)

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

  10. POJ 3264 Balanced Lineup 【线段树】

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 50004 Accepted: 23434 Cas ...

最新文章

  1. java opencv 平移_如何使用opencv pnpRansac()函数中的平移矩阵和旋转矩阵设置Rajawali相机的旋转?...
  2. caffe安装,编译(包括CUDA和cuDNN的安装),并训练,测试自己的数据(caffe使用教程)
  3. boost::proto模块实现在外部指定转换的示例的测试程序
  4. 数据结构c语言版总结,数据结构:C语言常见算法总结
  5. [引]VS2005 之 Visual Basic 程序的结构
  6. 作者:金海,博士,华中科技大学计算机科学与技术学院教授、博士生导师。...
  7. LeetCode 剑指Offer 64.求1,2到n的和, 不使用循环/判断及乘除
  8. stm32l4 外部中断按键会卡死_stm32f103c8怎么实现外部中断按键点灯,按一下就亮,再按一下就灭,求大神帮忙...
  9. c语言if全部用法,关于if的用法
  10. LeetCode 110. Balanced Binary Tree
  11. Python使用matplotlib可视化模拟闯红灯现象柱状图
  12. 工业革命 书_工业革命以来最重大的变化
  13. 64位字长的高性能微型计算机,什么是字长字节和字长的区别
  14. 电子版2寸照片的像素是多少
  15. 破解qq上网限制 突破限制上QQ
  16. Account locked due to 10 failed logins
  17. eureka自我保护机制EUREKA MAY BE INCORRECTLY CLAIMING INSTANCES ARE UP WHEN THEY‘RE NOT
  18. IJCAI 2022奖项公布:3篇杰出论文,南加大、耶拿大学等机构在列
  19. 僵尸进程和孤儿进程-(转自Anker's Blog)
  20. 【经验】一劳永逸解决中文显示是大方块,不是乱码(最详细解释,有图有真相)

热门文章

  1. Hadoop HIVE 安装配置(单机集群)
  2. 2.3 快速搭建你的第一个系统,并进行迭代
  3. java面向对象计算机与CD实验,Java面向对象上机实验指导书.doc
  4. mysql 编码php,php-MySQL的编码问题(和基础知识)
  5. 消息中间件学习总结(21)——RocketMQ 消息丢失场景分析及如何解决!
  6. Spring Boot学习总结(2)——Spring Boot整合Jsp
  7. 荣耀鸿蒙os2.0公测版,鸿蒙OS 2.0他来了!华为HarmonyOS 2.0开发者公测版正式推送
  8. linux 查看显卡信号_Ubuntu 16.04安装nvidia显卡驱动以及各种坑(包含解决方案)
  9. laravel常用命令整理
  10. 转载:JS数组reduce()和reduceRight()方法