WA at #13求助

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

NeNe_ @ 2023-12-20 20:42:22

代码如下

#include <bits/stdc++.h>
#define ll long long
#define for(i,k,j) for(ll i=k;i<=j;i++)
using namespace std;
const int N=2e5+5;
const ll INF=1e18;
ll w[N],v[N];
ll l[N],r[N];
ll s[N],s0[N];
ll n,m,st;
ll bg=INF,ed=-1;
ll ans=INF;
ll check(ll x)
{
    ll res=0;
    for(i,0,N) s[i]=s0[i]=0;
    for(i,1,n)
    {
        if(w[i]>=x) s[i]=s[i-1]+v[i],s0[i]=s0[i-1]+1;
        else s[i]=s[i-1],s0[i]=s0[i-1];
    }
    for(i,1,m)
    {
        res+=(s0[r[i]]-s0[l[i]-1])*(s[r[i]]-s[l[i]-1]);
    }
    return res;
}
int main()
{
    scanf("%lld%lld%lld",&n,&m,&st);
    for(i,1,n)
    {
        scanf("%lld%lld",w+i,v+i);
        bg=min(bg,w[i]);
        ed=max(ed,w[i]);
    }
    for(i,1,m) scanf("%lld%lld",l+i,r+i);
    while(bg<ed)
    {
        ll mid=(bg+ed)>>1;
        ll res=check(mid);
        if(res>=st) bg=mid+1;
        else ed=mid;
        ans=min(ans,abs(res-st));
    }
    printf("%lld\n",ans);
    return 0;
}

|