为什么爆零?

P1440 求m区间内的最小值

KK_lang @ 2022-11-23 17:02:14

写了个近乎模板的 ST 表。

两个 MLE,剩下的 WA。

大佬求调!!!

#include<bits/stdc++.h>
using namespace std;

int n, m;
int st[2000010][30];
int lg[2000010];

int query(int l, int r)
{
    int k = lg[r - l + 1] - 1;
    return min(st[l][k], st[r - (1 << k) + 1][k]);
}

int main()
{
    scanf("%d%d", &n, &m);
    for (int i = 1; i <= n; i++) scanf("%d", &st[i][0]);
    for (int j = 1; j < 30; j++)
        for (int i = 1; (1 << j) + i - 1 <= n; i++)
            st[i][j] = min(st[i][j - 1], st[i + (1 << (j - 1))][j - 1]);
    for (int i = 1; i <= n; i++)
        lg[i] = lg[i - 1] + ((1 << lg[i - 1]) == i);
    for (int i = 1; i <= m; i++)
    {
        int l = i - m, r = i - 1;
        if (l < 1 || r < 1) printf("0\n");
        else printf("%d\n", query(l, r));
    }
    return 0;
}

by KK_lang @ 2022-11-23 17:03:58

我蒻啊……黄题都切不了……


by _•́へ•́╬_ @ 2022-11-23 17:18:43

单调队列阿


by Usada_Pekora @ 2022-11-23 17:27:00

@KK_lang

    for (int i = 1; i <= n; i++)
    {
        int l = i - m, r = i - 1;
        l = max(l, 1), r = max(r, 1);
        if (i > 1)  printf("%d\n", query(l, r));
        else printf("0\n");
    }

只能做到 80 分,这是 ST 表空间的极限。


by KK_lang @ 2022-11-23 17:28:33

@•́へ•́╬ @Zyingyzzz

好吧,我写个单调队列……


|