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

  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

 
题解:典型的RMQ问题。线段树的应用。
 
代码:
  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<math.h>
  4. #include<ctype.h>
  5. #include<stdlib.h>
  6. #include<stdbool.h>
  7.  
  8. #define rep(i,a,b) for(i=(a);i<=(b);i++)
  9. #define clr(x,y) memset(x,y,sizeof(x))
  10. #define sqr(x) (x*x)
  11. #define LL long long
  12.  
  13. const int INF=0xffffff0;
  14.  
  15. struct {
  16. int L,R;
  17. int minV,maxV;
  18. } tree[];
  19.  
  20. int i,j,n,q,minV,maxV;
  21.  
  22. int min(int a, int b)
  23. {
  24. if(a<b) return a;
  25. return b;
  26. }
  27.  
  28. int max(int a,int b)
  29. {
  30. if(a>b) return a;
  31. return b;
  32. }
  33.  
  34. void BuildTree(int root,int L,int R)
  35. {
  36. tree[root].L=L;
  37. tree[root].R=R;
  38. tree[root].maxV=-INF;
  39. tree[root].minV=INF;
  40.  
  41. if(L!=R) {
  42. BuildTree(*root,L,(L+R)/);
  43. BuildTree(*root+,(L+R)/+,R);
  44. }
  45.  
  46. }
  47.  
  48. void Insert(int root,int i,int v)
  49. {
  50. int mid;
  51.  
  52. if(tree[root].L==tree[root].R) {
  53. tree[root].maxV=tree[root].minV=v;
  54. return ;
  55. }
  56.  
  57. tree[root].minV=min(tree[root].minV,v);
  58. tree[root].maxV=max(tree[root].maxV,v);
  59.  
  60. mid=(tree[root].L+tree[root].R)/;
  61. if(i<=mid)
  62. Insert(*root,i,v);
  63. else
  64. Insert(*root+,i,v);
  65.  
  66. }
  67.  
  68. void Query(int root,int s,int e)
  69. {
  70. int mid;
  71.  
  72. if(tree[root].minV>=minV && tree[root].maxV<=maxV) return ;
  73. if(tree[root].L==s && tree[root].R==e) {
  74. minV=min(minV,tree[root].minV);
  75. maxV=max(maxV,tree[root].maxV);
  76. return ;
  77. }
  78.  
  79. mid=(tree[root].L+tree[root].R)/;
  80.  
  81. if(e<=mid)
  82. Query(*root,s,e);
  83. else if(s>mid)
  84. Query(*root+,s,e);
  85. else {
  86. Query(*root,s,mid);
  87. Query(*root+,mid+,e);
  88. }
  89.  
  90. }
  91.  
  92. int main()
  93. {
  94. int i,x,y;
  95.  
  96. scanf("%d%d",&n,&q);
  97. BuildTree(,,n);
  98. rep(i,,n) {
  99. scanf("%d",&x);
  100. Insert(,i,x);
  101. }
  102.  
  103. while(q--) {
  104. scanf("%d%d",&x,&y);
  105. minV=INF;
  106. maxV=-INF;
  107. Query(,x,y);
  108. printf("%d\n",maxV-minV);
  109. }
  110.  
  111. return ;
  112. }

[POJ] 3264 Balanced Lineup [线段树]的更多相关文章

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

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

  2. POJ 3264 Balanced Lineup 线段树RMQ

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

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

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

  4. 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 ...

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

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

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

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

  7. Poj 3264 Balanced Lineup RMQ模板

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

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

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

  9. POJ 3264 Balanced Lineup 【ST表 静态RMQ】

    传送门:http://poj.org/problem?id=3264 Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total S ...

随机推荐

  1. InputStream转为byte数组

    InputStream is = request.getSession().getServletContext().getResourceAsStream("/WEB-INF/swdjzbg ...

  2. MVC3路由设置访问后缀 html jsp

     C# Code  12345678910111213141516171819202122232425262728293031323334353637383940414243444546   publ ...

  3. 【转】USB协议架构及驱动架构

    1. USB协议 1.1 USB主机系统 在USB主机系统中,通过根集线器与外部USB从机设备相连的处理芯片,称为USB主机控制器.USB主机控制器包含硬件.软件和固件一部分. 1.2 USB设备系统 ...

  4. 【转】iOS 解决ipv6问题

    解决ipv6的方法有很多种,由于现在国内的网络运营商还在使用ipv4的网络环境,所以appstore应用不可能大范围去修改自己的服务器, 而且国内的云服务器几乎没有ipv6地址. 这里附上苹果开发平台 ...

  5. Java 常调用的Webservice接口的方法

    WebService是基于Web的服务,WebService使用SOAP协议实现跨编程语言和跨操作系统平台,接收和响应外部系统的某种请求,从而实现远程调用.WebService采用HTTP协议传输数据 ...

  6. (转)Android创建桌面快捷方式两种方法

    [IT168技术]Android在桌面上生成快捷方式有两种情况,一种是直接在桌面直接生成;一种是长按桌面,在弹出的快捷菜单中生成. 谈谈在桌面上直接生成.个人觉得这个比较爽快,既然都是快捷方式了干嘛还 ...

  7. 在Ubuntu上下载、编译和安装Android最新源代码

    文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6559955 看完了前面说的几本书之后,对Lin ...

  8. open(),close() 打开/关闭文件

    Open open()是一个系统调用函数,用来打开或创建一个文件,通过不同的oflag选项实现不同功能. 使用时open()函数需要包含的头文件:<sys/types.h>,<sys ...

  9. Android 打造任意层级树形控件 考验你的数据结构和设计

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/40212367,本文出自:[张鸿洋的博客] 1.概述 大家在项目中或多或少的可能会 ...

  10. sql 表名为关键字

    user在sql server中时一个关键字,如上面说所的,有时候我们无意中将其作为表的名称,当我们在sql语句中要使用该名称时例如:select *from user这是会提示user附近有语法错误 ...