求助

P1314 [NOIP2011 提高组] 聪明的质监员

01bit @ 2021-02-13 17:04:30

#include<iostream>
#include<cstring>
#define ll long long
#define abs(x) (x)>0?(x):-(x)
#define min(a,b) (a)<(b)?(a):(b)
#define max(a,b) (a)>(b)?(a):(b)
const ll inf=9e18;
ll n,m,s;
ll w[200001],v[200001];
ll l[200001],r[200001];
ll prew[200001],prev[200001];
ll Y=0;
void check(ll W){
    Y=0;
    std::memset(prew,0,sizeof(prew));
    std::memset(prev,0,sizeof(prev));
    for(ll i=1;i<=n;i++)
        if(w[i]>=W){prew[i]=prew[i-1]+1;prev[i]=prev[i-1]+v[i];}
        else{prew[i]=prew[i-1];prev[i]=prev[i-1];}
    for(ll i=1;i<=m;i++)
        Y+=(prew[r[i]]-prew[l[i]-1])*(prev[r[i]]-prev[l[i]-1]);
}
int main(){
    std::ios::sync_with_stdio(false);
    std::cin>>n>>m>>s;
    ll lW=0,rW=0,ans=inf;
    for(ll i=1;i<=n;i++){std::cin>>w[i]>>v[i];lW=min(lW,w[i]);rW=max(rW,w[i]);}
    for(ll i=1;i<=m;i++)std::cin>>l[i]>>r[i];
    lW=lW-1,rW=rW+2;
    while(lW<rW){
        ll W=(lW+rW)>>1;
        check(W);
        if(Y>s)lW=W+1;
        else rW=W-1;
        ans=min(ans,abs(Y-s));
    }
    std::cout<<ans;
    return 0;
}

|