[THUPC2019] inequality

topic

The \ (| a_ix + b_i | \ ) does not look very beautiful, we consider some of it becomes beautiful;

Put a \ (a_i \) it is \ (| a_i the X-+ || \ FRAC b_i {} {a_i} | \) , we \ (\ frac {b_i} { a_i} \) as \ (- (- \ {} {a_i B_i FRAC}) \) , so that the original persimmon \ (| a_i || X - (- \ FRAC B_i} {} {a_i) | \) .

Ah, two coordinates are not the absolute value is subtracted from it; transformed into the problem so we have a number of axes, \ ((- \ FRAC B_i} {} {a_i, 0) \) at There \ (| a_i | \) points, we need to find a \ (the X-\) , so that \ ((x, 0) \ ) to the minimum distance and all points.

This is a classic problem, \ (the X-\) be the coordinates of the median, then the minimum distance and adjust the law license;

So we use the coordinates discrete segment tree to maintain the number of axes, find the median line segments directly to the trees half.

Code

#include<bits/stdc++.h>
#define re register
#define db double
#define LL long long
const int maxn=5e5+5;
inline int read() {
    char c=getchar();int x=0,r=1;while(c<'0'||c>'9'){if(c=='-')r=-1;c=getchar();}
    while(c>='0'&&c<='9')x=(x<<3)+(x<<1)+c-48,c=getchar();return x*r;
}
int a[maxn],b[maxn],id[maxn],pos[maxn],p[maxn],n;
int l[maxn*3],r[maxn*3];LL sa[maxn*3],sb[maxn*3],sum;
inline int cmp(int A,int B) {return 1ll*b[A]*a[B]>1ll*b[B]*a[A];}
void build(int x,int y,int i){
    l[i]=x,r[i]=y;if(x==y){p[x]=i;return;}
    int mid=x+y>>1;build(x,mid,i<<1),build(mid+1,y,i<<1|1);
}
inline void chg(int x,int va,int vb) {x=p[x];while(x)sa[x]+=va,sb[x]+=vb,x>>=1;}
int fid(int i,LL nw) {
    if(l[i]==r[i])return l[i];
    return sa[i<<1]>=nw?fid(i<<1,nw):fid(i<<1|1,nw-sa[i<<1]);
}
LL qry(int x,int y,int i,int o) {
    if(x<=l[i]&&y>=r[i]) return !o?sa[i]:sb[i];
    int mid=l[i]+r[i]>>1;LL tot=0;
    tot=(x<=mid?qry(x,y,i<<1,o):0)+(y>mid?qry(x,y,i<<1|1,o):0);
    return tot;
} 
int main() {
    n=read();for(re int i=1;i<=n;i++)a[i]=read();for(re int i=1;i<=n;i++)b[i]=read();
    for(re int i=1;i<=n;i++)if(a[i]<0)a[i]=-a[i],b[i]=-b[i];
    for(re int i=1;i<=n;i++)id[i]=i;std::sort(id+1,id+n+1,cmp);
    for(re int i=1;i<=n;i++)pos[id[i]]=i;build(1,n,1);
    for(re int t,i=1;i<=n;i++) {
        chg(pos[i],a[i],-b[i]);t=fid(1,((sum+=a[i])+1)>>1);
        db pnw=(db)(b[id[t]])/(db)(-a[id[t]]),ans=0;
        ans=(db)qry(1,t,1,0)*pnw-qry(1,t,1,1)+qry(t,n,1,1)-(db)qry(t,n,1,0)*pnw;
        printf("%.10lf\n",ans);
    }
    return 0;
}

Guess you like

Origin www.cnblogs.com/asuldb/p/12190189.html