Balanced Lineup

Time Limit: 5000MS   Memory Limit: 65536K
Total Submissions: 53703   Accepted: 25237
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 ≤ ABN), 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

  1. 6 3
  2. 1
  3. 7
  4. 3
  5. 4
  6. 2
  7. 5
  8. 1 5
  9. 4 6
  10. 2 2

Sample Output

  1. 6
  2. 3
  3. 0

Source

分析:线段树求最大值和最小值,然后最大值减去最小值即为正解!貌似这题好像有暴力写法?
下面给出AC代码:
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <string.h>
  4. using namespace std;
  5. #define maxsize 200020
  6. typedef struct
  7. {
  8. int left,right;
  9. int maxn;
  10. int minn;
  11. }Node;
  12. int n,m;
  13. int Max,Min;
  14. int num[maxsize];
  15. Node tree[maxsize*];
  16. inline void buildtree(int root,int left,int right)// 构建线段树
  17. {
  18. int mid;
  19. tree[root].left=left;
  20. tree[root].right=right;// 当前节点所表示的区间
  21. if(left==right)// 左右区间相同,则此节点为叶子,max 应储存对应某个学生的值
  22. {
  23. tree[root].maxn=num[left];
  24. tree[root].minn=num[left];
  25. return;
  26. }
  27. mid=(left+right)/;
  28. //int a,b;// 递归建立左右子树,并从子树中获得最大值
  29. buildtree(*root,left,mid);
  30. buildtree(*root+,mid+,right);
  31. tree[root].maxn=max(tree[root*].maxn,tree[root*+].maxn);
  32. tree[root].minn=min(tree[root*].minn,tree[root*+].minn);
  33. }
  34. inline void find(int root,int left,int right)// 从节点 root 开始,查找 left 和 right 之间的最大值
  35. {
  36. int mid;
  37. //if(tree[root].left>right||tree[root].right<left)// 若此区间与 root 所管理的区间无交集
  38. //return;
  39. if(left==tree[root].left&&tree[root].right==right)// 若此区间包含 root 所管理的区间
  40. {
  41. Max=max(tree[root].maxn,Max);
  42. Min=min(tree[root].minn,Min);
  43. return;
  44. }
  45. mid=(tree[root].left+tree[root].right)/;
  46. if(right<=mid)
  47. find(root*,left,right);
  48. else if(left>mid)
  49. find(root*+,left,right);
  50. else
  51. {
  52. find(root*,left,mid);
  53. find(root*+,mid+,right);
  54. //tree[root].maxn=max(tree[root*2].maxn,tree[root*2+1].maxn);
  55. //tree[root].minn=min(tree[root*2].minn,tree[root*2+1].minn);
  56. //return;
  57. }
  58. }
  59.  
  60. int main()
  61. {
  62. //char c;
  63. int i;
  64. int x,y;
  65. //scanf("d%d",&n,&m);
  66. while(scanf("%d%d",&n,&m)!=EOF)
  67. {
  68. for(i=;i<=n;i++)
  69. scanf("%d",&num[i]);
  70. buildtree(,,n);
  71. for(i=;i<=m;i++)
  72. {
  73. //getchar();
  74. Max=-;
  75. Min= ;
  76. scanf("%d%d",&x,&y);
  77. //if(c=='Q')
  78. //printf("%d\n",find(1,x,y));
  79. //else
  80. //{
  81. // num[x]=y;
  82. // update(1,x,y);
  83. //}
  84. find(,x,y);
  85. printf("%d\n",Max-Min);
  86. }
  87. }
  88. return ;
  89. }

POJ 3264 Balanced Lineup【线段树区间查询求最大值和最小值】的更多相关文章

  1. POJ 3264 Balanced Lineup 线段树RMQ

    http://poj.org/problem?id=3264 题目大意: 给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差. 思路: 依旧是线段树水题~ #include ...

  2. [POJ] 3264 Balanced Lineup [线段树]

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34306   Accepted: 16137 ...

  3. poj 3264 Balanced Lineup(线段树、RMQ)

    题目链接: http://poj.org/problem?id=3264 思路分析: 典型的区间统计问题,要求求出某段区间中的极值,可以使用线段树求解. 在线段树结点中存储区间中的最小值与最大值:查询 ...

  4. POJ 3264 Balanced Lineup 线段树 第三题

    Balanced Lineup Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line ...

  5. POJ 3264 Balanced Lineup (线段树)

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

  6. POJ - 3264 Balanced Lineup 线段树解RMQ

    这个题目是一个典型的RMQ问题,给定一个整数序列,1~N,然后进行Q次询问,每次给定两个整数A,B,(1<=A<=B<=N),求给定的范围内,最大和最小值之差. 解法一:这个是最初的 ...

  7. 【POJ】3264 Balanced Lineup ——线段树 区间最值

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 34140   Accepted: 16044 ...

  8. POJ3264 Balanced Lineup —— 线段树单点更新 区间最大最小值

    题目链接:https://vjudge.net/problem/POJ-3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000 ...

  9. Poj 3264 Balanced Lineup RMQ模板

    题目链接: Poj 3264 Balanced Lineup 题目描述: 给出一个n个数的序列,有q个查询,每次查询区间[l, r]内的最大值与最小值的绝对值. 解题思路: 很模板的RMQ模板题,在这 ...

随机推荐

  1. JavaWeb之ssm框架整合,用户角色权限管理

    SSM框架整合 Spring SpringMVC MyBatis 导包: 1, spring 2, MyBatis 3, mybatis-spring 4, fastjson 5, aspectwea ...

  2. iOS 5个Xcode开发调试技巧

    转自Joywii的博客,原文:Four Tips for Debugging in XCode Like a Bro    1.Enable NSZombie Objects(开启僵尸对象) Enab ...

  3. myecplise自带的tomcat问题

    今天做一个项目时候,发现myecplise自带的tomcat上面部署了是可以运行的,可是当部署到自己下载的tomcat时候,就报错,tomcat可以启动,项目无法启动,查了问题,发现是web,xml中 ...

  4. 2018年的UX设计师薪酬预测,你能拿多少?

    以下内容由Mockplus团队翻译整理,仅供学习交流,Mockplus是更快更简单的原型设计工具.   一个经验丰富的设计师完全可以根据地区和专业来可以预期薪酬之间的差距,其中悬殊最高可达80K. 本 ...

  5. C#中级-通过注册表读取Windows Service程序执行路径

    一.前言        假设我们的C#解决方案中有多个程序应用,如:Web应用.控制台程序.WPF程序应用和Windows服务应用. 那么这些非Windows Service应用程序怎么在代码中找到W ...

  6. CSS3 使用选择器在页面插入内容

    使用选择器来插入文字 h2:before{ content:'COLUMN'; color:white: background-color:orange: padding:1px 5px; } 注意点 ...

  7. [数据清洗]-使用 Pandas 清洗“脏”数据

    概要 准备工作 检查数据 处理缺失数据 添加默认值 删除不完整的行 删除不完整的列 规范化数据类型 必要的转换 重命名列名 保存结果 更多资源 Pandas 是 Python 中很流行的类库,使用它可 ...

  8. Linux发行版 CentOS6.5 修改默认主机名

    修改前准备 我们将主机名修改为comexchan.cnblogs.com(本文发布于http://comexchan.cnblogs.com/) 备份相关配置文件,以便回滚 cp /etc/sysco ...

  9. 我搞zabbix的那两天(2)

    摘要:前一篇(我搞zabbix的那两天(1))我介绍了Zabbix的安装部署以及遇到的问题,这一篇将介绍zabbix 使用及短信等告警实现!!! Zabbix主界面及汉化方法介绍 1.1 初始化主界面 ...

  10. Mobiscroll的介绍【一款兼容PC和移动设备的滑动插件】

    Mobiscroll是一个用于触摸设备的日期和时间选择器,它的使用不会改变HTML5.PhoneGap以及混合应用的原生用户体验.作为一款jQuery滑动选择插件,用户可以自定义主题样式,为自己的移动 ...