5pt求助

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

y15389097039 @ 2024-06-08 20:37:46

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,S,W;
const int MAXN = 2e6+5;
int sum[MAXN],cnt[MAXN],w[MAXN],v[MAXN],r[MAXN],l[MAXN];
int check(int x) {
    memset(sum,0,sizeof(sum));
    memset(cnt,0,sizeof(cnt));
    int s = 0;
    for (int i = 1; i <= n; ++i) {
        sum[i]=sum[i-1];
        cnt[i]=cnt[i-1];
        if(w[i]>=W) {
            sum[i]+=v[i];
            cnt[i]++;
        }
    }
    for(int i = 1; i <= m; ++i) {
        s=s+(cnt[r[i]]-cnt[l[i]-1])*(sum[r[i]]-sum[l[i]-1]);
    }
    return s;
}

signed main() {
    #ifndef ONLINE_JUDGE
//  freopen("da.in","r",stdin);
    #endif
    cin >> n >> m >> S;
    for(int i = 1; i <= n; ++i) {
        cin>>w[i]>>v[i];
        W = max(W,w[i]);
    }

    for(int i = 1; i <= m; ++i) {
        cin >> l[i] >> r[i];
    }
    int lt=1,rt=W;
    int ans = 1LL<<60 ;
    while(lt<=rt) {
        int mid=(lt+rt)/2;
        int t=check(mid);
        ans=min(ans,abs(t-S));
        if(t<S) {
            rt=mid-1;
        } else {
            lt=mid+1;
        }

    }
    cout << ans;
}

R161656821


|