bzoj 2006 分类: bzoj 2015-05-31 17:18 29人阅读 评论(0) 收藏



http://blog.csdn.net/orpinex/article/details/7012836 太神了~!



#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<cmath>
#include<cstring>
#include<map>
#include<string>
#include<stack>
#include<queue>
#include<utility>
#include<iostream>
#include<algorithm>

const int MAXN = 500005, logN = 20;
struct HeapNode
{
    int st,ed,l,r; long long v; HeapNode(int st=0,int ed=0,int l=0,int r=0,long long v=0):st(st),ed(ed),l(l),r(r),v(v){}
};
bool operator < (const HeapNode &a,const HeapNode &b){return a.v < b.v;}

std::priority_queue<HeapNode> heap;

int n, k, L, R, seg[MAXN];
long long sum[MAXN];
long long st[MAXN][logN];
int opt[MAXN][logN];
long long ans = 0;

void PreWork()
{
    for(int i = 1; i <= n; i++)
        sum[i] = sum[i-1] + seg[i], st[i][0] = sum[i], opt[i][0] = i;

    for(int i = 1; i < logN; i++)
    {
        int lmt = n - (1<<i) + 1;
        for(int j = 1; j <= lmt; j++)
        {
            int t = j + (1<<(i-1));

            if(st[j][i-1] > st[t][i-1])
                st[j][i] = st[j][i-1], opt[j][i] = opt[j][i-1];
            else
                st[j][i] = st[t][i-1], opt[j][i] = opt[t][i-1];
        }
    }
}
int Ask(int a,int b)
{
    int len = b - a + 1, t = 0; while((1<<(t+1)) <= len) t++;
    return (st[a][t] > st[b-(1<<t)+1][t])?opt[a][t]:opt[b-(1<<t)+1][t];
}
void Insert(int st,int ll,int rr)
{
    int t = Ask(ll,rr); heap.push(HeapNode(st,t,ll,rr,sum[t]-sum[st-1]));
}
void Build()
{
    for(int i = 1; i + L - 1<= n; i++)
        Insert(i,i+L-1,std::min(i+R-1,n));
}
void Solve()
{
    for(int tp = 1; tp <= k; tp++)
    {
        HeapNode max = heap.top();
        heap.pop();

        ans += max.v;
        if(max.l < max.ed) Insert(max.st,max.l,max.ed-1);
        if(max.ed < max.r) Insert(max.st,max.ed+1,max.r);
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("bzoj2006.in","r",stdin);
    freopen("bzoj2006.out","w",stdout);
#endif

    scanf("%d%d%d%d",&n,&k,&L,&R);
    for(int i = 1; i <= n; i++) scanf("%d",&seg[i]);

    PreWork();

    Build();

    Solve();

    printf("%lld",ans);

#ifndef ONLINE_JUDGE
    fclose(stdin);
    fclose(stdout);
#endif
    return 0;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。

posted @ 2015-05-31 17:18  <Dash>  阅读(140)  评论(0编辑  收藏  举报