洛谷上AC但是别的OJ被卡了一个点

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

Asika391 @ 2019-10-25 22:04:48

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=200005;

int n,m,l=1,r;
ll S,ans=((ll)1<<61);
ll sum[N],Fre[N];

struct point1{
    int w,v;
}mine[N];

struct point2{
    int L,R;
}Sec[N];

int read(){
    int x=0;
    char ch=getchar();
    while(ch<'0' || ch>'9') ch=getchar();
    while(ch>='0' && ch<='9'){x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return x;
}

int main(){
    scanf("%d%d%lld",&n,&m,&S);
    for(int i=1;i<=n;++i){
        mine[i].w=read(),mine[i].v=read();
        r=max(mine[i].w,r);
    }
    for(int i=1;i<=m;++i) Sec[i].L=read(),Sec[i].R=read();
    while(l<=r){
        memset(sum,0,sizeof(sum)),memset(Fre,0,sizeof(Fre));
        ll ret=0;
        int W=(l+r)>>1;
        for(int i=1;i<=n;++i){
            if(mine[i].w>=W) sum[i]=sum[i-1]+mine[i].v,Fre[i]=Fre[i-1]+1;
            else sum[i]=sum[i-1],Fre[i]=Fre[i-1];   
        }
        for(int i=1;i<=m;++i)
            ret+=(sum[Sec[i].R]-sum[Sec[i].L-1])*(Fre[Sec[i].R]-Fre[Sec[i].L-1]);
        ll t=S-ret;
        if(t>0){
            ans=min(ans,t);
            r=W-1;
        }else if(t<0){
            ans=min(ans,-t);
            l=W+1;
        }else if(t==0){
            ans=0;
            break;
        }
    }
    printf("%lld",ans);
    return 0;
}

by songhongyi @ 2020-02-22 21:55:05

@Asika391 您应该去别的OJ里问


|