标签:主席树 int a+b isp insert play img res open
首先\(\%\%\%\%olinr\)以及花_Q\(julao\)当场切题
然后就是怎么求
\[max(|a-A|,|b-B|)=max(a-A,A-a,B-b,b-B)\]
我们令\(x_1=(a+b)/2,x_2=(A+B)/2,y_1=(a-b)/2,y_2=(A-B)/2\)
\[max(x_1+y_1-x_2-y_2,x_2+y_2-x_1-y_1,x_1-y_1-x_2+y_2,x_2-y_2-x_1+y_1)\]
\[=max(x_1-x_2,x_2-x_1)+max(y_1-y_2,y_2-y_1)\]
\[=|x_1-x_2|+|y_1-y_2|\]
那么就是用主席树维护\(\{A_i+B_i\}\)以及\(\{A_i-B_i\}\)的中位数
/*-------------OI使我快乐-------------*/
int n,m,tot;
int cdy[M],wzy[M],lc[M],rc[M];
int root[M][2],siz[M*25][2],lson[M*25][2],rson[M*25][2];
ll sum[M*25][2];
IL void insert(int A,int &B,int le,int ri,int pos,int knd)
{
// printf("insert %d %d %d\n",le,ri,pos);
B=++tot;
siz[B][knd]=siz[A][knd]+1;
sum[B][knd]=sum[A][knd]+pos;
if(le==ri) return;
int mid=(le+(ll)ri)>>1;
if(pos<=mid) insert(lson[A][knd],lson[B][knd],le,mid,pos,knd),rson[B][knd]=rson[A][knd];
else insert(rson[A][knd],rson[B][knd],mid+1,ri,pos,knd),lson[B][knd]=lson[A][knd];
}
IL int qury_kth(int A,int B,int le,int ri,int k,int knd)
{
if(le==ri) return le;
int mid=(le+(ll)ri)>>1,tmp=siz[lson[B][knd]][knd]-siz[lson[A][knd]][knd];
if(k<=tmp) return qury_kth(lson[A][knd],lson[B][knd],le,mid,k,knd);
else return qury_kth(rson[A][knd],rson[B][knd],mid+1,ri,k-tmp,knd);
}
IL ll qury_sum(int A,int B,int lenow,int rinow,int le,int ri,int knd)
{
if(le<=lenow&&rinow<=ri) return sum[B][knd]-sum[A][knd];
int mid=(lenow+(ll)rinow)>>1;ll res=0;
if(le<=mid) res+=qury_sum(lson[A][knd],lson[B][knd],lenow,mid,le,ri,knd);
if(mid<ri) res+=qury_sum(rson[A][knd],rson[B][knd],mid+1,rinow,le,ri,knd);
return res;
}
IL int qury_cnt(int A,int B,int lenow,int rinow,int le,int ri,int knd)
{
if(le<=lenow&&rinow<=ri) return siz[B][knd]-siz[A][knd];
int mid=(lenow+(ll)rinow)>>1,res=0;
if(le<=mid) res+=qury_cnt(lson[A][knd],lson[B][knd],lenow,mid,le,ri,knd);
if(mid<ri) res+=qury_cnt(rson[A][knd],rson[B][knd],mid+1,rinow,le,ri,knd);
return res;
}
int main()
{
freopen("spy.in","r",stdin);
freopen("spy.out","w",stdout);
read(n);read(m);
for(R int i=1;i<=n;++i) read(cdy[i]);
for(R int i=1;i<=n;++i) read(wzy[i]);
for(R int i=1;i<=n;++i) lc[i]=cdy[i]+wzy[i],rc[i]=cdy[i]-wzy[i];
for(R int i=1;i<=n;++i) insert(root[i-1][0],root[i][0],-maxn,maxn,lc[i],0);
tot=0;
for(R int i=1;i<=n;++i) insert(root[i-1][1],root[i][1],-maxn,maxn,rc[i],1);
while(m--)
{
int le,ri,midx,midy;ll lx,rx,ly,ry;
read(le);read(ri);
midx=qury_kth(root[le-1][0],root[ri][0],-maxn,maxn,(ri-le+2)/2,0);
// puts("now now now");
midy=qury_kth(root[le-1][1],root[ri][1],-maxn,maxn,(ri-le+2)/2,1);
// puts("now now now");
// printf("%d %d\n",midx,midy);
lx=1ll*qury_cnt(root[le-1][0],root[ri][0],-maxn,maxn,-maxn,midx,0)*midx-qury_sum(root[le-1][0],root[ri][0],-maxn,maxn,-maxn,midx,0);
rx=qury_sum(root[le-1][0],root[ri][0],-maxn,maxn,midx,maxn,0)-1ll*qury_cnt(root[le-1][0],root[ri][0],-maxn,maxn,midx,maxn,0)*midx;
// printf("nmow %lld %lld\n",lx,rx);
ly=1ll*qury_cnt(root[le-1][1],root[ri][1],-maxn,maxn,-maxn,midy,1)*midy-qury_sum(root[le-1][1],root[ri][1],-maxn,maxn,-maxn,midy,1);
ry=qury_sum(root[le-1][1],root[ri][1],-maxn,maxn,midy,maxn,1)-1ll*qury_cnt(root[le-1][1],root[ri][1],-maxn,maxn,midy,maxn,1)*midy;
printf("%.2f\n",(lx+rx+ly+ry)/2.0);
}
fclose(stdin);
fclose(stdout);
return 0;
}
标签:主席树 int a+b isp insert play img res open
原文地址:https://www.cnblogs.com/LovToLZX/p/10623675.html