90分求助

P1886 滑动窗口 /【模板】单调队列

黄舀啊 @ 2022-05-02 19:20:39

#include<iostream>
using namespace std;
const int MAXN = 1e6+10;

long long n,m,c[MAXN];

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

void build(int p,int l,int r) {
    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 ask(int p,int l,int r) {
    if(tree[p].l>=l && tree[p].r<=r) {
        return tree[p].maxx;
    }
    int mid = (tree[p].l+tree[p].r)/2;
    long long ans = -(1<<30);
    if(l<=mid) ans = max(ans,ask(p<<1,l,r));
    if(r>mid)  ans = max(ans,ask(p<<1|1,l,r));
    return ans;
}
long long ask2(int p,int l,int r) {
    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() {

    cin >> n >> m;
    for(int i=1 ; i<=n ; i++)
        cin >> c[i];
    build(1,1,n);
    for(int i=1 ; i<=n-m+1 ; i++)
        cout << ask2(1,i,i+m-1) << ' ';
    cout << endl;
    for(int i=1 ; i<=n-m+1 ; i++)
        cout << ask(1,i,i+m-1) << ' ';
    return 0;
}

by bruhify @ 2022-05-02 20:18:28

用快读或者吸氧


by Abeeel51 @ 2022-05-15 15:36:38

吸氧快读printf


by Abeeel51 @ 2022-05-15 15:36:44

@黄舀啊


|