题意:求区间最大值-最小值。

分析:

1、线段树

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cctype>
  5. #include<cmath>
  6. #include<iostream>
  7. #include<sstream>
  8. #include<iterator>
  9. #include<algorithm>
  10. #include<string>
  11. #include<vector>
  12. #include<set>
  13. #include<map>
  14. #include<stack>
  15. #include<deque>
  16. #include<queue>
  17. #include<list>
  18. #define lowbit(x) (x & (-x))
  19. const double eps = 1e-8;
  20. inline int dcmp(double a, double b){
  21. if(fabs(a - b) < eps) return 0;
  22. return a > b ? 1 : -1;
  23. }
  24. typedef long long LL;
  25. typedef unsigned long long ULL;
  26. const int INT_INF = 0x3f3f3f3f;
  27. const int INT_M_INF = 0x7f7f7f7f;
  28. const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
  29. const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
  30. const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
  31. const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
  32. const int MOD = 1e9 + 7;
  33. const double pi = acos(-1.0);
  34. const int MAXN = 50000 + 10;
  35. const int MAXT = 10000 + 10;
  36. using namespace std;
  37. int a[MAXN];
  38. int minv[MAXN << 2], maxv[MAXN << 2];
  39. int _min, _max;
  40. void build(int id, int L, int R){
  41. if(L == R){
  42. minv[id] = maxv[id] = a[L];
  43. }
  44. else{
  45. int mid = L + (R - L) / 2;
  46. build(id << 1, L, mid);
  47. build(id << 1 | 1, mid + 1, R);
  48. minv[id] = min(minv[id << 1], minv[id << 1 | 1]);
  49. maxv[id] = max(maxv[id << 1], maxv[id << 1 | 1]);
  50. }
  51. }
  52. void query(int l, int r, int id, int L, int R){
  53. if(l <= L && R <= r){
  54. _max = max(_max, maxv[id]);
  55. _min = min(_min, minv[id]);
  56. return;
  57. }
  58. int mid = L + (R - L) / 2;
  59. if(l <= mid){
  60. query(l, r, id << 1, L, mid);
  61. }
  62. if(r > mid){
  63. query(l, r, id << 1 | 1, mid + 1, R);
  64. }
  65. }
  66. int main(){
  67. int N, Q;
  68. scanf("%d%d", &N, &Q);
  69. for(int i = 1; i <= N; ++i){
  70. scanf("%d", &a[i]);
  71. }
  72. build(1, 1, N);
  73. while(Q--){
  74. int A, B;
  75. scanf("%d%d", &A, &B);
  76. _min = INT_INF;
  77. _max = 0;
  78. query(A, B, 1, 1, N);
  79. printf("%d\n", _max - _min);
  80. }
  81. return 0;
  82. }

2、RMQ

Sparse-Table算法,预处理时间O(nlogn),查询O(1)。

  1. #include<cstdio>
  2. #include<cstring>
  3. #include<cstdlib>
  4. #include<cctype>
  5. #include<cmath>
  6. #include<iostream>
  7. #include<sstream>
  8. #include<iterator>
  9. #include<algorithm>
  10. #include<string>
  11. #include<vector>
  12. #include<set>
  13. #include<map>
  14. #include<stack>
  15. #include<deque>
  16. #include<queue>
  17. #include<list>
  18. #define lowbit(x) (x & (-x))
  19. const double eps = 1e-8;
  20. inline int dcmp(double a, double b){
  21. if(fabs(a - b) < eps) return 0;
  22. return a > b ? 1 : -1;
  23. }
  24. typedef long long LL;
  25. typedef unsigned long long ULL;
  26. const int INT_INF = 0x3f3f3f3f;
  27. const int INT_M_INF = 0x7f7f7f7f;
  28. const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
  29. const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
  30. const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
  31. const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
  32. const int MOD = 1e9 + 7;
  33. const double pi = acos(-1.0);
  34. const int MAXN = 50000 + 10;
  35. const int MAXT = 10000 + 10;
  36. using namespace std;
  37. int a[MAXN];
  38. int N, Q;
  39. int minv[MAXN][20];
  40. int maxv[MAXN][20];
  41. void RMQ_init(){
  42. for(int i = 1; i <= N; ++i){
  43. minv[i][0] = maxv[i][0] = a[i];
  44. }
  45. for(int j = 1; (1 << j) <= N; ++j){
  46. for(int i = 1; (i + (1 << j) - 1) <= N; ++i){
  47. minv[i][j] = min(minv[i][j - 1], minv[i + (1 << (j - 1))][j - 1]);
  48. maxv[i][j] = max(maxv[i][j - 1], maxv[i + (1 << (j - 1))][j - 1]);
  49. }
  50. }
  51. }
  52. int RMQ(int L, int R){
  53. int k = 0;
  54. while((1 << (k + 1)) <= (R - L + 1)) ++k;
  55. return max(maxv[L][k], maxv[R - (1 << k) + 1][k]) - min(minv[L][k], minv[R - (1 << k) + 1][k]);
  56. }
  57. int main(){
  58. scanf("%d%d", &N, &Q);
  59. for(int i = 1; i <= N; ++i){
  60. scanf("%d", &a[i]);
  61. }
  62. RMQ_init();
  63. while(Q--){
  64. int A, B;
  65. scanf("%d%d", &A, &B);
  66. printf("%d\n", RMQ(A, B));
  67. }
  68. return 0;
  69. }

  

POJ - 3264 Balanced Lineup(线段树或RMQ)的更多相关文章

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

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

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

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

  3. POJ 3264 Balanced Lineup 线段树RMQ

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

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

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

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

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

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

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

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

  8. Poj 3264 Balanced Lineup RMQ模板

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

  9. POJ 3264 Balanced Lineup -- RMQ或线段树

    一段区间的最值问题,用线段树或RMQ皆可.两种代码都贴上:又是空间换时间.. RMQ 解法:(8168KB 1625ms) #include <iostream> #include < ...

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

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

随机推荐

  1. scala命令行界面:help

    :help 查看所有可用命令 scala> :helpAll commands can be abbreviated, e.g., :he instead of :help.:edit < ...

  2. Python的常用库

    读者您好.今天我将介绍20个属于我常用工具的Python库,我相信你看完之后也会觉得离不开它们.他们是: Requests.Kenneth Reitz写的最富盛名的http库.每个Python程序员都 ...

  3. 设计模式课程 设计模式精讲 11-2 装饰者模式coding

    1 代码演练 1.1 代码演练1(未使用装饰者模式) 1.2 代码演练2(使用装饰者模式) 1 代码演练 1.1 代码演练1(未使用装饰者模式) 需求: 大妈下班卖煎饼,加一个鸡蛋加一元,一个火腿两元 ...

  4. Java 后端压缩图片

    import java.io.*;import java.util.Date;import java.awt.*;import java.awt.image.*;import javax.imagei ...

  5. 暴强贴:从.NET平台调用Win32 API----转载

      水之真谛 关注 17人评论 27649人阅读 2007-02-28 17:03:47 作者:刘铁猛日期:2005-12-20关键字:C# .NET Win32 API 版权声明:本文章受知识产权法 ...

  6. C 语言入门第五章--循环结构和选择结构

    C语言中有三大结构,分别是顺序结构.选择结构和循环结构: 逻辑运算: 与运算: && 或运算:|| 非运算:! ==== #include<stdio.h> int mai ...

  7. EMIS快速开发平台 - 微服务版技术选型

    http://demo.zuoyour.com/system/login EMIS快速开发平台 - 微服务版技术选型 开发框架:Spring Boot 2.1.3.RELEASE 微服务:Spring ...

  8. struts2--验证器

    1.输入验证: --struts2提供了一些基于Xwork Validation Framework的内建验证程序,使用这些验证程序不需要变编程,只要在一个XML文件里进行声明,声明的内容如下: &g ...

  9. springboot打war包上传到阿里云的Linux服务器

    下面的每一步应该都必不可少: 1.启动类 继承这个类,并且重新configure这个方法,return builder.sources(Code007Application.class); 2.pom ...

  10. 5.6 Nginx Rewrite模块配置