BZOJ2006: [NOI2010] Super Piano - Problem Solving

https://www.lydsy.com/JudgeOnline/problem.php?id=2006

https://www.luogu.org/problemnew/show/P2048#sub

Xiao Z is a famous pianist. Recently, Dr. C gave Xiao Z a super piano. Xiao Z hopes to use this piano to create the most wonderful music in the world.

This super piano can play n notes, numbered 1 to n. The beauty of the i-th note is Ai, where Ai can be positive or negative.

A "super chord" consists of several consecutively numbered notes, containing no less than L and no more than R. We define the beauty of a super chord as the sum of the beauty of all the notes it contains. Two super chords are considered the same if and only if the sets of notes contained in the two super chords are the same.

Little Z decides to create a piece of music composed of k super chords. In order to make the piece more beautiful, Little Z requires the piece to be composed of k different super chords. We define the beauty of a piece of music as the sum of the beauty of all the super chords it contains. Xiao Z wants to know what the maximum beauty of the music he can create is.

O(n^2) is easy to think, and it will definitely be T.

But in fact we only need the first k of the interval to be large, so there is no need to calculate the interval sum of O(n^2).

Consider that for each left endpoint l, there is an r that maximizes the sum of the interval [l,r]. We store all these values ​​and take the maximum value of the sum of all intervals, and then pop it out.

Continuing to contribute to the pop-up element can also contribute its second largest, third largest ... respectively satisfied r, we add it in turn.

An intuitive idea is to maintain the chairman tree, and then turn over the problem solution and start licking the st table.

In fact, the r corresponding to the maximum value we found in [L, R], the second largest corresponding r must be the r corresponding to the maximum value of [L, r-1] and [r+1, R], using the st table It can be maintained once.

(However, my food... I didn't think about the content of the third paragraph TAT)

(Sure enough, people should brush more questions)

#include<cmath>
#include<queue>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=5e5+5;
const int B=19;
inline int read(){
    int X=0,w=0;char ch=0;
    while(!isdigit(ch)){w|=ch=='-';ch=getchar();}
    while(isdigit(ch))X=(X<<3)+(X<<1)+(ch^48),ch=getchar();
    return w?-X:X;
}
int n,k,L,R;
int s[N],lg[N],dp[N][B+3];
struct data{
    int l,r,w,L,R;
    bool operator <(data b)const{
        return w<b.w;
    }
};
priority_queue<data>q;
inline int qpow(int a){return 1<<a;}
void st(){
    for(int i=1;i<=n;i++){
        dp[i][0]=i;
        lg[i] =lg[i- 1 ];
        if (( 1 <<lg[i]+ 1 )==i)lg[i]++ ;
    }
    for(int j=1;j<=lg[n];j++){
        for(int i=1;i<=n;i++){
            if(i+qpow(j)-1>n)break;
            int l=dp[i][j-1],r=dp[i+qpow(j-1)][j-1];
            if(s[l]>s[r])dp[i][j]=l;
            else dp[i][j]=r;
        }
    }
}
inline int getmax(int l,int r){
    int len=r-l+1,h=lg[len];
    int l1=dp[l][h],r1=dp[r-qpow(h)+1][h];
    if(s[l1]>s[r1])return l1;
    else return r1;
}
int main(){
    n=read(),k=read(),L=read(),R=read();
    for(int i=1;i<=n;i++)s[i]=s[i-1]+read();
    st();
    for(int i=1;i+L-1<=n;i++){
        int j=getmax(i+L-1,min(i+R-1,n));
        q.push((data){i,j,s[j]-s[i-1],i+L-1,min(i+R-1,n)});
    }
    ll ans=0;
    while(k--){
        data tmp=q.top();q.pop();
        ans+=tmp.w;
        int i=tmp.l,j=tmp.r;
        if(tmp.L<=j-1){
            int t=getmax(tmp.L,j-1);
            q.push((data){i,t,s[t]-s[i-1],tmp.L,j-1});
        }
        if(j+1<=tmp.R){
            int t=getmax(j+1,tmp.R);
            q.push((data){i,t,s[t]-s[i-1],j+1,tmp.R});
        }
    }
    printf("%lld\n",ans);
    return 0;
}

+++++++++++++++++++++++++++++++++++++++++++

 + Author of this article: luyouqi233. +

 +Welcome to my blog: http://www.cnblogs.com/luyouqi233/ +

+++++++++++++++++++++++++++++++++++++++++++

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325051432&siteId=291194637