$ Noip2011 / Luogu1314 $ clever quality supervision binary prefixes and clever +

$ Luogu $

 

$Sol$

First, it must be a $ W $ $ w_i $. Then it came out a way to violence, and enumerate $ W $ recalculation.

He noted that, to meet the SY $ $ $ of the smallest absolute value of the Y $ only be two, one is $ <S $ maximum the Y $ $ A is $> S $ minimum $ Y $. That They are seeking out pair of. values ​​were seeking when the $ W $ is to be dichotomous. but this does not $ a $ out this problem, because the $ check $ complexity is still $ O (NM) $ the look after explanations can be found $ $ prefix and Check acridine, I feel very clever $ qwq $. such complexity down $ Check () becomes $ $ O (N + M). $

 

$Code$

 

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<algorithm>
#define il inline
#define Rg register
#define go(i,a,b) for(Rg int i=a;i<=b;++i)
#define yes(i,a,b) for(Rg int i=a;i>=b;--i)
#define mem(a,b) memset(a,b,sizeof(a))
#define int long long
#define db double
#define inf 2147483647
using namespace std;
il int read()
{
    Rg int x=0,y=1;char c=getchar();
    while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();}
    while(c>='0'&&c<='9'){x=(x<<1)+(x<<3)+c-'0';c=getchar();}
    return x*y;
}
const int N=200010;
int n,m,S,as,minw=inf,maxw,sn[N],sv[N];
struct nd1{int w,v;}a[N];
struct nd2{int l,r;}b[N];
il int calc(int x)
{
    Rg int ret=0;
    mem(sn,0);mem(sv,0);
    go(i,1,n)
        if(a[i].w>=x)sn[i]=sn[i-1]+1,sv[i]=sv[i-1]+a[i].v;
        else sn[i]=sn[i-1],sv[i]=sv[i-1];
    go(i,1,m)
    {
        Rg int l=b[i].l,r=b[i].r;
        ret+=(sn[r]-sn[l-1])*(sv[r]-sv[l-1]);
    }
    return ret;
}
il int ef1()
{
    Rg int l=minw,r=maxw,mid,y,ret;
    while(l<=r)
    {
        mid=(l+r)>>1;
        y=calc(mid);
        if(y<=S)ret=y,r=mid-1;
        else l=mid+1;
    }
    return ret;
}
il int ef2()
{
    Rg int l=minw,r=maxw,mid,y,ret;
    while(l<=r)
    {
        mid=(l+r)>>1;
        y=calc(mid);
        if(y>=S)ret=y,l=mid+1;
        else r=mid-1;
    }
    return ret;
}
main()
{
    n=read(),m=read(),S=read();
    go(i,1,n)a[i]=(nd1){read(),read()},minw=min(minw,a[i].w),maxw=max(maxw,a[i].w);
    go(i,1,m)b[i]=(nd2){read(),read()};
    Rg int y1=ef1(),y2=ef2();
    as=min(abs(y1-S),abs(y2-S));
    printf("%lld\n",as);
    return 0;
}
View Code

 

 

 

Guess you like

Origin www.cnblogs.com/forward777/p/11418545.html