萌新刚学线段树,咋就T了x

P1440 求m区间内的最小值

Macaron_lin @ 2019-04-28 17:08:04


#include <cstdio>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 8000000 + 10;
int n, m;
int num[maxn];
struct Tr
{
    int l, r;
    int v;
}tr[maxn];

void Read()
{
    cin >> n >> m;
    for (int i = 1; i <= n; i++)
    {
        cin >> num[i];
    }
}

void Build(int L, int R, int i)
{
    tr[i].l = L;    tr[i].r = R;//区间范围

    if (L == R) 
    {
        tr[i].v = num[L];
    }
    else
    {
        int M = (L + R) / 2;
        Build(L, M, 2 * i);
        Build(M + 1, R, 2 * i + 1);
        tr[i].v = min(tr[2 * i].v, tr[2 * i + 1].v);
    }
}

int Fun(int qL, int qR, int i)
{
    int L = tr[i].l, R = tr[i].r;
    if (qL <= L && R <= qR) return tr[i].v;
    int ans = 1e8, M = (L + R) / 2;
    if (qL <= M) ans = min(ans, Fun(qL, qR, 2 * i));
    if (M + 1 <= qR) ans = min(ans, Fun(qL, qR, 2 * i + 1));
    return ans;
}

void Print()
{
    for (int i = 1; i <= n; i++)
    {
        if (i == 1) cout << 0 << '\n';
        else    cout << Fun(max(1, i - m), i - 1, 1) << '\n';
    }
}

int main()
{
    ios::sync_with_stdio(false);
    Read();
    Build(1, n, 1);
    Print();
    getchar(); getchar();
    return 0;
}

求助啊(委屈


by yummy @ 2019-04-28 17:18:44

正解不是单调队列吗

单调队列O(N),线段树O(NlogN)


by ecnerwaIa @ 2019-04-28 17:21:37

@嘿小恐龙 可以试着手写read,然后Fun函数改一下,可以卡卡常


by ecnerwaIa @ 2019-04-28 17:25:37

能用位运算肯定位运算啊


by MSwalker @ 2019-04-28 17:28:36

build里边没return啊


by wxwoo @ 2019-04-28 17:39:12

您最后的那两个getchar()不应该删掉吗......


by VenusM1nT @ 2019-04-28 17:47:56

理性分析一下,线段树每次查询 \Theta(n\log n),一共 n-m 次查询,也就是说复杂度是 \Theta((n-m)n\log n),你告诉我凭什么不 \texttt{T}。。。


by VenusM1nT @ 2019-04-28 17:52:10

啊说错了【我是智障】


by Smile_Cindy @ 2019-04-28 17:52:10

@Venus

确认线段树每次查询O(nlogn)???


by Smile_Cindy @ 2019-04-28 17:52:26

我可能学了假的线段树


by Juan_feng @ 2019-04-28 17:53:19

神仙Venus写错了吧/kk


| 下一页