萌新刚学线段树,咋就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 Juan_feng @ 2019-04-28 17:58:03

st表最优解第一页/kk


by VenusM1nT @ 2019-04-28 18:28:38

@Alpha 窝说了窝智障了啊qaq(

脑子出了一些问题


by Macaron_lin @ 2019-04-29 23:40:23

@wxwoo 不用删掉的,这个没有关系。


by NaCly_Fish @ 2019-07-08 20:49:16

@Venus orz Venus


上一页 |