新人求助,树状数组开$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 Drinkkk @ 2018-08-18 10:46:44

@Fraciton 试试不吸氧?


by Fraction @ 2018-08-18 10:47:20

@钟梓俊 不吸氧T5个点


by namespace_std @ 2018-08-18 10:51:07

@Fraciton 你INF太小了(数据的最大值>INF)(滑稽.jpg)

#define INF 0x7fffffff

by Fraction @ 2018-08-18 10:51:29

@namespace_std 真的啊?


by namespace_std @ 2018-08-18 10:52:42

Int_max=2^{31}-1=0x7fffffff


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

@namespace_std 想跪了,吧0xfffffff改成INT_MAX之后T3 orz


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

关键这道题貌似必须单调队列优化QwQ

我没试过树状数组


by namespace_std @ 2018-08-18 10:55:20

你再吸一次氧跑一次


by Fraction @ 2018-08-18 10:55:54

@namespace_std 树状数组区间最值的复杂度是(log_{2}n)^2,应该没超时吧


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

@namespace_std 吸氧T1,90分orz


| 下一页