纯RMQ,三个MLE,求调!!!!orz

P1440 求m区间内的最小值

481986534Lin @ 2022-11-16 22:15:52

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
const ll qwq=25,C=55,T=505,M=5005,N=1e6+7,NN=1e7+7,L=2139062143,oo=1e18;
inline ll read(){
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        ch=='-'?f=-1:1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}
ll n,m,dp[N*2][25],li[N*2],tx[2*N];
int main(){
    n=read(),m=read();
    tx[0]=-1;
    for(ll a=1;a<=n;a++) {
        li[a]=read();
        dp[a][0]=li[a];
        tx[a]=tx[a>>1]+1;
    }
//  tx[0]=1;
    for(int a=1;(1<<a)<=m;a++){
        for(int b=1;b+(1<<a)-1<=n;b++){
            dp[b][a]=min(dp[b][a-1],dp[b+(1<<(a-1))][a-1]);
        }
    }
    for(int a=1;a<=n;a++){
        if(a==1) printf("0\n");
        else if(a==2) printf("%lld\n",li[1]);
        else {
            int l=a-m,r=a-1;
            if(l<=0) l=1;
            int x=tx[r-l+1];        
            printf("%lld\n",min(dp[l][x],dp[r-(1<<x)+1][x]));
        }
    }
    return 0;
}
/*
6 2
7 8 1 4 3 2
*/

by qwq___qaq @ 2022-11-16 22:46:58

@481986534Lin 你算算空间,这道题RMQ过不了


|