2019独角兽企业重金招聘Python工程师标准>>>

题目在此

解题思路:查询区间最大值/最小值之差,最基础的线段树应用。

代码:


#include <cstdio>// 线段树为完全二叉树,因此可用 root * 2 和 root * 2 + 1
// 来求得左右孩子节点的位移
#define L(root) ((root) << 1)
#define R(root) (((root) << 1) + 1)
#define MAX(a, b) ((a) > (b) ? (a) : (b))
#define MIN(a, b) ((a) < (b) ? (a) : (b))const int MAXN = 50001;
int cows[MAXN];
int maxn, minn;struct st {// 左右区间int left, right;// 区间最大、最小值int max, min;
} st[MAXN * 4];// 建树
void build(int root, int l, int r) {// 区间为叶子节点时,初始化各值后返回st[root].left = l, st[root].right = r;if (l == r) {st[root].max = st[root].min = cows[l];return;}// 否则,递归构建左右节点int m = l + ((r - l) >> 1);build(L(root), l, m);build(R(root), m + 1, r);st[root].max = MAX(st[L(root)].max, st[R(root)].max);st[root].min = MIN(st[L(root)].min, st[R(root)].min);
}// 查询
void query(int root, int l, int r) {// 查询范围与节点区间范围恰好重合if (st[root].left == l && st[root].right == r) {maxn = MAX(maxn, st[root].max);minn = MIN(minn, st[root].min);return;}// 否则,将区间一分为二int m = st[root].left + ((st[root].right - st[root].left) >> 1);// 查询范围“落”在左子节点区间if (r <= m) {query(L(root), l, r);// 查询范围“落”在右子节点区间} else if (l > m) {query(R(root), l, r);// 左右子节点区间各有一部分被查询范围覆盖:// 将查询范围一分为二,分别查询左右子节点} else {query(L(root), l, m);query(R(root), m + 1, r);}
}int main() {int N, Q;while (scanf("%d%d", &N, &Q) != EOF) {for (int i = 1; i <= N; ++i) {scanf("%d", &cows[i]);}build(1, 1, N);int l, r;while (Q--) {scanf("%d%d", &l, &r);maxn = 0, minn = 0x7FFFFFFF;query(1, l, r);printf("%d\n", maxn - minn);}}return 0;
}

转载于:https://my.oschina.net/Alexanderzhou/blog/204056

POJ 3264: Balanced Lineup相关推荐

  1. POJ 3264 Balanced Lineup

    POJ 3264 Balanced Lineup 题目链接 Description For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,00 ...

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

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

  3. POJ 3264 Balanced Lineup

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 53629   Accepted: 25223 ...

  4. poj 3264 Balanced Lineup RMQ问题 线段树

    For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One d ...

  5. POJ 3264 Balanced Lineup(RMQ)

    Balanced Lineup Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 24349   Accepted: 11348 ...

  6. POJ 3264 Balanced Lineup 【线段树】

    Balanced Lineup Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 50004 Accepted: 23434 Cas ...

  7. 【RMQ】POJ 3264 Balanced Lineup

    前言 这题出现在RMQRMQRMQ的WordWordWord文档中,我就直接把以前ACACAC过的程序交上去,结果WAWAWA了,然后才发现这道题还要求最小值... 链接 http://poj.org ...

  8. POJ 3264 Balanced Lineup (RMQ)

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

  9. POJ 3264.Balanced Lineup-RMQ(ST)详解

    先写一道水题的博客,为后面要写的博客做一个铺垫. ヾ(◍°∇°◍)ノ゙ RMQ(Range Minimum/Maximum Query),即区间最值查询,对于长度为n的数列A,回答若干询问RMQ(A, ...

最新文章

  1. 简单介绍Python中异常处理用法
  2. java执行cmd命令,返回结果中文乱码问题解决
  3. JS 事件冒泡和事件捕获
  4. 四个标志位SF,ZF,OF,CF
  5. Activity功能控件
  6. activiti jbpm相关资源
  7. 专访涯海:阿里云中间件是如何支撑双11的?
  8. java swing 图片旋转_Java图片旋转,背景透明化
  9. BootStrap_04之jQuery插件(导航、轮播)、以及Less
  10. Photoshop 2019 破解
  11. 服务器系统补丁失败,Windows Server 2016安装累积更新补丁失败
  12. sqlite developer过期解决办法
  13. 华滋先生:互联网创业,加入社群是有用的吗?
  14. 2021年高处安装、维护、拆除新版试题及高处安装、维护、拆除考试试卷
  15. 今日立春,介绍一些立春的习俗吧
  16. AI专家Raj Reddy建议:政府应免费发放智能手机
  17. 计算机主机的跳线怎么接,手把手教你台式机电脑主板跳线接法
  18. 9.3.2 自然连接(NATURAL JOIN)
  19. 领英进阶技巧-google和LinkedIn结合
  20. 新媒体人必备的10个效率工具,神器收藏起来

热门文章

  1. less新手入门(四)—— Mixin Guards
  2. .gitignore过滤规则
  3. Jetty开发指导:框架
  4. input添加float样式后位置偏移的解决
  5. opencv 图像雾检测_OpenCV图像处理-基于OpenPose的关键点检测
  6. centos6 安装 mantisbt-1.2.8 —— (5)Mantisbt-1.2.8在contos上的安装
  7. 关于python 类的使用
  8. eclipse解决maven编码UTF-8的不可映射字符
  9. BZOJ 2793: [Poi2012]Vouchers(调和级数)
  10. OGG 跳过事务(转)