AOJ 351 rmq或 基础线段树 求解
题目链接: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或 基础线段树 求解相关推荐
- HDU5726 线段树求解区间GCD
GCD Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submi ...
- 【luogu1816】忠(RMQ问题、线段树)
题面 RMQ问题 题解 线段树 #include<iostream> #include<algorithm> using namespace std; const int ma ...
- 蓝书4.1-4.4 树状数组、RMQ问题、线段树、倍增求LCA
这章的数据结构题很真实 T1 排队 bzoj 1699 题目大意: 求静态一些区间的最大值-最小值 思路: ST表裸题 1 #include<iostream> 2 #include< ...
- UVA 12299 RMQ with Shifts 线段树
简单的单点更新 //#pragma comment(linker, "/STACK:1024000000,1024000000") #include<cstdio> # ...
- 【HDU - 5875】Function(线段树,区间第一个小于某个数的数 或 RMQ二分)
题干: The shorter, the simpler. With this problem, you should be convinced of this truth. You a ...
- Poj 3246 Balanced Lineup(线段树基础)
依旧是线段树基础题 询问区间的最大值和最小值之差,只有询问,没有插入删除.继续理解基础线段树 #include <iostream> #include <algorithm> ...
- 线段树 --算法竞赛专题解析(24)
本系列文章将于2021年整理出版.前驱教材:<算法竞赛入门到进阶> 清华大学出版社 网购:京东 当当 作者签名书:点我 有建议请加QQ 群:567554289 文章目录 1. 线段树概 ...
- 0x43.数据结构进阶 - 线段树
目录 一.基础线段树 线段树的建树 线段树的单点修改 线段树的区间查询 线段树的延迟标记(懒惰标记) 1.POJ3486 ASimpleProblemwithIntegersA\ Simple\ Pr ...
- 这是我第一题AC的线段树
题目简述: 有N个整数,Q次操作,每次操作为询问一个区间[a, b]内数的和(0号操作)或者把一个区间内的数全部加上v(1号操作) 线段树求解即可. #include <cstdio> # ...
最新文章
- 英文书《用unreal来学习c++》_用机器学习来提升你的用户增长:第四步,客户流失预测
- Android Stadio 所有的窗口都没有了
- 面试题-Category(分类)
- 【深度学习】基于Pytorch进行深度神经网络计算(二)
- springboot HBase操作工具类
- 植树节快乐|用小学数学到高数的知识思考种树,你能种到哪一步?
- 我的MVC之旅(3)--------MVC Music Store 第三篇 Views and ViewModels [翻译]
- [2-sat][topsort输出解] POJ 3648 Wedding
- python实现艾宾浩斯背单词功能,实现自动提取单词、邮件发送,部署在阿里云服务器,再也不用担心背单词啦!!
- jade 编译 html,学习前端模板引擎 jade (一)_html/css_WEB-ITnose
- RF+ Appium,如何隐藏Android的键盘?
- 计算适应度函数(目标函数)(单目标)
- 带宽,线速以及吞吐量的概念
- MS Windows 7 快捷键大全
- IDEA Java代码注释规范
- macOS上的改键指南--使用Karabiner
- 在Windows电脑上修改图片内存大小的方法
- Win11安装Android子系统
- 核磁共振分析处理软件:Nucleomatica iNMR for Mac
- Opencv中flip函数讲解
热门文章
- Python之Excel使用实现BOM表自动生成工具
- SSM-SpringMVC过滤器
- MYSQL Error: 1089 - Incorrect sub part key; the used key part isn't a string...
- 犀牛书第七版学习笔记:let、const和 var 声明与赋值
- 《胡小闹日记》——深刻的教育理念,重塑家长们的教育观
- IE内存泄露监测工具sIEve
- Python如何添加下载源
- 1. 微信公众号的申请
- 微信支付多用户并发使用mweb_url
- ICPC 山东省省赛刷题 第十八届西南科技大学ACM程序设计竞赛(同步赛)牛客 BFH题解