Cutting Bamboos

链接:https://ac.nowcoder.com/acm/contest/889/H
来源:牛客网
 

时间限制:C/C++ 5秒,其他语言10秒
空间限制:C/C++ 262144K,其他语言524288K
Special Judge, 64bit IO Format: %lld

题目描述

There are n bamboos arranged in a line. The i-th bamboo from the left has height hih_{i}hi​.

You are given q queries of the type (l, r, x, y). For each query (l, r, x, y) we consider only the l-th to r-th bamboo inclusive. We want to make y horizontal cuts through these bamboos, such that after each cut, the total length of bamboo cut is the same, and after all y cuts there is no bamboo left. For example, if there are 3 bamboos of height 3, 4, 5 respectively and y = 4. The first cut should be at height 3, after which the heights of the bamboos are 3, 3, 3 respectively and the total amount of bamboo cut is 0 + 1 + 2 = 3. Then, the next 3 cuts should be at height 2, 1, 0 respectively. You want to find out what is the height the x-th cut is performed.

Note that after each query, the bamboos are not actually cut, so the heights of the bamboos remain constant after each query.

 

输入描述:

The first line of input contains two space-separated integers n, q (1 <= n <= 200000, 1 <= q <= 100000).

The next line of input contains n space-separated integers, the i-th of which denotes hi, the height of the i-th bamboo (1 <= hi <= 100000).

The next q lines of input contains 4 space-separated integers each, denoting l, r, x, y (1 <= l <= r <= n, 1 <= x <= y <= 109).

输出描述:

Output q lines of real numbers, the i-th line contains the answer to the i-th query. Your answer will be accepted if its absolute or relative error is less than 10-6.

示例1

输入

复制

5 4
3 5 1 7 4
2 4 3 5
1 4 4 9
1 3 1999 101111
2 2 1 1

输出

复制

2.100000005215406
2.629629638046026
4.822066854685545
0.000000026077032

说明

For the first query, we only consider the bamboos of height 5, 1, 7.

The first cut should be at height 4.7, the total amount of bamboo obtained is 0.3 + 0 + 2.3 = 2.6.

The second cut should be at height 3.4, the total amount of bamboo obtained is 1.3 + 0 + 1.3 = 2.6.

The third cut should be at height 2.1, the total amount of bamboo obtained is 1.3 + 0 + 1.3 = 2.6.

The fourth cut should be at height 13/15, the total amount of bamboo obtained is 37/30 + 2/15 + 37/30 = 2.6.

The fifth cut should be at height 0, the total amount of bamboo obtained is 13/15 + 13/15 + 13/15 = 2.6.

Note that the output values are not exact, but are within the precision requirements.

题目大意:每次给出一个l,r,x,y。 代表要用y次l,r的竹子砍光,每次砍的总量一样。问第x次砍的高度是多少。

解题思路:比赛的时候直接在主席树上二分,没控制好精度。。。一个简单的做法是,先算出剩下x后剩下多少。

然后二分h,分两次查询到l,r小于等于h的和。当时所有都用的浮点数。。。精度炸了。。

#include<bits/stdc++.h>
using namespace std;
const int N = 200005;
typedef long long ll;
const int mmax = 1e5+5;

int h[N],T[N];

int tot;
int tcnt[N*20];
ll tsum[N*20];
int L[N*20],R[N*20];

ll sum[N];

void ins(int &now,int pre,int l,int r,int hi)
{
    now = ++tot;
    tcnt[now] = tcnt[pre] + 1;
    tsum[now] = tsum[pre] + hi;
    L[now] = L[pre];R[now] = R[pre];
    if(l == r)return ;
    int m = (l+r)>>1;
    if(hi<=m) ins(L[now],L[pre],l,m,hi);
    else ins(R[now],R[pre],m+1,r,hi);
}

int cal1(int le,int ri,int l,int r,int k)
{
    if(l==r)
        return l <= k ? tcnt[ri] - tcnt[le] : 0;
    int m = (l+r)>>1;
    if(k<=m) return cal1(L[le],L[ri],l,m,k);
    else return tcnt[L[ri]] - tcnt[L[le]] + cal1(R[le],R[ri],m+1,r,k);
}

ll cal2(int le,int ri,int l,int r,int k)
{
    if(l==r)
        return 1ll*k*l;
    int del = tcnt[L[ri]] - tcnt[L[le]];
    int m = (l+r)>>1;
    if(del>=k) return cal2(L[le],L[ri],l,m,k);
    else return 0ll+tsum[L[ri]] - tsum[L[le]] + cal2(R[le],R[ri],m+1,r,k-del);
}

double V;
bool check(int x,int y,double hi)
{
    int h = floor(hi);
    int k = cal1(T[x-1],T[y],1,mmax,h);
    ll tmp = cal2(T[x-1],T[y],1,mmax,k);
    double tmp2 = tmp*1.0 + (y-x+1-k)*hi;
    return tmp2 - V >= 1e-8;
}

int main()
{
    int n,q;
    scanf("%d%d",&n,&q);
    for(int i=1; i<=n; i++)
    {
        scanf("%d",h+i);
        sum[i] = sum[i-1] + h[i];
        ins(T[i],T[i-1],1,mmax,h[i]);
    }

    while(q--)
    {
        int x,y,_l,_r;
        scanf("%d%d%d%d",&x,&y,&_l,&_r);
        ll tmp = sum[y] - sum[x-1];
        double v = 1.0*tmp/_r;
        V = (1.0*_r - 1.0*_l)*v;
        double l = 0.0 , r = 100004.0;
        while(r-l>=1e-8)
        {
            double m = (l+r)*0.5;
            if(check(x,y,m)) r = m;
            else l = m;
        }
        printf("%.10f\n",r);
    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值