题解:显然,二分W值,满足单调性
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <climits>
using namespace std;
const int M=200005;
#define ll long long
#define INF LLONG_MAX
ll sum[M],cnt[M];
int w[M],p[M],l[M],r[M];
int n,m;
ll L,R,mid,t,ans,s,mx;
ll ok(ll x)
{
ll tot=0;
for(int i=1;i<=n;i++)
{
sum[i]=sum[i-1];
cnt[i]=cnt[i-1];
if(x<=w[i])
{sum[i]+=p[i];cnt[i]++;}
}
for(int i=1;i<=m;i++)
tot+=(cnt[r[i]]-cnt[l[i]-1])*(sum[r[i]]-sum[l[i]-1]);
return tot;
}
void init()
{
cin>>n>>m>>s;
for(int i=1;i<=n;i++)
{
scanf("%d%d",&w[i],&p[i]);
if(mx<w[i])
mx=w[i];
}
for(int i=1;i<=m;i++)
scanf("%d%d",&l[i],&r[i]);
}
void work()
{
L=0;R=mx+1;ans=INF;
while(L<=R)
{
mid=(L+R)>>1;
t=ok(mid);
ans=min(ans,abs(s-t));
if(t<s) R=mid-1;
else L=mid+1;
}
printf("%lld\n",ans);
}
int main()
{
init();
work();
}