CF276C

题目简化和分析:

属于一种贪心思维,我们想如果要使得和最大,那么就必须保证最大的数乘的次数越多越好,并且排序没有限制,快速累加每个位置出现的次数,所以应该使用线段树差分

然后排序最大乘最大累加。

Solution:

#include<bits/stdc++.h>

using namespace std;

typedef long long ll;
typedef double db;

const int N=2e5+50;
const int M=1e5+50;
const int Mod=1e9+7;

inline ll read(){
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9'){
        if(ch=='-')
            f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9'){
        x=(x<<1)+(x<<3)+(ch^48);
        ch=getchar();
    }
    return x*f;
}

ll n,q;
ll a[N],c[N];

int main()
{
	n=read(),q=read();
	for(int i=1;i<=n;++i) a[i]=read();
	sort(a+1,a+n+1);
	for(int i=1;i<=q;++i){
		ll l,r;
		l=read(),r=read();
		c[l]++,c[r+1]--;
	}
	for(int i=1;i<=n;++i){
		c[i]=c[i-1]+c[i];
	}
	sort(c+1,c+n+1);
	ll ans=0;
	for(int i=1;i<=n;++i) ans+=c[i]*a[i];
	printf("%lld\n",ans);
	return 0;
}

posted @ 2022-07-29 10:46  RVG  阅读(29)  评论(0)    收藏  举报