JiuZhE66666 @ 2023-10-14 14:05:24
#include<bits/stdc++.h>
#define ll long long
using namespace std;
ll w[200005]={0},v[200005]={0};
ll l[200005]={0},r[200005]={0};
ll weight[200005]={0};
ll sums[200005]={0};
ll counts[200005]={0};
int main()
{
int n,m;
ll s;
scanf("%d%d%lld",&n,&m,&s);
for(int i=1;i<=n;i++) {scanf("%d%d",&w[i],&v[i]);weight[i]=w[i];}
for(int i=1;i<=m;i++) scanf("%d%d",&l[i],&r[i]);
sort(weight+1,weight+n+1);
ll maxs=0;
ll l1=0,r1=n+1;
while(l1<r1-1)
{
ll mid=weight[(l1+r1)/2];
memset(sums,0,sizeof(sums));
memset(counts,0,sizeof(counts));
for(int i=1;i<=n;i++)
{
if(w[i]>=mid)
{
counts[i]=counts[i-1]+1;
sums[i]=sums[i-1]+v[i];
}
else
{
counts[i]=counts[i-1];
sums[i]=sums[i-1];
}
}
ll sum=0;
// printf("l:%lld,r:%lld\n",l1,r1);
// printf("W的值为:%d\n",mid);
for(int i=1;i<=m;i++)
{
int div=(sums[r[i]]-sums[l[i]-1])*(counts[r[i]]-counts[l[i]-1]);
// printf("Y%d=%d ;",i,div);
sum+=div;
}
// printf("\n和为%lld\n",sum);
if(abs(sum-s)<abs(maxs-s)) maxs=sum;
if(sum>=s)l1=(l1+r1)/2;
else r1=(l1+r1)/2;
// printf("l:%lld,r:%lld\n",l1,r1);
}
printf("%lld\n",abs(maxs-s));
return 0;
}
by goldener @ 2024-03-01 20:56:54
div 开long long