[NOI2010] 超级钢琴

1 篇文章 0 订阅
1 篇文章 0 订阅

luogu2048:https://www.luogu.org/problemnew/show/P2048

这道题大概就是求一个序列前 k 大长度在 [ L, R ] 内的连续区间的和。
序列的长度为 5*10^5,枚举每个序列肯定是不现实的。
我们可以想到,可以将多个具有相同性质的区间合并,并维护这一个区间集合的最大的连续区间的值。可以用一个堆来维护,当取到这个值时,将该集合分裂,再加入堆中。
我为了求出多个区间中的最大区间,我们用线段树维护一个前缀和,以及区间最大前缀和,即每个代表 [ i, i ] 的节点维护一个 maxv 值,代表 a[1] 至 a[i] 的和 ,pushup 时,取左右子节点 maxv 值的 max。这样,我们就可以方便的求出 [ 1, 1…n ] 的最大值。相似的,我们也可以求出 [ i, i..n ] 的最大值,只需要查找线段树上代表 [ i, n ] 的一个或者多个节点,取 maxv 的 max 并减去 a[i-1] 即可。
利用前缀和线段树,我们可以快速的求出左边界相同的一系列区间的最大值。这样我们便可以将左边界相同的区间合并,用一个三元组 ( i, l, r ) 来表示,i 表示左边界 l, r表示该区间的右边界的区间。即一个 ( i, l, r ) 代表了 [ i, l ],[ i, l+1 ],[ i, l+2 ] … [ i, r ] 的区间。
当我们取到一个三元组时,将答案加上该三元组的最大区间的大小,并检测他能不能被拆成更小的三元组。为了实现这个操作,我们需要在线段树上再维护一个 where 标记,表示当前的 maxv 的右边界是什么。这样,我们就可以将 ( i, l, r ) 分裂成 ( i, l, wher-1 ),( i, where+1, r ),如果分裂后的三元组合法的话,求出新三元组的 where 和 maxv,并推入堆中。

如果在做题的时候出现了一些在程序中加入一些对答案没有影响的语句,却改变了最终答案的情况,比如说加了一个输出中间量的 printf ,程序的最终输出结果就变了。这种情况可能是一些变量没有设置初始值。

代码:

#include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 6000000;
const int INF = 1<<30;

inline int read()
{
    char c = getchar(); int f = 1, x = 0;
    while(c<'0' || c>'9') { if(c == '-') f = -1;  c = getchar(); }
    while(c>='0'&&c<='9') { x = x * 10 + c - '0'; c = getchar(); }
    return f * x;
}

int n, k, L, R, a[MAXN];
LL ans;

struct HeapNode
{
    int maxv, where, l, llimit, rlimit;
    bool operator < (const HeapNode& rhs) const{
        return maxv < rhs.maxv; 
    }
} ;
priority_queue <HeapNode> q;

struct Segment_Tree
{
    int maxv[MAXN*4], where[MAXN*4];
    #define lson (o<<1)
    #define rson (o<<1|1)
    Segment_Tree()
    {
        memset(where, 0, sizeof(where));
        for(int i=0; i<MAXN*4; i++)
            maxv[i] = -INF;
    }
    inline void pushup(int o)
    {
        maxv[o] = max(maxv[lson], maxv[rson]);
        where[o] = (maxv[lson] > maxv[rson]) ? where[lson] : where[rson]; 
    }
    inline void build(int o, int l, int r)
    {
        if(l == r) { maxv[o] = a[l]; where[o] = l; return ; }

        int mid = (l+r) >>1;
        build(lson, l, mid);
        build(rson,mid+1,r);
        pushup(o);
    }
    inline int querypos(int o, int l, int r, int ql, int qr)
    {
        if(ql<=l && r<=qr) return o;

        int mid = (l+r) >>1;
        int ans1=0, ans2=0;
        if(ql <= mid) ans1 = querypos(lson, l, mid, ql, qr);
        if(mid+1<=qr) ans2 = querypos(rson,mid+1,r, ql, qr)
        return (maxv[ans1] > maxv[ans2]) ? ans1 : ans2;
    }
} st;

int main()
{
    n = read(); k = read(); L = read(); R = read();
    for(int i=1; i<=n; i++) a[i] = read(), a[i] += a[i-1];
    st.build(1, 1, n);

    for(int i=1; i<=n-L+1; i++)
    {
        int tmpl = i+L-1, tmpr = min(i+R-1, n);
        int o = st.querypos(1, 1, n, tmpl, tmpr);
        q.push((HeapNode){st.maxv[o]-a[i-1], st.where[o], i, tmpl, tmpr});
        //printf("where:%d, i:%d, l:%d, r:%d\n", st.where[o], i, tmpl, tmpr);
    }

    while(k -- > 0)
    {
        HeapNode tmp = q.top(); q.pop();
        ans += (long long)tmp.maxv;
        //printf("%lld\n", ans);
        //printf("maxv:%d, where:%d, l:%d, llimit:%d, rlimit:%d\n", tmp.maxv, tmp.where, tmp.l, tmp.llimit, tmp.rlimit);

        if(tmp.where-1 >= tmp.llimit)
        {
            int o = st.querypos(1, 1, n, tmp.llimit, tmp.where-1);
            q.push((HeapNode){st.maxv[o]-a[tmp.l-1], st.where[o], tmp.l, tmp.llimit, tmp.where-1});
        }
        if(tmp.where+1 <= tmp.rlimit)
        {
            int o = st.querypos(1, 1, n, Qtmp.where+1, tmp.rlimit);
            q.push((HeapNode){st.maxv[o]-a[tmp.l-1], st.where[o], tmp.l, tmp.where+1, tmp.rlimit});
        }
    }

    printf("%lld\n", ans);

    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值