首页 > 其他分享 >P3865 ST表 学习笔记

P3865 ST表 学习笔记

时间:2022-12-25 10:00:10浏览次数:33  
标签:le max ll 笔记 ST include 预处理 P3865

题意

给定一个长度为 \(N\) 的数列,和 \(M\) 次询问,求出每一次询问的区间内数字的最大值。

对于 \(100\%\) 的数据,满足 \(1\le N\le {10}^5\),\(1\le M\le 2\times{10}^6\),\(a_i\in[0,{10}^9]\),\(1\le l_i\le r_i\le N\)。

思路

什么是ST表

ST表,是一种用来处理RMQ(区间最值问题)的算法。ST表可以做到 \(\mathcal{O}(n\log{n})\) 预处理,之后 \(\mathcal{O}(1)\) 查询, ST表的空间复杂度也是 \(\mathcal{O}(n\log{n})\) 的。

如何实现ST表和它的原理

预处理

我们定义 \(ST_{i,j}\),为从第 \(i\) 个位置开始之后的 \(2^j\) 个位置的区间最大值。 我们知道:\(2^k=2^{k-1}+2^{k-1}\),所以在预处理时也一样。ST表有些类似于dp的思想。 \(ST_{i,j}=\max(ST_{i,j-1},ST_{i+2^{j-1},j-1})\)

\[\underbrace{i,i+1,i+2,\cdots,i+2^{j-1}-1}_{\text{max里的第一部分}}\underbrace{,i+2^{j-1},i+2^{j-1}+1,\cdots,i+2^{j-1}+2^{j-1}-1}_{\text{max里的第二部分}} \]

这样就可以从小合大了。

查询

查询的话也很简单,求一下 \(k= \log{(r-l+1)}\),也就是区间长度的覆盖。然后因为向下取整,不可以直接 \(ST_{l,k}\),而是要用 \(r-2^k+1\) 重叠上去。不明白 \(r-2^k+1\)的话就是从终点开始往中心走那么多格子,这样的话一定可以和 \(ST_{l,k}\) 接上。

tips

需要注意一些边界,预处理的时候不要少处理,空间也要预留够。

代码

#include<cstdio>
#include <set>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <cmath>
using namespace std;
typedef long long ll;
const ll MAXN=1e5+5;
ll st[MAXN][40];
ll query(ll l,ll r){
    ll k= ll(log2(r-l+1));
    return max(st[l][k],st[r-(1<<k)+1][k]);
}
ll n,m,l,r;
int main(){
    cin>>n>>m;
    for (int i = 1; i <=n ; ++i) {
        scanf("%lld",&st[i][0]);
    }
    for (int k = 1; k <=log2(MAXN) ; ++k) {
        for (int i = 1; i+(1<<k)-1<=n ; ++i) {
            st[i][k]= max(st[i][k-1],st[i+(1<<(k-1))][k-1]);
        }
    }
    for (int i = 1; i <=m ; ++i) {
        scanf("%lld%lld",&l,&r);
        printf("%lld\n", query(l,r));
    }
    return 0;
}

标签:le,max,ll,笔记,ST,include,预处理,P3865
From: https://www.cnblogs.com/tanghg/p/17003695.html

相关文章

  • Flutter statecontroller.update(MaterialState.disabled,false)无效
    因为中间会调用voidinitStatesController(){if(widget.statesController==null){internalStatesController=MaterialStatesController();}......
  • UVA12459 Bees' ancestors 题解
    题目传送门题目大意雌蜂有一个父亲一个母亲,而雄蜂只有母亲。计算出Willy的祖先中,哪一代有多少祖先。解题思路已知Willy为雄蜂,从Willy开始向前推:有一个母亲(1);......
  • AT_past202010_b 電卓 题解
    题目传送门题目大意给定\(x\)和\(y\),求$\dfrac{x}{y}$。舍弃小数点后第三及以下位。解题思路首先判断$\dfrac{x}{y}$是否可以成立,也就是判断\(y\)是否等于......
  • AT_past202010_b 電卓 翻译
    题目传送门题目描述在你的计算器上输入非负整数$X,\Y$,然后以$\frac{X}{Y}$为开头,没有多余的$0$,小数点后第\(3\)及以下位的数全部舍弃,显示到小数点后第\(2\)......
  • AT_past202010_a 中央値 题解
    题目传送门题目大意输入三个数,输出他们的中第二大的数的编号(这三个数的编号分别用ABC来表示)。解题思路将这三个数赋给另外三个数,再将这三个数按冒泡的思想排好序(so......
  • [LeetCode] 1754. Largest Merge Of Two Strings
    Youaregiventwostrings word1 and word2.Youwanttoconstructastring merge inthefollowingway:whileeither word1 or word2 arenon-empty,choos......
  • Continual Learning with Transformers for Image Classification---阅读笔记
    ContinualLearningwithTransformersforImageClassification---阅读笔记摘要:阻止灾难性遗忘是一件很困难的事,一个最近的研究趋势是动态扩展参数可以有效的减少灾难......
  • AtCoder Beginner Contest 283
    A-Power(abc283a)题目大意给定\(A,B\),输出\(A^B\)解题思路数不大,暴力即可。数大了可用快速幂。神奇的代码#include<bits/stdc++.h>usingnamespacestd;us......
  • pytest用例管理框架
    先安装pipinstallpytestpytest用例管理框架默认规则:1.py文件必须以test_开头或者_test结尾2.类名必须以test开头3.测试用例必须以test_开头 get请求通过params......
  • 在线视频项目笔记总结
    一、管理员登录接口1.业务逻辑见代码:controller层@RestController@Slf4jpublicclassAdminController{@AutowiredprivateAdminServiceadminService;......