蒟蒻求助!!!线段树WA*2

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

Mystery_Sky @ 2019-01-09 20:40:49

#include <iostream>
#include <cstdio>
using namespace std;
//Benjamin
//
#define ll long long
#define maxn 10000010
long long a[maxn], sum1[maxn<<2];
long long sum2[maxn<<2];

inline int lc(int k) {return k<<1;}
inline int rc(int k) {return k<<1|1;}

inline void PushUp_max(int k)
{
    sum1[k] = max(sum1[lc(k)], sum1[rc(k)]);
}

void build_max(int l, int r, int k)
{
    if(l == r) {
        sum1[k] = a[l];
        return;
    }
    int m = (l+r)>>1;
    build_max(l, m, lc(k));
    build_max(m+1, r, rc(k));
    PushUp_max(k);
}

ll query_max(int L, int R, int l, int r, int k)
{
    if(L <= l && r <= R) {
        return sum1[k];
    }
    int m = (l+r)>>1;
    ll Ans = -maxn;
    if(L <= m) Ans = max(query_max(L, R, l, m, lc(k)), Ans);
    if(R > m) Ans = max(query_max(L, R, m+1, r, rc(k)), Ans);
    return Ans;
}

inline void PushUp_min(int k)
{
    sum2[k] = min(sum2[lc(k)], sum2[rc(k)]);
}

void build_min(int l, int r, int k)
{
    if(l == r) {
        sum2[k] = a[l];
        return;
    }
    int m = (l+r)>>1;
    build_min(l, m, lc(k));
    build_min(m+1, r, rc(k));
    PushUp_min(k);
}

ll query_min(int L, int R, int l, int r, int k)
{
    if(L <= l && r <= R) {
        return sum2[k];
    }
    int m = (l+r)>>1;
    ll Ans = maxn;
    if(L <= m) Ans = min(query_min(L, R, l, m, lc(k)), Ans);
    if(R > m) Ans = min(query_min(L, R, m+1, r, rc(k)), Ans);
    return Ans;
}

int main() {
    int n, k;
    scanf("%d%d", &n, &k);
    for(int i = 1; i <= n; i++) scanf("%lld", &a[i]);
    build_max(1, n, 1);
    build_min(1, n, 1);
    int m;
    m = n - k + 1;
    for(int i = 1; i <= m; i++) {
        printf("%lld ", query_min(i, i+k-1, 1, n, 1));
    }
    printf("\n");
    for(int i = 1; i <= m; i++) {
        printf("%lld ", query_max(i, i+k-1, 1, n, 1));
    }
    return 0;
}

by 上官书房 @ 2019-01-09 21:01:13

呵呵

呵呵

呵呵

呵呵

呵呵
呵呵

呵呵


by Mystery_Sky @ 2019-01-09 21:04:36


by wkywkywky @ 2019-01-31 23:24:45

把maxn调大


|