RMQ算法 模板
来源:互联网 发布:绵阳哪有mac魅可专柜 编辑:程序博客网 时间:2024/05/20 13:16
RMQ :用于求一个区间内的最大最小值
ST算法代码
时间复杂度:预处理O(n * log n) 查询O(1)
#include <stdio.h> #include <iostream> #include <string.h> #include <stack> #include <algorithm> #include <queue> #include <map> #include <cmath> #define eps 0.000001 #define pi acos(-1,0) #define pr 999983 #define LL long long using namespace std; int n,m; int _2[20]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192}; int a[10000][100]; void RMQ(){ int z=0; while (_2[z]<=n) z++; for (int i = 1; i < z; i++){ for (int j = 0; j <= n-_2[i]; j++){ a[j][i]=min(a[j][i-1],a[j+_2[i-1]][i-1]); } } } int main(){ while (~scanf("%d",&n)){ for (int i = 0; i < n; i++){ scanf("%d",&a[i][0]); } RMQ(); scanf("%d",&m); while (m--){ int l,r,x; scanf("%d%d",&l,&r); x=(int)(log(r-l+1)/log(2.0)); printf("%d\n",min(a[l][x],a[r-(1<<x)+1][x])); } } return 0; }
不知名大神代码
时间复杂度:预处理O(n * log n) 查询O(log n)
#include <stdio.h>#include <iostream>#include <string.h>#include <stack>#include <algorithm>#include <queue>#include <map>#include <cmath>#define eps 0.000001#define pi acos(-1,0)#define pr 999983#define LL long longusing namespace std;int n,m;int _2[20]={1,2,4,8,16,32,64,128,256,512,1024,2048,4096,8192};int a[10000][100];int fd(int l,int x){ int z=0; while (_2[z]<=x) z++; z--; if (_2[z]<x) return min(a[l][z],fd(l+_2[z],x-_2[z])); return a[l][z];}void RMQ(){int z=0; while (_2[z]<=n) z++; for (int i = 1; i < z; i++){ for (int j = 0; j <= n-_2[i]; j++){ a[j][i]=min(a[j][i-1],a[j+_2[i-1]][i-1]); } }} int main(){ while (~scanf("%d",&n)){ for (int i = 0; i < n; i++){ scanf("%d",&a[i][0]); } RMQ(); scanf("%d",&m); while (m--){ int l,r,x; scanf("%d%d",&l,&r); x=r-l+1; printf("%d\n",fd(l-1,x)); } } return 0;}
0 0
- rmq算法模板
- RMQ算法模板
- ST算法(RMQ模板)
- RMQ ST 算法模板
- RMQ算法模板
- RMQ(ST算法)模板
- ST算法 rmq 模板
- RMQ算法 模板
- RMQ ST算法模板
- rmq算法详解 模板
- 算法模板之RMQ
- RMQ中的ST算法模板
- rmq的st算法及模板
- LCA转RMQ 在线算法模板
- RMQ(st在线算法模板)
- hihoCoder 1068 RMQ-ST算法模板题
- codeforces 361D rmq st算法模板
- RMQ转换LCA模板 ST算法
- 微信小程序初体验-项目基本结构
- IOS使用AVAudioPlayer播放mp3歌曲文件并监听来电打断
- 在VPS上搭建Shadowsocks代理服务器
- Java(随机往mysql中导入一亿条数据)
- cache和buffer
- RMQ算法 模板
- extjs 树节点操作
- Python基础数据结构之大循环(for list,set,dict,tuple)
- [HDU](2099)整除的尾数
- 七牛操作流程
- ssh/sftp无需密码登录配置
- Android 操作SQLite基本用法
- 自定义栈
- 为什么数据库有时候不能定位阻塞(Blocker)源头的SQL语句