$|x_1-x_2|+|y_1-y_2|=\max(|(x_1+y_1)-(x_2+y_2)|,|(x_1-y_1)-(x_2-y_2)|)$

将坐标$(x,y)$逆变换为$(\frac{x+y}{2},\frac{x-y}{2})$后,询问[l,r]的最优解为中位数

离散化后用主席树支持查询

#include<cstdio>
#include<algorithm>
typedef long long ll;
const int N=100010,M=1800000;
int n,q,i,j,x,y,k,X[N],Y[N],lix[N],liy[N];
struct ChairTree{
int tot,l[M],r[M],v[M],root[N];ll sum[M];
int ins(int x,int a,int b,int c,int d){
int y=++tot;v[y]=v[x]+1,sum[y]=sum[x]+d;
if(a==b)return y;
int mid=(a+b)>>1;
if(c<=mid)l[y]=ins(l[x],a,mid,c,d),r[y]=r[x];else l[y]=l[x],r[y]=ins(r[x],mid+1,b,c,d);
return y;
}
inline ll ask(int x,int y,int k){
int a=1,b=n,mid,t,cnt=0;ll ans=0;
while(a<b){
mid=(a+b)>>1,t=v[l[x]]-v[l[y]];
if(k<=t){
cnt+=v[r[x]]-v[r[y]],ans+=sum[r[x]]-sum[r[y]];
x=l[x],y=l[y],b=mid;
}else{
cnt-=t,ans-=sum[l[x]]-sum[l[y]];
k-=t,x=r[x],y=r[y],a=mid+1;
}
}
return ans-sum[x]/v[x]*cnt;
}
}Tx,Ty;
inline int lowerx(int x){
int l=1,r=n,t,mid;
while(l<=r)if(lix[mid=(l+r)>>1]<=x)l=(t=mid)+1;else r=mid-1;
return t;
}
inline int lowery(int x){
int l=1,r=n,t,mid;
while(l<=r)if(liy[mid=(l+r)>>1]<=x)l=(t=mid)+1;else r=mid-1;
return t;
}
int main(){
scanf("%d%d",&n,&q);
for(i=1;i<=n;i++)scanf("%d",&X[i]);
for(i=1;i<=n;i++)scanf("%d",&j),Y[i]=X[i]-j,X[i]+=j;
for(i=1;i<=n;i++)lix[i]=X[i],liy[i]=Y[i];
std::sort(lix+1,lix+n+1),std::sort(liy+1,liy+n+1);
for(i=1;i<=n;i++)X[i]=lowerx(X[i]),Y[i]=lowery(Y[i]);
for(i=1;i<=n;i++)Tx.root[i]=Tx.ins(Tx.root[i-1],1,n,X[i],lix[X[i]]),Ty.root[i]=Ty.ins(Ty.root[i-1],1,n,Y[i],liy[Y[i]]);
while(q--){
scanf("%d%d",&x,&y),k=(y-x+2)/2;
printf("%.2f\n",(double)(Tx.ask(Tx.root[y],Tx.root[x-1],k)+Ty.ask(Ty.root[y],Ty.root[x-1],k))/2.0);
}
return 0;
}

  

BZOJ2735 : 世博会的更多相关文章

  1. BZOJ 2735: 世博会 主席树+切比雪夫距离转曼哈顿距离

    2735: 世博会 Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 124  Solved: 51[Submit][Status][Discuss] D ...

  2. NOIP2009分数线划定【B004】

    [B004]分数线划定[难度B]—————————————————————————————————————————————————————————————————————————— [题目要求] 世博 ...

  3. FZU 1894 志愿者选拔(单调队列)

    传送门 Description 世博会马上就要开幕了,福州大学组织了一次志愿者选拔活动.参加志愿者选拔的同学们排队接受面试官们的面试.参加面试的同学们按照先来先面试并且先结束的原则接受面试官们的考查. ...

  4. ACM 房间安排

    房间安排 时间限制:3000 ms  |  内存限制:65535 KB 难度:2   描述 2010年上海世界博览会(Expo2010),是第41届世界博览会.于2010年5月1日至10月31日期间, ...

  5. FZU1894 单调队列

    S - 1019 Time Limit:1500MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Submit Sta ...

  6. 房间安排-nyoj168

    描述 2010年上海世界博览会(Expo2010),是第41届世界博览会.于2010年5月1日至10月31日期间,在中国上海市举行.本次世博会也是由中国举办的首届世界博览会.上海世博会以“城市,让生活 ...

  7. WPF数据绑定Binding(二)

    WPF数据绑定Binding(二) 1.UI控件直接的数据绑定 UI对象间的绑定,也是最基本的形式,通常是将源对象Source的某个属性值绑定 (拷贝) 到目标对象Destination的某个属性上. ...

  8. NOIP200902分数线划定

    NOIP200902分数线划定 描述 世博会志愿者的选拔工作正在 A 市如火如荼的进行.为了选拔最合适的人才,A 市对所有报名的选手进行了笔试,笔试分数达到面试分数线的选手方可进入面试.面试分数线根据 ...

  9. nyoj------170网络的可靠性

    网络的可靠性 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 A公司是全球依靠的互联网解决方案提供商,也是2010年世博会的高级赞助商.它将提供先进的网络协作技术,展 ...

随机推荐

  1. LVS负载均衡集群服务搭建详解(一)

    LVS概述 1.LVS:Linux Virtual Server 四层交换(路由):根据请求报文的目标IP和目标PORT将其转发至后端主机集群中的某台服务器(根据调度算法): 不能够实现应用层的负载均 ...

  2. 03-VTK基础概念(2)

    3.3 光照 剧场里有各式各样的灯光,三维渲染场景中也一样,可以有多个光照存在.光照和相机是三维渲染场景必备的因素,如果没有指定(像3.1.1_RenderCylinder例子,我们没有给Render ...

  3. 最长回文子串O(n)算法

    原文链接:英文版链接 首先,我们将字符串S中插入符号“#”转化成另一个字符串T. 比如:S = "abaaba",T = “#a#b#a#a#b#a#”. 为了找到最长回文字串,我 ...

  4. 发个题目坑 二模03day1

    1.数列(seq2.pas/c/cpp) 题目描述 一个数列定义如下:f(1) = 1,f(2) = 1,f(n) = (A * f(n - 1) + B * f(n - 2)) mod 7.给定 A ...

  5. macbook air电池保养方法

    转自: http://bbs.feng.com/read-htm-tid-5819895.html http://www.zhihu.com/question/22628030

  6. 【Hibernate】Hibernate系列7之二级缓存

    二级缓存 7.1.概述 7.2.配置方法

  7. Java for LeetCode 191 Number of 1 Bits

    Write a function that takes an unsigned integer and returns the number of ’1' bits it has (also know ...

  8. Java for LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  9. Java for LeetCode 168 Excel Sheet Column Title

    Given a positive integer, return its corresponding column title as appear in an Excel sheet. For exa ...

  10. NEFU 2016省赛演练一 I题 (模拟题)

    这题没名字 Problem:I Time Limit:2000ms Memory Limit:65535K Description Now give you an interger m and a s ...