[NOI2010] 超级钢琴

luogu2048:https://www.luogu.org/problemnew/show/P2048c++

这道题大概就是求一个序列前 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,并推入堆中。web

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

代码:ui

#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;
}