题目连接:

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

题目大意:

  有n个数从1开始编号,问在指定区间内,最大数与最小数的差值是多少?

解题思路:

  在节点中存储max,min,然后查询指定区间的max、min。

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. using namespace std;
  5. const int maxn = ;
  6. const int INF = 0x3f3f3f3f;
  7. struct node
  8. {
  9. int L, R;
  10. int Min, Max;
  11. int Mid()
  12. {
  13. return (L + R) / ;
  14. }
  15. };
  16. node tree[maxn];
  17. int Min, Max;
  18.  
  19. void build(int root, int l, int r)
  20. {
  21. tree[root].L = l;
  22. tree[root].R = r;
  23. tree[root].Min = INF;
  24. tree[root].Max = -INF;
  25. if (l == r)
  26. return ;
  27. build (*root+, l, tree[root].Mid());
  28. build (*root+, tree[root].Mid()+, r);
  29. }
  30. void insert (int root, int x, int s)
  31. {
  32. tree[root].Min = min (tree[root].Min, s);
  33. tree[root].Max = max (tree[root].Max, s);
  34. if (tree[root].L == tree[root].R)
  35. return ;
  36. if (x <= tree[root].Mid())
  37. insert (*root+, x, s);
  38. else if (x > tree[root].Mid())
  39. insert (*root+, x, s);
  40. }
  41. void query (int root, int s, int e)
  42. {
  43. if (tree[root].L == s && tree[root].R == e)
  44. {
  45. Min = min (Min, tree[root].Min);
  46. Max = max (Max, tree[root].Max);
  47. return ;
  48. }
  49. if (e <= tree[root].Mid())
  50. query (*root+, s, e);
  51. else if (s > tree[root].Mid())
  52. query (*root+, s, e);
  53. else
  54. {
  55. query (*root+, s, tree[root].Mid());
  56. query (*root+, tree[root].Mid()+, e);
  57. }
  58. }
  59. int main ()
  60. {
  61. int n, q, num;
  62. while (scanf ("%d %d", &n, &q) != EOF)
  63. {
  64. build(, , n);
  65. for (int i=; i<=n; i++)
  66. {
  67. scanf ("%d", &num);
  68. insert (, i, num);
  69. }
  70. while (q --)
  71. {
  72. int s, e;
  73. scanf ("%d %d", &s, &e);
  74. Min = INF;
  75. Max = -INF;
  76. query (, s, e);
  77. printf ("%d\n", Max - Min);
  78. }
  79. }
  80. return ;
  81. }

暑期训练狂刷系列——poj 3264 Balanced Lineup(线段树)的更多相关文章

  1. 暑期训练狂刷系列——poj 3468 A Simple Problem with Integers (线段树+区间更新)

    题目连接: http://poj.org/problem?id=3468 题目大意: 给出n个数,有两种操作: 1:"C a b c",[a,b]中的每一个数都加上c. 2:&qu ...

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

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

  3. POJ 3264 Balanced Lineup 线段树RMQ

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

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

  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 线段树解RMQ

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

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

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

  9. 暑期训练狂刷系列——Hdu 1698 Just a Hook (线段树区间更新)

    题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=1698 题目大意: 有一个钩子有n条棍子组成,棍子有铜银金三种组成,价值分别为1,2,3.为了对付每场 ...

随机推荐

  1. C# 9.0新特性

    CandidateFeaturesForCSharp9 看到标题,是不是认为我把标题写错了?是的,C# 8.0还未正式发布,在官网它的最新版本还是Preview 5,通往C#9的漫长道路却已经开始.前 ...

  2. Openwrt挂载NTFS硬盘提示“只读”错误的解决方法!

    Openwrt是基于Linux代码编写,只支持NTFS格式硬盘的只读权限,否则当挂载的NTFS硬盘写入超过2M左右,就会出现"error:read-only file system" ...

  3. 配置activeMQ

    一.加入以下的库 并配置好路径 ws2_32.lib;Mswsock.lib;cppunit.lib;libapr-1.lib;libapriconv-1.lib;libaprutil-1.lib;l ...

  4. coco2dx新建项目报错,ld: -pie can only be used when targeting iOS 4.2 or later clang: error: linker command

    在新建cocos2d-x以后,执行发现下面错误: ld: -pie can only be used when targeting iOS 4.2 or later clang: error: lin ...

  5. android 不同进程间的调用 AIDL 实现通讯服务

    android 不同进程间的调用  AIDL 实现通讯服务       近期对aidl android 不同进程间的调用,不同运用间的调用做了一些尝试: 过程例如以下: 1:首先在要被调用的程序里写好 ...

  6. [转] logback logback.xml常用配置详解(一)<configuration> and <logger>

    转载文章:原文出处:http://aub.iteye.com/blog/1101260 详细整理了logback常用配置, 不是官网手册的翻译版,而是使用总结,旨在更快更透彻的理解其配置 根节点< ...

  7. ZOJ 3962 E.Seven Segment Display / The 14th Zhejiang Provincial Collegiate Programming Contest Sponsored by TuSimple E.数位dp

    Seven Segment Display Time Limit: 1 Second      Memory Limit: 65536 KB A seven segment display, or s ...

  8. SSH无密码验证可能出现的问题

    雪影工作室版权所有,转载请注明[http://blog.csdn.net/lina791211] 一.安装和启动SSH协议 假设没有安装ssh和rsync,可以通过下面命令进行安装. sudo apt ...

  9. select case when if

    select case when if 的一些用法 - 马丁传奇 - 博客园 https://www.cnblogs.com/martinzhang/p/3220595.html Write a SQ ...

  10. 强类型DataSet (2011-12-30 23:16:59)转载▼ 标签: 杂谈 分类: Asp.Net练习笔记 http://blog.sina.com.cn/s/blog_9d90c4140101214w.html

    强类型DataSet (2011-12-30 23:16:59) 转载▼ 标签: 杂谈 分类: Asp.Net练习笔记 using System; using System.Collections.G ...