[bzoj2006][NOI2010]超级钢琴

来源:互联网 发布:php https post请求 编辑:程序博客网 时间:2024/05/27 09:46

http://www.lydsy.com/JudgeOnline/problem.php?id=2006
题目大意
选择k个不同的区间使它们的和最大

首先,
我们可以把所有区间都丢到堆里,每次取出最大值
但这显然是要爆炸的。。。
然后,
可以轻易发现一个明显的性质…(很明显吗…)(Orz w_yqts)
有若干三元组(x,l,r)
l-x+1<=L,r-x+1<=r
t(x,l,r)表示左端点为x,右端点在l~r的区间中最大区间的右端点
于是我们处理完(x,l,r)之后可以继续做(x,l,t-1)和(x,t+1,r)
这样子处理显然是比较优的…
所以一开始将所有(x,x+l-1,x+r-1)丢到堆里
每次取出堆顶,然后再分裂丢回去就可以了
st表比较优秀…
代码如下…

#include <bits/stdc++.h>using namespace std;#define N 500005struct node{    int x,l,r,t;};priority_queue <node> heap;int a[N],st[N][19];int n,K,L,R;bool operator > (node x,node y){    return a[x.t]-a[x.x-1]>a[y.t]-a[y.x-1];}bool operator < (node x,node y){    return a[x.t]-a[x.x-1]<a[y.t]-a[y.x-1];}int calc(int l,int r){    int len=log(1.0+r-l)/log(2.0);    return a[st[l][len]]>a[st[r-(1<<len)+1][len]]?st[l][len]:st[r-(1<<len)+1][len];}int main(){    cin>>n>>K>>L>>R;    for (int i=1;i<=n;++i) scanf("%d",&a[i]),a[i]+=a[i-1],st[i][0]=i;    for (int i=1;i<=18;++i)    for (int j=1;j+(1<<i)-1<=n;++j)    {    st[j][i]=a[st[j][i-1]]>a[st[j+(1<<(i-1))][i-1]]?st[j][i-1]:st[j+(1<<(i-1))][i-1];    }    for (int i=1;i+L-1<=n;++i)    heap.push({i,i+L-1,min(n,i+R-1),calc(i+L-1,min(n,i+R-1))});    long long ans=0LL;    while (K--)    {        node t=heap.top();heap.pop();        ans+=a[t.t]-a[t.x-1];        if (t.t>t.l) heap.push({t.x,t.l,t.t-1,calc(t.l,t.t-1)});        if (t.t<t.r) heap.push({t.x,t.t+1,t.r,calc(t.t+1,t.r)});    }    cout<<ans<<endl;}