题目链接:AOJ 351

RMQ:

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#include<cmath>
using namespace std;
#define debug 0const int maxn = 100000 + 5;
int a[maxn],n,m,Max[maxn][20],Min[maxn][20];void initRMQ() {for (int i = 1; i <= n; i++) {Max[i][0] = Min[i][0] = a[i];}for(int j = 1; j < 20; j++)//初始化for(int i = 1; i <= n; i++)if (i + (1 << j) - 1 <= n){Max[i][j] = max(Max[i][j - 1], Max[i + (1 << (j - 1))][j - 1]);Min[i][j] = min(Min[i][j - 1], Min[i + (1 << (j - 1))][j - 1]);}
}int main() {
#if debugfreopen("in.txt", "r", stdin);
#endif//debugint l, r;while (~scanf("%d%d", &n, &m)){for (int i = 1; i <= n; i++)scanf("%d", &a[i]);initRMQ();while (m--){scanf("%d%d", &l, &r);int x = (int)log2(r - l + 1);//query查询
            int maxAns = max(Max[l][x], Max[r - (1 << x) + 1][x]);int minAns = min(Min[l][x], Min[r - (1 << x) + 1][x]);printf("%d\n", maxAns - minAns);}}return 0;
}

线段树:

#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<stack>
using namespace std;
#define debug 0const int maxn = 100000 + 5;
int  Max[maxn << 2], Min[maxn << 2];
int n, m, ans1, ans2;
#define Mid ((l + r) >> 1)
#define ls  rt << 1, l , Mid
#define rs  rt << 1 | 1, Mid + 1 , r
void build(int rt, int l, int r) {if (l == r){scanf("%d", &Max[rt]);//建树时读入Min[rt] = Max[rt];}else {build(rt << 1, l, (l + r) >> 1);build(rt << 1 | 1, ((l + r) >> 1) + 1, r);Max[rt] = max(Max[rt << 1], Max[rt << 1 | 1]);Min[rt] = min(Min[rt << 1], Min[rt << 1 | 1]);}
}void  query(int rt, int l, int r, int L, int R) {//查询if (L <= l && r <= R) {ans1 = max(ans1, Max[rt]);ans2 = min(ans2, Min[rt]);}else {if (L <= ((l + r) >> 1)){query(ls, L, R);}if (R > ((l + r) >> 1)) {query(rs, L, R);}}
}int main() {
#if debugfreopen("in.txt", "r", stdin);
#endif //debugint a, b;while (~scanf("%d%d", &n, &m)) {build(1, 1, n);while (m--) {ans1 = -0x3f3f3f3f;ans2 = 0x3f3f3f3f;//printf("%d\n", m);scanf("%d%d", &a, &b);query(1, 1, n, a, b);printf("%d\n", ans1 - ans2);}}return 0;
}

AOJ 351 rmq或 基础线段树 求解相关推荐

  1. HDU5726 线段树求解区间GCD

    GCD Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Total Submi ...

  2. 【luogu1816】忠(RMQ问题、线段树)

    题面 RMQ问题 题解 线段树 #include<iostream> #include<algorithm> using namespace std; const int ma ...

  3. 蓝书4.1-4.4 树状数组、RMQ问题、线段树、倍增求LCA

    这章的数据结构题很真实 T1 排队 bzoj 1699 题目大意: 求静态一些区间的最大值-最小值 思路: ST表裸题 1 #include<iostream> 2 #include< ...

  4. UVA 12299 RMQ with Shifts 线段树

    简单的单点更新 //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> # ...

  5. 【HDU - 5875】Function(线段树,区间第一个小于某个数的数 或 RMQ二分)

    题干: The shorter, the simpler. With this problem, you should be convinced of this truth.        You a ...

  6. Poj 3246 Balanced Lineup(线段树基础)

    依旧是线段树基础题 询问区间的最大值和最小值之差,只有询问,没有插入删除.继续理解基础线段树 #include <iostream> #include <algorithm> ...

  7. 线段树 --算法竞赛专题解析(24)

    本系列文章将于2021年整理出版.前驱教材:<算法竞赛入门到进阶> 清华大学出版社 网购:京东 当当   作者签名书:点我 有建议请加QQ 群:567554289 文章目录 1. 线段树概 ...

  8. 0x43.数据结构进阶 - 线段树

    目录 一.基础线段树 线段树的建树 线段树的单点修改 线段树的区间查询 线段树的延迟标记(懒惰标记) 1.POJ3486 ASimpleProblemwithIntegersA\ Simple\ Pr ...

  9. 这是我第一题AC的线段树

    题目简述: 有N个整数,Q次操作,每次操作为询问一个区间[a, b]内数的和(0号操作)或者把一个区间内的数全部加上v(1号操作) 线段树求解即可. #include <cstdio> # ...

最新文章

  1. 英文书《用unreal来学习c++》_用机器学习来提升你的用户增长:第四步,客户流失预测
  2. Android Stadio 所有的窗口都没有了
  3. 面试题-Category(分类)
  4. 【深度学习】基于Pytorch进行深度神经网络计算(二)
  5. springboot HBase操作工具类
  6. 植树节快乐|用小学数学到高数的知识思考种树,你能种到哪一步?
  7. 我的MVC之旅(3)--------MVC Music Store 第三篇 Views and ViewModels [翻译]
  8. [2-sat][topsort输出解] POJ 3648 Wedding
  9. python实现艾宾浩斯背单词功能,实现自动提取单词、邮件发送,部署在阿里云服务器,再也不用担心背单词啦!!
  10. jade 编译 html,学习前端模板引擎 jade (一)_html/css_WEB-ITnose
  11. RF+ Appium,如何隐藏Android的键盘?
  12. 计算适应度函数(目标函数)(单目标)
  13. 带宽,线速以及吞吐量的概念
  14. MS Windows 7 快捷键大全
  15. IDEA Java代码注释规范
  16. macOS上的改键指南--使用Karabiner
  17. 在Windows电脑上修改图片内存大小的方法
  18. Win11安装Android子系统
  19. 核磁共振分析处理软件:Nucleomatica iNMR for Mac
  20. Opencv中flip函数讲解

热门文章

  1. Python之Excel使用实现BOM表自动生成工具
  2. SSM-SpringMVC过滤器
  3. MYSQL Error: 1089 - Incorrect sub part key; the used key part isn't a string...
  4. 犀牛书第七版学习笔记:let、const和 var 声明与赋值
  5. 《胡小闹日记》——深刻的教育理念,重塑家长们的教育观
  6. IE内存泄露监测工具sIEve
  7. Python如何添加下载源
  8. 1. 微信公众号的申请
  9. 微信支付多用户并发使用mweb_url
  10. ICPC 山东省省赛刷题 第十八届西南科技大学ACM程序设计竞赛(同步赛)牛客 BFH题解