线段树最后一个点TLE求助
查看原帖
线段树最后一个点TLE求助
185910
PigAunt楼主2021/5/9 21:26

已证实不是输入输出速度的问题(std::ios::sync_with_stdio(false)scanf()都尝试了,均TLE,且时间相近)

同样代码-O2可以过

#include <iostream>

const int N = 1e6 + 5, PosInf = 1e9;

struct Info
{
    Info() { }
    Info(int maxValue, int minValue) : maxValue(maxValue), minValue(minValue) { }
    int maxValue, minValue;
}plainInfo;

struct Node
{
    int l, r;
    Info info;
}tree[N * 4];

int n, m, c;

void PushUp(int x)
{
    tree[x].info.maxValue = std::max(
        tree[x << 1].info.maxValue, tree[(x << 1) + 1].info.maxValue
    );
    tree[x].info.minValue = std::min(
        tree[x << 1].info.minValue, tree[(x << 1) + 1].info.minValue
    );
}

void Build(int x, int l, int r)
{
    tree[x].l = l;
    tree[x].r = r;
    if (l == r)
        return;
    int mid = (l + r) >> 1;
    Build(x << 1, l, mid);
    Build((x << 1) + 1, mid + 1, r);
    PushUp(x);
}

Info Query(int x, int l, int r)
{
    if (tree[x].l >= l && tree[x].r <= r)
        return Info(tree[x].info.maxValue, tree[x].info.minValue);
    
    int mid = (tree[x].l + tree[x].r) >> 1; 
    Info result = plainInfo, get;
    if (l <= mid)
    {
        get = Query(x << 1, l, r);
        result = Info(std::max(
            result.maxValue, get.maxValue), std::min(result.minValue, get.minValue));
    }
    if (r > mid)
    {
        get = Query((x << 1) + 1, l, r);
        result = Info(std::max(
            result.maxValue, get.maxValue), std::min(result.minValue, get.minValue));
    }
    
    return result;
}

void Modify(int x, int s, int maxValue, int minValue)
{
    if (tree[x].l == s && tree[x].r == s)
    {
        tree[x].info.maxValue = maxValue;
        tree[x].info.minValue = minValue;
        return;
    }

    int mid = (tree[x].l + tree[x].r) >> 1; 
    int result = 0;
    if (s <= mid)
        Modify(x << 1, s, maxValue, minValue);
    else
        Modify((x << 1) + 1, s, maxValue, minValue);

    PushUp(x);
    return;
}

int main()
{
    plainInfo.maxValue = -PosInf;
    plainInfo.minValue = PosInf;

    std::ios::sync_with_stdio(false);
    std::cin >> n >> m >> c;
    Build(1, 1, n);
    for (int i = 1; i <= n; i++)
    {
        int x;
        std::cin >> x;
        Modify(1, i, x, x);
    }
    
    bool flag = false;
    for (int i = 1; i <= n - m + 1; i++)
    {
        Info get = Query(1, i, i + m - 1);
        if (get.maxValue - get.minValue <= c)
        {
            std::cout << i << std::endl;
            flag = true;
        }
    }
    if (!flag)
        std::cout << "NONE" << std::endl;
    return 0;
}

谢谢各位神犇

2021/5/9 21:26
加载中...