NOIP 2011 D2T2
题解:
二分W的值,每次O(n+m)计算检验结果(前缀和),如果S>Y则缩小W,否则增大W。
P.S.1A,贼稳!
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
const int MAXN=2e5+4;
typedef long long ll;
int n,m,L[MAXN],R[MAXN],w[MAXN],v[MAXN],l=0,r=0;
ll S,ans=1e18,sumw[MAXN]={0},sumv[MAXN]={0};
inline bool cck(int mid) {
ll res=0;
for (register int i=1;i<=n;++i) sumw[i]=sumw[i-1]+(w[i]>=mid),sumv[i]=sumv[i-1]+v[i]*(w[i]>=mid);
for (register int i=0;i<m;++i) res+=(sumw[R[i]]-sumw[L[i]-1])*(sumv[R[i]]-sumv[L[i]-1]);
ans=min(ans,abs(res-S));
return res>S;
}
int main() {
// freopen("P1314.in","r",stdin);
scanf("%d%d%lld",&n,&m,&S);
for (register int i=1;i<=n;++i) scanf("%d%d",&w[i],&v[i]),r=max(r,w[i]);
for (register int i=0;i<m;++i) scanf("%d%d",&L[i],&R[i]);
while (l<=r) {
int mid=l+r>>1;
if (cck(mid)) l=mid+1;
else r=mid-1;
}
printf("%lld\n",ans);
return 0;
}