st表模板
这篇博客讲解的很详细了,求区间最大值也可以用st表,时间复杂度O(n log(n)),查询复杂度O(1)
主要是用到区间动规 的思想(虽然我不会区间动规= =)
在求解rmq问题时,st表是很有用的
-------------------------**********************-------------------------------------
要注意的是st表只能离线处理,要在线处理必须要用树状数组或者线段树
-------------------------**********************--------------------------------------
一道模板题lightoj1082
#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<cstdio>
#include<cassert>
#include<iomanip>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#define C 0.5772156649
#define pi acos(-1.0)
#define ll long long
#define mod 1000000007
#define ls l,m,rt<<1
#define rs m+1,r,rt<<1|1
#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; const double g=10.0,eps=1e-;
const int N=+,maxn=+,inf=0x3f3f3f; int st[N][];
int n;
void init()
{
for(int i=;(<<i)<=n;i++)
for(int j=;j<=n-(<<i)+;j++)
st[j][i]=min(st[j][i-],st[j+(<<(i-))][i-]);
}
int get(int l,int r)
{
int s=log2(r-l+);
return min(st[l][s],st[r-(<<s)+][s]);
}
int main()
{
/*ios::sync_with_stdio(false);
cin.tie(0);*/
int t,m,cnt=;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=; i<=n; i++)scanf("%d",&st[i][]);
init();
printf("Case %d:\n",++cnt);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",get(a,b));
}
}
return ;
}
/********************
5
1 5 3 4 2
********************/
st表模板的更多相关文章
- Luogu P2880 [USACO07JAN]平衡的阵容Balanced Lineup (ST表模板)
传送门(ST表裸题) ST表是一种很优雅的算法,用于求静态RMQ 数组l[i][j]表示从i开始,长度为2^j的序列中的最大值 注意事项: 1.核心部分: ; (<<j) <= n; ...
- 洛谷 P2880 [USACO07JAN]Balanced Lineup G (ST表模板)
题意:给你一组数,询问\(q\)次,问所给区间内的最大值和最小值的差. 题解:经典RMQ问题,用st表维护两个数组分别记录最大值和最小值然后直接查询输出就好了 代码: int n,q; int a[N ...
- ST表模板 Balanced Lineup POJ3264
http://poj.org/problem?id=3264 题意 rmq max min之差 模板: #define _CRT_SECURE_NO_WARNINGS #include<cmat ...
- 收藏一个ST表模板
#include<iostream> #include<cstdio> #include<algorithm> #include<cstring> #i ...
- 51nod(1174 区间中最大的数)(ST表模板题)
1174 区间中最大的数 1.0 秒 131,072.0 KB 0 分 基础题 给出一个有N个数的序列,编号0 - N - 1.进行Q次查询,查询编号i至j的所有数中,最大的数是多少. 例如: 1 ...
- 【模板】ST表
给定一个长度为 \(N\) 的数列,和 \(M\) 次询问,求出每一次询问的区间\([l,r]\)内数字的最大值. 说明 对于30%的数据,满足: \(1 \leq N, M \leq 10 , 1≤ ...
- 【Luogu】P3865ST表模板(ST表)
题目链接 本来准备自己yy一个倍增来着,然而一看要求O1查询就怂了. ST表模板.放上代码. #include<cstdio> #include<cstdlib> #inclu ...
- RMQ求解->ST表
ST表 这是一种神奇的数据结构,用nlogn的空间与nlongn的预处理得出O(1)的区间最大最小值(无修) 那么来看看这个核心数组:ST[][] ST[i][j]表示从i到i+(1<<j ...
- POJ 3264 Balanced Lineup | st表
题意: 求区间max-min st表模板 #include<cstdio> #include<algorithm> #include<cstring> #inclu ...
随机推荐
- Python菜鸟之路:Django 文件上传的几种方式
方式一:通过form表单中,html input 标签的“file”完成 # 前端代码uoload.html <form method="post" action=" ...
- css的常用知识点
一.css的引入方式 1.行内引入 直接在标签中定义样式 <p style="background-color: red">test</p> 2.嵌入式引入 ...
- js中window.open的参数及注意
IE9下使用window.open时需要注意name参数值不能有"-"出现,否则会出现脚本错误,IE9以及版本测试没有问题 window.open(URL,name,specs ...
- 正则表达式 \b
引用网上一段话: \b 是正则表达式规定的一个特殊代码(好吧,某些人叫它元字符,metacharacter),代表着单词的开头或结尾,也就是单词的分界处.虽然通常英文的单词是由空格,标点符号或者换行来 ...
- Linux network namespace源码分析
一.network namespace的创建 在对iproute2的源码进行分析后,我们可以知道,当我们调用命令`ip netns add ns1`时,本质上就是调用`unshare(CLONE_NE ...
- DateTimeTypeHandler
mysql-timestimp-------model-DateTime(jodatime) public class DataTimeTypeHandler extends BaseTypeHand ...
- 关于shared pool的深入探讨(三)
基本命令: ALTER SESSION SET EVENTS 'immediate trace name LIBRARY_CACHE level LL'; 其中LL代表Level级别,对于9.2.0及 ...
- Java集合(3):Vector && Stack
一.Vector介绍 Vector可以实现可增长的动态对象数组.与数组一样,它包含可以使用整数索引进行访问的组件.不过,Vector的大小是可以增加或者减小的,以便适应创建Vector后进行添加或者删 ...
- smarty内置函数
1.{append} 追加 2.{assign} 赋值 3.{block} 块 4.{call} 调用 5.{capture}捕获 6.{config_load}用来从配置文件中加载config变 ...
- LeetCode:N叉树的前序遍历【589】
LeetCode:N叉树的前序遍历[589] 题目描述 给定一个 N 叉树,返回其节点值的前序遍历. 例如,给定一个 3叉树 : 返回其前序遍历: [1,3,5,6,2,4]. 题目分析 使用栈结构. ...