Bzoj 2006: [NOI2010] Super Piano heap, ST table

Source: Internet
Author: User

2006: [NOI2010] Super piano time limit:20 Sec Memory limit:552 MB
submit:2222 solved:1082
[Submit] [Status] [Discuss] 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. HINT

n<=500,000
k<=500,000
-1000<=ai<=1000,1<=l<=r<=n and guaranteed that there must be a music that satisfies the conditions

Source problem: Heap +st Table finally put this question over ... I am still very weak ... Alas... Qaq We can enumerate the beginning I and then the interval range of the end to get [i+l-1,i+r-1]. Then we can put the opening at I, end in [I+l-1,i+r-1] Maximum and(prefix and maintenance and, ST Table maintenance maximum) and start position, end interval, maximum and positionAdded to the heap. Then, because you want to remove the first K-large chord, each time you remove maximum and top elements of a heapAccumulation. However, when we take out the maximum value starting with I, it is possible to start with I second largest or third largestIt's bigger than the top of the heap, so we're going to divide the original interval into two intervals when we take out the top of the heap and add it to the heap.
1#include <bits/stdc++.h>2 using namespacestd;3 #defineLL Long Long4 #defineMAXN 5000105 structnode6 {7     intp1,p2;8 }mx;9 intn,a[maxn],sum[maxn],mx[maxn][ +],mxi[maxn][ +],heap1[2*maxn+Ten],heap2[2*maxn+Ten],heap3[2*maxn+Ten],heap4[2*maxn+Ten],heap5[2*maxn+Ten],size;Ten intRead () One { A     ints=0, fh=1;CharCh=GetChar (); -      while(ch<'0'|| Ch>'9'){if(ch=='-') fh=-1; ch=GetChar ();} -      while(ch>='0'&&ch<='9') {s=s*Ten+ (ch-'0'); ch=GetChar ();} the     returns*fh; - } - voidST () - { +     inti,j; -      for(i=1; i<=n;i++) {mx[i][0]=sum[i];mxi[i][0]=i;} +      for(j=1;(1&LT;&LT;J) <=n;j++) A     { at          for(i=1; i+ (1&LT;&LT;J)-1<=n;i++) -         { -             if(mx[i][j-1]>=mx[i+ (1<< (J-1))][j-1]) mx[i][j]=mx[i][j-1],mxi[i][j]=mxi[i][j-1]; -             Elsemx[i][j]=mx[i+ (1<< (J-1))][j-1],mxi[i][j]=mxi[i+ (1<< (J-1))][j-1]; -         } -     } in } -Node Get (intLintR) to { +     intJ; - node C1; the      for(j=0;(1&LT;&LT;J) <= (r-l+1, j + +); j--; *     if(Mx[l][j]>=mx[r-(1&LT;&LT;J) +1][j]) {c1.p1=mx[l][j];c1.p2=mxi[l][j];} $     Else{c1.p1=mx[r-(1&LT;&LT;J) +1][j];c1.p2=mxi[r-(1&LT;&LT;J) +1][j];}Panax Notoginseng     returnC1; - } the voidSwap (intRootintNow ) + { A swap (Heap1[root],heap1[now]); the swap (Heap2[root],heap2[now]); + swap (Heap3[root],heap3[now]); - swap (Heap4[root],heap4[now]); $ swap (Heap5[root],heap5[now]); $ } - voidPUSH1 (intK1,intK2,intK3,intK4,intK5) - { the     intNow,root; -HEAP1[++SIZE]=K1; HEAP2[SIZE]=K2; HEAP3[SIZE]=K3; HEAP4[SIZE]=K4; heap5[size]=k5;now=SIZE;Wuyi      while(now>1) the     { -root=now/2; Wu         if(Heap4[root]>=heap4[now])return; - Swap (root,now); Aboutnow=Root; $     } - } - voidPOP1 (intk) - { A     intNow,root; +Heap1[k]=heap1[size]; Heap2[k]=heap2[size]; Heap3[k]=heap3[size]; Heap4[k]=heap4[size]; heap5[k]=heap5[size--];root=K; the      while(root<=size/2) -     { $now=root*2; the         if(now<size&&heap4[now+1]>heap4[now]) now++; the         if(Heap4[root]>=heap4[now])return; the Swap (root,now); theroot=Now ; -     } in } the intMain () the { About     intK,l,r,i,l,r,k1,k2,k3,k4,k5; the node CC1,CC2; theLL ans=0; theN=read (); K=read (); L=read (); r=read (); +      for(i=1; i<=n;i++) {a[i]=read (); sum[i]=sum[i-1]+a[i];} - ST (); the      for(i=1; i<=n-l+1; i++)Bayi     { thel=i+l-1; R=min (i+r-1, n); theNode cc=Get (l,r); -PUSH1 (i,l,r,cc.p1-sum[i-1],cc.p2); -     } the      while(k--) the     { thek1=heap1[1];k2=heap2[1];k3=heap3[1];k4=heap4[1];k5=heap5[1]; POP1 (1); theans+=K4; -         if(K2<= (k5-1)) Cc1=get (k2,k5-1); the         if((k5+1) <=k3) Cc2=get (k5+1, K3); the         if(K2<= (k5-1)) Push1 (k1,k2,k5-1, cc1.p1-sum[k1-1],cc1.p2); the         if((k5+1) <=k3) Push1 (k1,k5+1, k3,cc2.p1-sum[k1-1],cc2.p2);94     } theprintf"%lld", ans); the fclose (stdin); the fclose (stdout);98     return 0; About}

Bzoj 2006: [NOI2010] Super Piano heap, ST table

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.