【学习笔记】ST表

【学习笔记】ST表

给狂妄自负以适当的绝望,这就是真理

  • RMQ问题
    给定一个长度为N的区间,M个询问,每次询问Li到Ri这段区间元素的最大值/最小值。
    如果暴力找最大值,复杂度是o(n)。但如果查询多次,这个复杂度就很大了。
    解决这个问题的方法是离线ST表和支持在线修改的线段树。
  • ST表:一种利用dp求解区间最值的倍增算法。

  • 定义:f[i][j]表示i到i+2^j-1这段区间的最大值。

  • 预处理:f[i][0]=a[i]。即i到i区间的最大值就是a[i]。

    • 状态转移:将f[i][j]平均分成两段,一段为f[i][j-1],另一段为f[i+2^(j-1)][j-1]。

    • 两段的长度均为2^j-1。f[i][j]的最大值即这两段的最大值中的最大值。

\[ f[i][j]=max(f[i][j-1],f[i+2^{j-1}][j-1]) \]

  • 查询:需要查询的区间为[i,j],则需要找到两个覆盖这个闭区间的最小幂区间。
    这两个区间可以重复,因为两个区间是否相交对区间最值没有影响。(如下图)
    image

模板题:Balanced Lineup

题目描述:

For the daily milking, Farmer John's N cows (1 ≤ N ≤ 50,000) always line up in the same order. One day Farmer John decides to organize a game of Ultimate Frisbee with some of the cows. To keep things simple, he will take a contiguous range of cows from the milking lineup to play the game. However, for all the cows to have fun they should not differ too much in height.
Farmer John has made a list of Q (1 ≤ Q ≤ 200,000) potential groups of cows and their heights (1 ≤ height ≤ 1,000,000). For each group, he wants your help to determine the difference in height between the shortest and the tallest cow in the group.

输入:

Line 1: Two space-separated integers, N and Q.
Lines 2.. N+1: Line i+1 contains a single integer that is the height of cow i
Lines N+2.. N+ Q+1: Two integers A and B (1 ≤ A ≤ B ≤ N), representing the range of cows from A to B inclusive.

输出:

Lines 1.. Q: Each line contains a single integer that is a response to a reply and indicates the difference in height between the tallest and shortest cow in the range.

样例

6 3
1
7
3
4
2
5
1 5
4 6
2 2

6
3
0

#include <algorithm>
#include <cstdio>
#define ll long long
using namespace std;
const int maxn=1e5;
int stmax[maxn][20],stmin[maxn][20];
int poww[25],logg[maxn];
int n,q;
void init()
{
    poww[0]=1;
    for(int i=1; i<=20; i++)//预处理次方
    {
        poww[i]=poww[i-1]<<1;
    }
    for(int i=2; i<=n; i++)
    {
        logg[i]=logg[i>>1]+1;
    }
    int temp=1;
    for(int j=1; j<=logg[n]; j++)//temp=2^(j-1)
    {
        for(int i=1; i<=n-temp-temp+1; i++)
        {
            stmax[i][j]=max(stmax[i][j-1],stmax[i+temp][j-1]);
            stmin[i][j]=min(stmin[i][j-1],stmin[i+temp][j-1]);
        }
        temp<<=1;
    }
}
inline int query_min(int l,int r)
{
    int len=r-l+1;
    int k=logg[len];
    return min(stmin[l][k],stmin[r-poww[k]+1][k]);
}
inline int query_max(int l,int r)
{
    int len=r-l+1;
    int k=logg[len];
    return max(stmax[l][k],stmax[r-poww[k]+1][k]);
}
int main()
{
    int a;
    scanf("%d%d",&n,&q);
    for(int i=1;i<=n;i++){
        scanf("%d",&a);
        stmax[i][0]=stmin[i][0]=a;
    }
    init();
    int l,r;
    while(q--){
        scanf("%d%d",&l,&r);
        printf("%d\n",query_max(l,r)-query_min(l,r));
    }
    return 0;
}
  • LCA+ST
    用dfs序表示2*n的数组,数组的值是深度。fir表示第一次遍历到这个点的dfs序,cur表示这个dfs序对应的节点。lca就是l到r区间深度最小的节点。
#include <bits/stdc++.h>
using namespace std;
#define ll long long
const int maxn=5e5+10;//点的个数
int cur[maxn<<1];// cur 当前dfs序的点
int fir[maxn];//fir 第一次遍历的 dfs序
int rmq[maxn<<1];//rmq 深度
struct Edge{
    int to,next;
}e[maxn<<1];
int head[maxn],tol,cnt;
struct St{
    int stm[maxn<<1][21];//stm dfs序列上深度最小的点
    int logg[maxn<<1],poww[21];
    void init(int n) {
        poww[0]=1;logg[0]=-1;
        for(int i=1; i<=20; i++)
        {
            poww[i]=poww[i-1]<<1;
        }
        for (int i = 1; i <= n; i++) {
            logg[i]=logg[i>>1]+1;
            stm[i][0]=i;
        }
        int temp=1;
        for (int j = 1; j <=logg[n]; ++j) {
            for (int i = 1; i <=n-temp-temp+1; ++i) {
                stm[i][j]=rmq[stm[i][j-1]]<rmq[stm[i+temp][j-1]]?stm[i][j-1]:stm[i+temp][j-1];
            }
            temp<<=1;
        }
    }
    int Query(int a,int b){
        if(a>b) swap(a,b);
        int k=logg[b-a+1];
        return rmq[stm[a][k]]<=rmq[stm[b-poww[k]+1][k]]?stm[a][k]:stm[b-poww[k]+1][k];
    }
}st;
void add(int u,int v){
    e[++tol].to=v;
    e[tol].next=head[u];
    head[u]=tol;
}
void dfs(int u,int pre,int dep){
    cur[++cnt]=u;
    rmq[cnt]=dep;
    fir[u]=cnt;
    for(int i=head[u];i;i=e[i].next){
        int v=e[i].to;
        if(v==pre) continue;
        dfs(v,u,dep+1);
        cur[++cnt]=u;
        rmq[cnt]=dep;
    }
}
void cal(int roof,int n){
    dfs(roof,roof,0);
    st.init(n);
}
int query_lca(int a,int b){
    return cur[st.Query(fir[a],fir[b])];
}
int main(){
     int n,m,s,u,v;
     scanf("%d%d%d",&n,&m,&s);
    for (int i = 1; i < n; ++i) {
        scanf("%d%d",&u,&v);
        add(u,v);
        add(v,u);
    }
    cal(s,(n*2)-1);
    while (m--){
        scanf("%d%d",&u,&v);
        printf("%d\n",query_lca(u,v));
    }
}

转载于:https://www.cnblogs.com/smallocean/p/9575850.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在风能领域,准确预测风速对于风电场的运行与管理至关重要。Matlab作为一个强大的数学计算和数据分析平台,被广泛应用于风速预测模型的构建。本文将深入探讨基于四种风速——随机风、基本风、阵风和渐变风的组合风速预测技术。 我们来理解这四种风速类型: 1. **随机风**:随机风是指风速呈现出随机性的变化,通常由大气湍流引起。在建模中,通常通过统计方法如高斯分布或Weibull分布来模拟这种不确定性。 2. **基本风**:基本风速是指在无特定扰动条件下的平均风速,它是长期观测结果的平均值,通常用于结构设计和风能评估。 3. **阵风**:阵风是短时间内风速显著增强的现象,对建筑物和风力发电机造成的主要威胁之一。阵风的预测涉及到风的脉动特性分析。 4. **渐变风**:渐变风是指风速随时间和空间逐渐变化的过程,常见于风向转变或地形影响下的风场变化。 在Matlab中,利用这四种风速类型进行组合预测,可以提高预测的准确性。预测模型可能包括以下几个步骤: 1. **数据收集与预处理**:收集历史风速数据,包括随机风、基本风、阵风和渐变风的数据,进行异常值检测、缺失值填充以及数据标准化。 2. **特征工程**:提取风速变化的相关特征,如平均值、标准差、极值、频率分布等,这些特征可能对预测有重要影响。 3. **模型选择**:可以选择多种预测模型,如时间序列分析(ARIMA、状态空间模型等)、机器学习算法(线性回归、决策树、支持向量机、神经网络等)或深度学习模型(LSTM、GRU等)。 4. **模型训练**:利用历史数据训练选定的模型,调整模型参数以优化性能,例如通过交叉验证来避免过拟合。 5. **模型验证与评估**:使用独立的测试集验证模型预测效果,常见的评估指标有均方误差(MSE)、平均绝对误差(MAE)和决定系数(R²)。 6. **组合预测**:结合四种风速的不同模型预测结果,可以采用加权平均、集成学习(如bagging、boosting)等方式,以提升整体预测精度。 7. **实时更新与动态调整**:实际应用中,模型需要不断接收新的风速数据并进行在线更新,以适应风场环境的变化。 通过以上步骤,可以构建一个综合考虑各种风速特性的预测系统,这对于风电场的功率输出预测、风电设备的维护计划以及电网调度都具有重要价值。然而,需要注意的是,每个风场的地理环境、气候条件和设备状况都有所不同,因此模型的建立应根据实际情况进行定制和优
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值