bzoj2006 noi2010 Super Piano chairman Tree + Priority queue

Source: Internet
Author: User

Time limit:20 Sec Memory limit:552 MB
submit:2435 solved:1195

Description

Small Z is a small famous pianist, recently Dr. C gave a small z a super piano, small z hope to use this piano to create the world's most beautiful music. The super piano can play n notes, numbered 1 to N. The magic of the I note is AI, where AI can be negative. A "super chord" consists of a number of consecutive notes that contain no less than L and no more than R. We define the beauty of the Super chord for the sum of the wonderful degrees of all the notes it contains. Two super chords are thought to be the same, when and only if these two super chords contain a set of notes that are the same. Small Z decided to create a song composed of K super-chords, in order to make the music more beautiful, small z requires that the song is composed of K different super chords. We define the beauty of a piece of music as the sum of the wonderful degrees of all the super chords it contains. Little Z wants to know the maximum amount of music he can create.

Input

The first line consists of four positive integers n, K, L, R. where n is the number of notes, K is the number of super chords contained in the song, and L and R are the lower and upper limits of the number of notes contained in the Super chord. Next n rows, each line contains an integer AI, which represents the degree of beauty of each note by number from small to large.

Output

There is only one integer that represents the maximum value of the melody's beauty.

Sample Input4 3 2 3
3
2
-6
8

Sample Output11

"Sample description"
There are 5 different kinds of super chords:
Note 1 ~ 2, the degree of beauty is 3 + 2 = 5
Note 2 ~ 3, 2 + (-6) = 4
Note 3 ~ 4, the degree of beauty is (-6) + 8 = 2
Notes 1 ~ 3, 3 + 2 + (-6) = 1
Note 2 ~ 4, 2 + (-6) + 8 = 4
The best scenario is that the music consists of chord 1, chord 3, and chord 5, with a wonderful degree of 5 + 2 + 4 = 11. HINTn<=500,000


k<=500,000


-1000<=ai<=1000,1<=l<=r<=n and guaranteed that there must be a music that satisfies the conditionsIdeas:each sub-sequence weights and can be converted to two prefixes and the difference. We consider the subsequence at the end of each position, its weight and can be considered as the prefix at the end of the position and minus some prefix and before it.

So want the weight of this sub-sequence and as large as possible, then the previous prefix and as small as possible. If the number is not enough, the 2nd is small. Not enough, the 3rd is small.

So we maintain a priority queue, maintain a 3-tuple (v,r,k), V represents the weight of the sequence, and R represents the end position of the sequence, and K indicates that V is the K-value of the qualifying sequence at the end of R and

The maximum subsequence weights at the end of each position and, each time the top of the heap is taken out, is then placed in the heap with the same end position as the k+1 large weights and.

So k times can be solved.

This requires that we can quickly find the interval K small, the use of a durable line tree can be.

Time complexity O (KLOGN).

Code:

                                              //File Name:bzoj2006.cpp//Author:long//Mail: [email protected]//Created time:2016 July 31 Sunday 14:12 42 seconds#include<stdio.h>#include<string.h>#include<iostream>#include<algorithm>#include<queue>#defineLL Long Long#defineHash _hash_using namespacestd;Const intMAXN =500000+3;intH_num[maxn],hash[maxn],tot,top;voidHash_init () {sort (h_num+1, h_num+top+1); Tot=0; hash[++tot] = h_num[1];  for(intI=2; i<=top;i++){        if(H_num[i]! = h_num[i-1]) hash[++tot] =H_num[i]; }}intHash_find (intx) {    intL =1, r =Tot,mid;  while(L <=R) {Mid= (L + r) >>1; if(Hash[mid] < x) L = mid +1; ElseR = Mid-1; }    returnl;}structt{intv,r,k; BOOL operator< (ConstT&a)Const{        returnV <A.V; }};p Riority_queue<T>que;structnode{intls,rs,w; Node () {LS= rs = W =0;}} NODE[MAXN* -];intA[maxn],sum[maxn],root[maxn],sz,n,k,l,r;voidUpdateint&i,intLintRintW) {node[++SZ] =Node[i]; I=sz; NODE[I].W++; if(L = = r)return ; intMid = (L + r) >>1; if(W <=mid) Update (NODE[I].LS,L,MID,W); ElseUpdate (node[i].rs,mid+1, r,w);}intQueryintIintJintLintRintk) {    if(L = = r)returnl; intt = NODE[NODE[J].LS].W-NODE[NODE[I].LS].W; intMid = (L + r) >>1; if(T >= k)returnquery (NODE[I].LS,NODE[J].LS,L,MID,K); Else returnQuery (node[i].rs,node[j].rs,mid+1, r,k-t);}void Get(intIint&l,int&R) {L= I-r, R = i-L; if(L <0) L =0; if(R <0) R =-1;} LL solve () {sum[0] =0; Top=0; h_num[++top] =0;  for(intI=1; i<=n;i++) {Sum[i]= sum[i-1] +A[i]; h_num[++top] =Sum[i];    } hash_init ();  for(intI=0; i<=n;i++) Sum[i]=Hash_find (Sum[i]); SZ=0; root[1] = root[0] =0; Update (root[1],1, tot,sum[0]);  for(intI=1; i<=n;i++) {Root[i+1] =Root[i]; Update (Root[i+1],1, Tot,sum[i]); }     while(!que.empty ()) Que.pop (); LL ans=0; intNow,l,r;  for(inti=l;i<=n;i++){        Get(I,L,R); if(R <0)Continue; //printf ("i =%d L =%d r =%d\n", i,l,r);now = Hash[sum[i]]-Hash[query (root[l],root[r+1],1Tot1)]; //printf ("q =%d now =%d\n", query (root[l],root[r+1],1,tot,1), now);Que.push ((T) {now,i,1});     } T u;  while(true) {u=Que.top ();        Que.pop (); //printf ("U.V =%d\n", U.V);Ans + =U.V; K--; if(! K Break; Get(U.R,L,R); if(R-l +1<= U.K)Continue; now= HASH[SUM[U.R]]-Hash[query (root[l],root[r+1],1, tot,u.k+1)]; Que.push ((T) {NOW,U.R,U.K+1}); }    returnans;}intMain () { while(~SCANF ("%d %d%d%d",&n,&k,&l,&R)) {         for(intI=1; i<=n;i++) scanf ("%d",&A[i]); //cout << Solve () << Endl;printf"%lld\n", Solve ()); }    return 0;}

bzoj2006 noi2010 Super Piano chairman Tree + Priority queue

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.