标签:des style blog http color os
1 const maxn=600000+10;inf=maxlongint; 2 type node=record 3 x,y,z:longint; 4 end; 5 var s,t:array[0..21,0..maxn] of longint; 6 a,b:array[0..maxn] of longint; 7 i,j,n,m,x,y,k,l,r,tmp:longint; 8 ans:int64; 9 c:array[0..2*maxn] of node; 10 w:node; 11 function max(x,y:longint):longint; 12 begin 13 if x>y then exit(x) else exit(y); 14 end; 15 16 procedure modify(x,y,z:longint); 17 var k,i:longint; 18 begin 19 c[0].x:=x;c[0].y:=y;c[0].z:=z; 20 i:=1;k:=2; 21 while k<=m do 22 begin 23 if (k<m) and (c[k+1].x>c[k].x) then inc(k); 24 if c[0].x<c[k].x then begin c[i]:=c[k];i:=k;k:=k<<1;end 25 else k:=m+1; 26 end; 27 c[i]:=c[0]; 28 end; 29 procedure insert(x,y,z:longint); 30 var k,i:longint; 31 begin 32 inc(m);c[0].x:=x;c[0].y:=y;c[0].z:=z; 33 i:=m;k:=i>>1; 34 while k>=1 do 35 begin 36 if c[0].x>c[k].x then begin c[i]:=c[k];i:=k;k:=i>>1;end 37 else k:=0; 38 end; 39 c[i]:=c[0]; 40 end; 41 procedure sort(l,r:longint); 42 var i,j,m,temp:longint; 43 begin 44 i:=l;j:=r;x:=a[(i+j)>>1]; 45 repeat 46 while b[a[i]]<b[x] do inc(i); 47 while b[a[j]]>b[x] do dec(j); 48 if i<=j then 49 begin 50 y:=a[i];a[i]:=a[j];a[j]:=y; 51 inc(i);dec(j); 52 end; 53 until i>j; 54 if i<r then sort(i,r); 55 if j>l then sort(l,j); 56 end; 57 procedure build(h,l,r:longint); 58 var i,p,mid:longint; 59 begin 60 mid:=(l+r)>>1;p:=0; 61 for i:=l to r do 62 if t[h,i]<=mid then 63 begin 64 t[h+1,l+p]:=t[h,i]; 65 inc(p); 66 s[h,i]:=p; 67 end 68 else 69 begin 70 t[h+1,mid+1+i-p-l]:=t[h,i]; 71 s[h,i]:=p; 72 end; 73 if l=r then exit; 74 build(h+1,l,mid); 75 build(h+1,mid+1,r); 76 end; 77 function find(h,l,r,x,y,k:longint):longint; 78 var ll,rr,mid:longint; 79 begin 80 if l=r then exit(t[h,l]); 81 mid:=(l+r)>>1; 82 if l=x then ll:=0 else ll:=s[h,x-1];rr:=s[h,y]-ll; 83 if rr>=k then exit(find(h+1,l,mid,l+ll,l+rr+ll-1,k)) 84 else exit(find(h+1,mid+1,r,mid+1+x-l-ll,mid+1+y-l-rr-ll,k-rr)); 85 end; 86 procedure init; 87 begin 88 readln(n,k,l,r);inc(n); 89 for i:=2 to n do begin readln(x);b[i]:=b[i-1]+x;end; 90 for i:=1 to n do a[i]:=i; 91 sort(1,n); 92 end; 93 procedure main; 94 begin 95 for i:=1 to n do t[0,a[i]]:=i; 96 build(0,1,n);//writeln(n); 97 for i:=l+1 to n do 98 begin 99 tmp:=b[a[find(0,1,n,max(i-r,1),i-l,1)]]; 100 // writeln(tmp); 101 insert(b[i]-tmp,i,1); 102 end; 103 ans:=0; 104 for i:=1 to k do 105 begin 106 w:=c[1]; // writeln(w.x); 107 inc(ans,w.x); 108 if (w.z>=r-l+1) or ((w.y<r+1) and (w.z>w.y-l-1)) then modify(-inf,inf,inf) 109 else 110 begin 111 tmp:=b[w.y]-b[a[find(0,1,n,max(w.y-r,1),w.y-l,w.z+1)]]; 112 modify(tmp,w.y,w.z+1); 113 end; 114 end; 115 writeln(ans); 116 end; 117 begin 118 assign(input,‘input.txt‘);assign(output,‘output.txt‘); 119 reset(input);rewrite(output); 120 init; 121 main; 122 close(input);close(output); 123 end.
标签:des style blog http color os
原文地址:http://www.cnblogs.com/zyfzyf/p/3854409.html