link :
Method: Chairman Tree + heap
parsing:
we think about it that way.
for each point that can be the right endpoint.
We're going to take the first K-sum[i]−sum[j−1] sum[i]-sum[j-1], first dealing with all the smallest of the vertices that can be the right endpoint.
and then we take the biggest lump.
and then for the right endpoint of the lump that was taken.
The smallest-sum[j in front of it has been unable to take.
so to take the 2nd small-sum[j],
then throw the value into the heap.
then keep on doing so, from the heap to take K can be.
This involves an interval to take the K small operation.
so the last President tree is good.
What is the complexity of this?
O (KLOGN), can not be too empty.
Code:
#include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define N 500010 #d
Efine M 20000000 #define INF 0x3f3f3f3f using namespace std;
typedef long Long LL;
int n,k,l,r,size,heap_root;
int ch[n][2];
int limit[n];
int fa[n];
int root[n];
int lson[m];
int rson[m];
int siz[m];
int sum[n];
int rnk[n];
int val[n];
int h[n];
int a[n];
int find (int x) {while (fa[x]) x=fa[x];
return x; int merge (int x,int y) {if (!x) | | (!y))
return x+y;
if (Val[x]<val[y]) swap (x,y);
Ch[x][1]=merge (Ch[x][1],y);
Fa[ch[x][1]]=x;
if (H[ch[x][1]]>h[ch[x][0]]) swap (ch[x][1],ch[x][0]);
h[x]=h[ch[x][1]]+1;
return x;
} void Insert (int l,int r,int x,int &y,int val) {y=++size;
siz[y]=siz[x]+1;
if (l==r) return;
LSON[Y]=LSON[X],RSON[Y]=RSON[X];
int mid= (L+R) >>1;
if (val<=mid) insert (l,mid,lson[x],lson[y],val);
else insert (mid+1,r,rson[x],rson[y],val);
int query (int l,int r,int x,int y,int kth) { if (l==r) return l;
int mid= (L+R) >>1;
int num=siz[lson[y]]-siz[lson[x]];
if (num<kth) return query (Mid+1,r,rson[x],rson[y],kth-num);
else return query (L,MID,LSON[X],LSON[Y],KTH);
int main () {h[0]=-1;
scanf ("%d%d%d%d", &n,&k,&l,&r);
Insert (-inf,inf,0,root[1],0);
for (int i=2;i<=n+1;i++) {int x;
scanf ("%d", &x);
Sum[i]=sum[i-1]+x;
Insert (-inf,inf,root[i-1],root[i],sum[i]);
Rnk[i]=1;
} Val[l+1]=sum[l+1]; for (int i=l+2;i<=n+1;i++) {if (i<=r+1) {val[i]=sum[i]-query (-inf,inf,root[1-1],root
[I-l],rnk[i]);
limit[i]=i-l+1;
else {int t=query (-inf,inf,root[i-r-1],root[i-l],rnk[i]);
Val[i]=sum[i]-query (-inf,inf,root[i-r-1],root[i-l],rnk[i]);
limit[i]=r-l+1;
} rnk[i]++;
Heap_root=merge (Find (i-1), i);
fa[heap_root]=0; ll Ans=0;
for (int i=1;i<=k;i++) {Ans+=val[heap_root];
int flag=0; if (Rnk[heap_root]<=limit[heap_root]) {if (heap_root<=r) {Val[heap_r
Oot]=sum[heap_root]-query (-inf,inf,root[1-1],root[heap_root-l],rnk[heap_root]); else {val[heap_root]=sum[heap_root]-query (-inf,inf,root[heap_root-r-1],root[he
Ap_root-l],rnk[heap_root]);
} flag=1;
} rnk[heap_root]++;
int pre=heap_root;
Heap_root=merge (ch[heap_root][0],ch[heap_root][1]);
fa[heap_root]=0;
fa[pre]=0,ch[pre][0]=ch[pre][1]=0;
if (flag) {heap_root=merge (heap_root,pre); fa[heap_root]=0;}
printf ("%lld\n", ans); }