AcWing 499. 聪明的质监员
原题链接
困难
#include<cstdio>
#include<cmath>
#define ll long long
using namespace std;
const int N=1001000;
template<class T>
void read(T &x)
{
x=0;
T f;
f=1;
char c;
c=getchar();
while((c<'0'||c>'9')&&c!='-')
{
c=getchar();
}
if(c=='-')
{
f=-1;
c=getchar();
}
while(c>='0'&&c<='9')
{
x=(x<<3)+(x<<1)+(c^48);
c=getchar();
}
x=x*f;
}
int w[N],v[N],L[N],R[N],n,m;
ll s,sum[N],num[N];
ll check(ll W)
{
int i;
ll ans;
ans=0;
for(i=1;i<=n;i++)
{
if(w[i]<W)
{
num[i]=num[i-1];
sum[i]=sum[i-1];
}
else
{
sum[i]=sum[i-1]+v[i];
num[i]=num[i-1]+1;
}
}
for(i=1;i<=m;i++)
{
ans+=(sum[R[i]]-sum[L[i]-1])*(num[R[i]]-num[L[i]-1]);
}
return ans;
}
inline ll labs(ll a)
{
return a>0 ? a : -a;
}
int main()
{
int i;
ll l,r,mid,ans;
read(n);
read(m);
read(s);
for(i=1;i<=n;i++)
{
read(w[i]);
read(v[i]);
}
for(i=1;i<=m;i++)
{
read(L[i]);
read(R[i]);
}
l=0;
r=1e6;
while(l<=r)
{
mid=(l+r)>>1;
if(check(mid)>s)
{
l=mid+1;
}
else
{
r=mid-1;
}
}
ans=labs(check(l)-s);
if(ans>labs(check(r)-s))
{
ans=labs(check(r)-s);
}
printf("%lld",ans);
}