新人求助,树状数组开$O2$WA2TLE1

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

Fraction @ 2018-08-18 10:45:07

求救,代码如下:

#include <bits/stdc++.h>
#define fp(i, l, r) for(register int i = (l); i <= (r); ++i)
#define fd(i, l, r) for(register int i = (l); i >= (r); --i)
#define ANTISYNC ios::sync_with_stdio(false)
#define full(a, b) memset(a, b, sizeof(a))
#define MAXN (int)3e6 + 5
#define lowbit(x) x & (-x)
#define ll long long
#define il inline
using namespace std;

const int INF = 0xfffffff;

int n, k;
int maxn[MAXN], minn[MAXN], num[MAXN];

namespace Fenwick {
    il int add(int x, int y) {
        while(x <= n) {
            maxn[x] = num[x];
            minn[x] = num[x];
            for(register int i = 1; i < lowbit(x); i <<= 1) {
                maxn[x] = max(maxn[x], maxn[x-i]);
                minn[x] = min(minn[x], minn[x-i]);
            }
            x += lowbit(x);
        }
        return 0;
    }

    il int getmax(int l, int r) {
        int ans = -INF;
        while(r >= l) {
            ans = max(ans, num[r]);
            --r;
            for(; r - lowbit(r) >= l; r -= lowbit(r)) {
                ans = max(ans, maxn[r]);
            }
        }
        return ans;
    }

    il int getmin(int l, int r) {
        int ans = INF;
        while(r >= l) {
            ans = min(ans, num[r]);
            --r;
            for(; r - lowbit(r) >= l; r -= lowbit(r)) {
                ans = min(ans, minn[r]);
            }
        }
        return ans;
    }
};

il int init() {
    scanf("%d%d", &n, &k);
    fp(i, 1, n) {
        scanf("%d", &num[i]);
        Fenwick::add(i, num[i]);
    }
    fp(i, 1, n-k+1) {
        printf("%d%c", Fenwick::getmin(i, i+k-1), i == n-k+1 ? '\n' : ' ');
    }
    fp(i, 1, n-k+1) {
        printf("%d%c", Fenwick::getmax(i, i+k-1), i == n-k+1 ? '\n' : ' ');
    }
    return 0;
}

int main() {
    init();
    return 0;
}

by namespace_std @ 2018-08-18 10:58:23

@Fraciton

n=1e6$,$log_2^2n=(20)^2=400

总复杂度O(n*log_2^2n=4e8),T到飞起QaQ


by Arashi丶 @ 2018-08-18 10:58:49

我这个线段树都跑的飞快啊...


by namespace_std @ 2018-08-18 10:59:25

@Arashi丶 线段树O(nlog_2n)肯定能过


by Fraction @ 2018-08-18 10:59:37

@Arashi丶 树状数组不能log_{2}n维护最值


by Fraction @ 2018-08-18 10:59:57

我去加一堆优化看看


by namespace_std @ 2018-08-18 11:00:04

@Arashi丶 树状数组差分之后是O(nlog_2^2n)


by Arashi丶 @ 2018-08-18 11:33:29

哦对啊 为啥要树状数组做

肯定t啊 多带一个Log的复杂度担不起来


上一页 |