样例过了,但是零分,大佬求调

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

IridoYume @ 2023-11-16 17:12:34

RT

代码如下:

#include <bits/stdc++.h>
#define int long long
using namespace std;
int n,m,s;
int l[2000005],r[2000005];
int w[2000005];
int v[2000005];
int sv[2000005],sw[2000005];
int x,y=0,sum;
int W;
signed main(){
    cin>>n>>m>>s;
    for(int i=1;i<=n;i++){
        cin>>w[i]>>v[i];
        y=max(y,w[i]);
    }
    W=s;
    x=1;y++;
    for(int i=1;i<=m;i++){
        cin>>l[i]>>r[i];
    }
    while(x<y){
        memset(sw,0,sizeof(sw));
        memset(sv,0,sizeof(sv));
        int mid=(x+y)>>1;
        sum=0;
        for(int i=1;i<=n;i++){
            if(w[i]>=mid){
                sw[i]=sw[i-1]+1;
                sv[i]=sv[i-1]+v[i];
            }else{
                sw[i]-sw[i-1];
                sv[i]=sv[i-1];
            }
        }
        for(int i=1;i<=m;i++){
            sum+=(long long)(sw[r[i]]-sw[l[i]-1])*(sv[r[i]]-sv[l[i]-1]);
        } 
        if(sum==s){
            W=0;
            break;
        }
        W=min(W,abs(sum-s));
        if(sum<=s){
            y=mid;
        }else if(sum>s){
            x=mid+1;
        }
    }
    cout<<W<<endl;
    return 0;
}

by a31325365476587687 @ 2023-12-10 22:24:04

for(int i=1;i<=n;i++){
    if(w[i]>=mid){
        sw[i]=sw[i-1]+1;
        sv[i]=sv[i-1]+v[i];
    }else{
        sw[i]-sw[i-1];
        sv[i]=sv[i-1];
    }
}

这一段前缀和里的 sw[i]-sw[i-1]; 等号写成减号了


|