wa0样例过了求调呜呜呜
查看原帖
wa0样例过了求调呜呜呜
516722
WSSBWSSWSW楼主2024/9/29 20:20
#include<bits/stdc++.h>
using namespace std;
const int N = 5e5+10;
int n,K,L,R,cnt;
long long a[N];
long long st[N][30];
int id[N][30];
int find(int L,int R){
	for(int k=20;~k;--k){
		if((1<<k)+L-1<=R){
			if(st[L][k]<st[R-(1<<k)+1][R])return id[R-(1<<k)+1][R];
			return id[L][k];
		}
	}
}
long long res(int l,int L,int R){
	for(int k=20;~k;--k){
		if((1<<k)+L-1<=R){
			return max(st[L][k],st[R-(1<<k)+1][R])-a[l-1];
		}
	}
}
struct node{
	int l,L,R;
	long long v;
	bool operator<(const node &b)const{
		return v<b.v;
	}
};
priority_queue<node> q;
long long ans;
void work(){
	while(cnt<K&&!q.empty()){
		++cnt;
		auto x=q.top();
		q.pop();
		ans+=x.v;
		int mid = find(x.L,x.R);
		if(x.R>mid){
			q.push({x.l,mid+1,x.R,res(x.l,mid+1,x.R)});
		if(x.L<mid){
			q.push({x.l,x.L,mid-1,res(x.l,x.L,mid-1)});
		}
	}
}
int main(){
	scanf("%d%d%d%d",&n,&K,&L,&R);
	for(int i = 1;i<=n;++i){
		scanf("%lld",&a[i]);
		a[i]+=a[i-1];
		st[i][0] = a[i];
		id[i][0]=i;
	}
	for(int k = 1;(1<<k)<=n;++k){
		for(int i = 1;i+(1<<k)-1<=n;++i){
			if(st[i][k-1]<st[i+(1<<(k-1))][k-1]){
				st[i][k]=st[i+(1<<(k-1))][k-1];
				id[i][k]=id[i+(1<<(k-1))][k-1];
			}else{
				st[i][k]=st[i][k-1];
				id[i][k]=id[i][k-1];
			}
		
		}
	}
	for(int i = 1;i<=n;++i){
		if(i+L-1>n)break;
		q.push({i,i+L-1,min(n,i+R-1),res(i,i+L-1,min(n,i+R-1))});
		//printf("%d %d %d\n",i,i+L-1,min(n,i+R-1),res(i,i+L-1,min(n,i+R-1)));
	}
	work();
	printf("%lld",ans);
	return 0;
}
2024/9/29 20:20
加载中...