http://poj.org/problem?id=3264

题目大意:

给定N个数,还有Q个询问,求每个询问中给定的区间[a,b]中最大值和最小值之差。

思路:

依旧是线段树水题~

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<algorithm>
  4. using namespace std;
  5. const int MAXN=50000+10;
  6. const int MAXM=MAXN<<2;
  7. const int INF=0x3fffffff;
  8. int maxv[MAXM],minv[MAXM],a[MAXN];
  9.  
  10. void build(int k,int L,int R)
  11. {
  12. if(L==R)
  13. {
  14. maxv[k]=a[L];
  15. minv[k]=a[L];
  16. }
  17. else
  18. {
  19. int m=(L+R)>>1;
  20. build(k<<1,L,m);
  21. build((k<<1)+1,m+1,R);
  22. maxv[k]=max(maxv[k<<1],maxv[(k<<1)+1]);
  23. minv[k]=min(minv[k<<1],minv[(k<<1)+1]);
  24. }
  25. }
  26.  
  27. int query_max(int k,int L,int R,int a,int b)
  28. {
  29. int ans=-INF;
  30. if(a<=L && R<=b) return maxv[k];
  31. else
  32. {
  33. int m=(L+R)>>1;
  34. if(a<=m) ans=max(ans,query_max(k<<1,L,m,a,b));
  35. if(m<b) ans=max(ans,query_max((k<<1)+1,m+1,R,a,b));
  36. return ans;
  37. }
  38. }
  39.  
  40. int query_min(int k,int L,int R,int a,int b)
  41. {
  42. int ans=INF;
  43. if(a<=L && R<=b) return minv[k];
  44. else
  45. {
  46. int m=(L+R)>>1;
  47. if(a<=m) ans=min(ans,query_min(k<<1,L,m,a,b));
  48. if(m<b) ans=min(ans,query_min((k<<1)+1,m+1,R,a,b));
  49. return ans;
  50. }
  51. }
  52.  
  53. int main()
  54. {
  55. int n,q;
  56. while(~scanf("%d%d",&n,&q))
  57. {
  58. for(int i=1;i<=n;i++)
  59. scanf("%d",&a[i]);
  60.  
  61. build(1,1,n);
  62.  
  63. for(int i=0;i<q;i++)
  64. {
  65. int a,b;
  66. scanf("%d%d",&a,&b);
  67. printf("%d\n",query_max(1,1,n,a,b)-query_min(1,1,n,a,b));
  68. }
  69. }
  70. return 0;
  71. }

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

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

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

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

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

  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

    题目链接:http://poj.org/problem?id=3264 For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) alw ...

  8. Poj 3264 Balanced Lineup RMQ模板

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

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

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

随机推荐

  1. cogs 50. [NOIP2002] 选数

    50. [NOIP2002] 选数 ★   输入文件:choose.in   输出文件:choose.out   简单对比时间限制:1 s   内存限制:128 MB [问题描述]: 已知 n 个整数 ...

  2. js中由undefined说起

    typeof()函数 返回的是字符串.有六种可能:"number"."string"."boolean"."object" ...

  3. 76.Nodejs Express目录结构

    转自:https://blog.csdn.net/xiaoxiaoqiye/article/details/51160262 Express是一个基于Node.js平台的极简.灵活的web应用开发框架 ...

  4. 用SSL保Samba安全

    用SSL保Samba安全          在企业中用Samba做为文件服务器是非常容易的事了,那如何保证存储数据的安全,如何保证数据传输的安全呢?我以前介绍过通过Samba安全级别和加装防病毒软件在 ...

  5. AIX 适配器

    1. 查看所有适配卡 lsdev -CHc adapter     2. 物理网卡适配卡 查看到物理网卡的个数与类型 lsdev -Cc adapter|grep ent   查看物理网卡具体插槽位( ...

  6. css--两行显示省略号兼容火狐浏览器

    css--两行显示省略号兼容火狐浏览器 正常写法: .ellipse1{overflow: hidden;white-space: nowrap;text-overflow: ellipsis;} . ...

  7. libssh2进行远程运行LINUX命令

    /** * CSSHClient.h * @file 说明信息.. * DATE February 13 2015 * * @author Ming_zhang */ #ifndef _CSSHCLI ...

  8. windows下硬盘安装debian

    windows下硬盘安装debian 此方法在 windows8.1 + debian8.7.1 可用 配置系统安装镜像 1 在windows下格式化一个fat32的分区 2 把下载的debian-7 ...

  9. 用djbdns为域名解析服务护航

      上期回顾:http://chenguang.blog.51cto.com/350944/292195       650) this.width=650;" alt="&quo ...

  10. httpurlconnection发送文件到服务端并接收

    httpurlconnection发送文件到服务端并接收 客户端 import java.io.DataInputStream; import java.io.File; import java.io ...