第8节 聪明的质监员
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e5+10;
ll n,m,s;
ll w[N],v[N];
ll cot[N],cct[N];
ll len;
ll l[200005],r[200005];
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*10+ch-'0';ch=getchar();}
return x*f;
}
ll ac(ll x)
{
ll sum=0;
cot[0]=0,cct[0]=0;
for(int i=1;i<=n;i++)//前缀和与差分
{
if(w[i]>=x)
{
cot[i]=cot[i-1]+1;
cct[i]=cct[i-1]+v[i];
}
else{
cot[i]=cot[i-1];
cct[i]=cct[i-1];
}
}
for(int i=1;i<=m;i++)
{
sum+=(cot[r[i]]-cot[l[i]-1])*(cct[r[i]]-cct[l[i]-1]);
}
return sum;
}
int main()
{
//freopen("test.txt","r",stdin);
//cin>>n>>m>>s;
n=read();m=read();s=read();
for(int i=1;i<=n;i++)
{
w[i]=read();v[i]=read();
//scanf("%lld%lld",&w[i],&v[i]);
}
for(int i=1;i<=m;i++)
{
//scanf("%lld%lld",&l[i],&r[i]);
l[i]=read();r[i]=read();
}
ll l=0,r=1e12;
while(l<r)
{
ll mid=l+((r-l)>>1);
if(ac(mid)>=s) l=mid+1;
else r=mid;
}
ll ans=min(abs(ac(l-1)-s),abs(ac(l)-s));//取差值小的
cout<<ans<<endl;
return 0;
}