85求助

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

qwq2519 @ 2020-09-20 15:28:49

#include<bits/stdc++.h>
#define rep(i,j,k) for(register int i(j);i!=(k+1);++i)
using namespace std;
typedef long long ll;
const int N=209007;

inline void read(ll &x) {
    x=0;
    register int ch=getchar();
    int w=0;
    while(ch<'0'||ch>'9') {
        w|=ch=='-';
        ch=getchar();
    }
    while(ch>='0'&&ch<='9') {
        x=x*10+ch-48;
        ch=getchar();
    }
    w?x=~(x-1):x;
}
ll n,m,ans,w[N],val[N],s;
struct inter {
    ll l,r;
} in[N];
ll zuiyou=2147455457575783647;

ll tot,num[N],sumval[N];

inline int you(int x) {
    int l=0,r=tot;
    while(l<r) {
        int mid=(l+r+1)>>1;
        if(num[mid]<=x) l=mid;
        else r=mid-1;
    }
    return l;
}
inline int zuo(int x) {
    int l=0,r=tot;
    while(l<r) {
        int mid=(l+r)>>1;
        if(num[mid]>=x) r=mid;
        else l=mid+1;
    }
    return l;
}

inline ll abs(ll &a) {
    a>0? 1:a=-a;
}

inline bool check(ll limit,ll &L,ll &R) {
    tot=0;
    ans=0;
    rep(i,1,n) {
        if(w[i]>=limit) {
            num[++tot]=i;
            sumval[tot]=sumval[tot-1]+val[i];
        }
    }
    rep(i,1,m) {
        int l=zuo(in[i].l);
        int r=you(in[i].r);
        ans+=(r-l+1)*(sumval[r]-sumval[l-1]);
    }
    if(s==ans) {
        cout<<0;
        exit(0);
    }
    ll t=s-ans;
    if(s>ans) {
        zuiyou=min(zuiyou,s-ans);
        return 0;
    } else {
        zuiyou=min(zuiyou,ans-s);
        return 1;
    }
}

int main() {
    //freopen("qc.in","r",stdin);
//  freopen("qc.out","w",stdout);
    read(n);
    read(m);
    read(s);
    rep(i,1,n) {
        read(w[i]);
        read(val[i]);
    }
    rep(i,1,m) {
        read(in[i].l);
        read(in[i].r);
    }
//  check(4);
    ll l=0,r=100100,mid;
    while(l<=r) {
        mid=(l+r)>>1;
        if(check(mid,l,r)) l=mid+1;
        else r=mid-1;
//      else l=mid+1;
    }
//  l=0 r=100100,mid;
//  while(l<r) {
//      mid=(l+r+1)>>1;
//      if(check(mid)) r=mid-1;
//      else l=mid;
//  }
    cout<<zuiyou;
    return 0;
}

|