#include<bits/stdc++.h>
#define rep(i,t) for(int i=1;i<=n;i++)
typedef long long ll;
using namespace std;
double eps=1e-8;
int a[200009];
int b[200009];
struct node{
int l,r;
ll sum;
int num;
}tree[40000009];
int root[400009];
int haha;
ll sum[200009];
int cnt;
void update(int l,int r,int &x,int y,int pos)
{
tree[++cnt]=tree[y];
tree[cnt].num++;
tree[cnt].sum+=1LL*b[pos];
x=cnt;
if(l==r)
return;
int mid=(l+r)>>1;
if(mid>=pos)
update(l,mid,tree[x].l,tree[y].l,pos);
else
update(mid+1,r,tree[x].r,tree[y].r,pos);
return;
}
double query(int l,int r,int x,int y,double fuck)
{
//cout<<tree[x].sum<<endl;
double ans=0.0;
if((double)b[l]-fuck>=eps)
{
haha+=tree[x].num-tree[y].num;
return (double)(tree[x].sum-tree[y].sum);
}
if((double)b[r]-fuck<eps)
return 0;
if(l==r)
{
haha+=tree[x].num-tree[y].num;
return (double)(tree[x].sum-tree[y].sum);
}
int mid=(l+r)>>1;
if((double)b[mid]-fuck>=eps)
{
ans+=query(l,mid,tree[x].l,tree[y].l,fuck);
}
if((double)b[r]-fuck>=eps)
{
ans+=query(mid+1,r,tree[x].r,tree[y].r,fuck);
}
return ans;
}
int main()
{
int n,q;
scanf("%d%d",&n,&q);
for(int i=1;i<=n;i++)
{
scanf("%d",&a[i]);
b[i]=a[i];
sum[i]=sum[i-1]+a[i];
}
sort(b+1,b+1+n);
int tot=unique(b+1,b+1+n)-b-1;
for(int i=1;i<=n;i++)
{
root[i]=root[i-1];
int pos=lower_bound(b+1,b+1+tot,a[i])-b;
update(1,tot,root[i],root[i-1],pos);
}
while(q--)
{
int l,r,x,y;
scanf("%d%d%d%d",&l,&r,&x,&y);
if(x>=y)
{
printf("0.0000000000\n");
continue;
}
double zuo=0.0;
double you=100000.0;
double need=(double)(sum[r]-sum[l-1])*(double)x/(double)y; //需要砍去的部分
while(you-zuo>=eps) //二分
{
haha=0;
double mid=(zuo+you)/2;
if(query(1,tot,root[r],root[l-1],mid)-(double)haha*mid-need>=eps)
{
zuo=mid;
}
else
you=mid;
}
printf("%.10lf\n",zuo);
}
return 0;
}
2019牛客多校第九场 H.Cutting Bamboos
最新推荐文章于 2019-08-27 21:22:06 发布