TLE求助

P1440 求m区间内的最小值

黄舀啊 @ 2022-05-06 18:36:00

#include<iostream>
#define ll long long
using namespace std;
const int MAXN = 2*1e6+10;

long long n,m,c[MAXN];

struct Tree {
    int l,r;
    long long maxx,minn;
};
Tree tree[MAXN<<2];

inline ll read()
{
    ll x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9'){if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-48;ch=getchar();}
    return x*f;
}
void build(int p,int l,int r) {
    if(l>r) return ;
    tree[p].l = l,tree[p].r = r;
    if(r == l) {
        tree[p].maxx = c[l];
        tree[p].minn = c[l];
        return ;
    }
    int mid = (l+r)/2;
    build(p<<1,l,mid);
    build(p<<1|1,mid+1,r);
    tree[p].maxx = max(tree[p<<1].maxx,tree[p<<1|1].maxx);
    tree[p].minn = min(tree[p<<1].minn,tree[p<<1|1].minn);
}
long long ask2(int p,int l,int r) {
    if(l>r) return 0;
    if(tree[p].l>=l && tree[p].r<=r) {
        return tree[p].minn;
    }
    int mid = (tree[p].l+tree[p].r)/2;
    long long ans = (1<<30);
    if(l<=mid) ans = min(ans,ask2(p<<1,l,r));
    if(r>mid)  ans = min(ans,ask2(p<<1|1,l,r));
    return ans;
}

int main() {

    n = read(),m = read();
    for(int i=1 ; i<=n ; i++)
        c[i] = read();
    build(1,1,n);
    for(int i=1 ; i<=n ; i++){
        if(i == 1){
            cout << 0 << endl;
            continue;
        }
        if(i-m<=0) cout << ask2(1,1,i-1) << endl;
        else      cout << ask2(1,i-m,i-1) << endl;
    }

    return 0;
}

by Sunny郭 @ 2022-05-27 21:10:12

cout改成printf就行


by Theudas @ 2022-08-15 12:15:32

有没有一种可能:endl也很慢((


by 黄舀啊 @ 2022-10-13 19:11:07

@61_T 迟来的感谢,现在才过


|