Balanced Lineup
Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 45243   Accepted: 21240
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

USACO 2007 January Silver

分析 既可以用线段树也可以用ST来做
也是我的ST第一道题,不错
这道题我两种方法都写过的,比较简单,当做练熟练度
线段树的解法已经写过的,可以很明显的看出,ST的代码比线段树简洁得多
就直接上ST的代码
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cmath>
 4 #define maxx 50005
 5 using namespace std;
 6 int maxsum[maxx][18],minsum[maxx][18],n,Q;
 7 int _max(int a,int b)
 8 {
 9     return a>b?a:b;
10 }
11 int _min(int a,int b)
12 {
13     return a<b?a:b;
14 }
15 int main()
16 {
17
18     while(~scanf("%d %d\n",&n,&Q))
19     {
20         for(int i=1;i<=n;i++)
21         {
22             scanf("%d\n",&maxsum[i][0]);
23             minsum[i][0]=maxsum[i][0];
24         }
25         for(int j=1;j<=18;j++)// 预处理
26         for(int i=1;i<=n;i++)    //j 循环在 i 循环外
27         {
28             if(i+(1<<j)-1<=n)        // 注意左右区间
29             {
30                 maxsum[i][j]=_max(maxsum[i][j-1],maxsum[i+(1<<(j-1))][j-1]);
31                 minsum[i][j]=_min(minsum[i][j-1],minsum[i+(1<<(j-1))][j-1]);
32             }
33         }
34         while(Q--)
35         {int a,b;
36             scanf("%d %d\n",&a,&b);
37             int k=(int)((log((double)(b-a+1)))/log(2.0));    //注意要包含端点并转换成double
38             int maxl=_max(maxsum[a][k],maxsum[b-(1<<k)+1][k]);//不然有些测试要出问题 比如poj
39             int minl=_min(minsum[a][k],minsum[b-(1<<k)+1][k]);
40             printf("%d\n",maxl-minl);
41         }
42     }
43     return 0;
44 }

转载于:https://www.cnblogs.com/lwhinlearning/p/5677179.html

poj3264 - Balanced Lineup(RMQ_ST)相关推荐

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

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

  2. POJ3264 Balanced Lineup【线段树】

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 63040   Accepted: 29405 ...

  3. 竞赛最好用的平衡树-Size Balanced Tree(SBT)【建议收藏】

    大家好. 前段时间我更新过AVL.红黑树以及搜索二叉树的简单讲解.今天我们还是围绕着平衡树的话题,来讲解一个很牛逼的平衡树结构.这种结构是我国的一位大牛:陈启峰.在2006年,他还在读高中阶段,所发明 ...

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

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

  5. Balanced Lineup(POJ-3264)

    Problem Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in t ...

  6. POJ——3624 Balanced Lineup(线段树入门——区间最值问题)

    原题链接:http://poj.org/problem?id=3264 每天挤奶时,农夫John的N头奶牛(1≤N≤50,000头)总是按照相同的顺序排列.一天,农夫约翰决定和几头牛组织一场极限飞盘游 ...

  7. Poj 3246 Balanced Lineup(线段树基础)

    依旧是线段树基础题 询问区间的最大值和最小值之差,只有询问,没有插入删除.继续理解基础线段树 #include <iostream> #include <algorithm> ...

  8. uva live 7637 Balanced String (贪心)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  9. POJ 3274 Gold Balanced Lineup(哈希)

    题目链接 很难想.会哈希,但是想不出.需要一个转化,本来是求某一段上的二进制上每一位的1数目相等,转化为找两段相等的,换元可推出公式.最后注意特判.. 1 #include <iostream& ...

最新文章

  1. centos7samba服务的搭建
  2. 从安全视角对机器学习的部分思考
  3. JavaScript常用判断函数 [转]
  4. python浪漫代码-python七夕浪漫表白源码
  5. 创业做什么好?先学习精英式创业从平庸到卓越
  6. 质量兴农战略规划-农业大健康·韩长赋:质量效益和竞争力
  7. 我的代码为什么看起来像shit?
  8. web----tcp三次握手
  9. mysql 报错1005_MysqlERROR 1005错误处理
  10. vmware安装黑苹果教程
  11. labelcontrol 多行_ios – UISegmentedControl中的两行文本
  12. 【时间序列】时间序列数据的缺失填补方法总结
  13. Linux系统编程入门学习笔记5-文件IO
  14. GPS原始RMC数据解析之DDMM.MMMM
  15. mysql grant priv_mysql怎么将grant priv的权限
  16. 什么oracle,什么是ORACLE?
  17. [历朝通俗演义-蔡东藩-前汉]第012回 戕县令刘邦发迹 杀郡守项梁举兵
  18. http://wenzhang.ztcztc.com/Detail.aspx?id=70537498-7FD3-8992-552B-27716F9315F8
  19. NetTerm 使用简介
  20. 【大学生Python】字典的基础使用

热门文章

  1. ios与html数据交互,iOS iOS与html进行交互
  2. android 开发jni,示例:hello-jni
  3. swagger怎么扫描多个包_Swagger快速入门
  4. 怎样快速学习html5,如何快速学习HTML5?带你了解HTML5学什么?
  5. DOM模拟京东常用快捷键
  6. 代数系统思维导图_线性代数思维导图专题
  7. Python3 基础语法(笔记2)
  8. 【 FPGA/IC 】常考加法器总结
  9. 【 FPGA 】时序分析中的基本概念和术语
  10. 波场DApp数据分析