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

题目描述

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.

首先建立主席树,维护区间内不同长度间的num和sum。

假设砍到长度k,那么总的保留下来长度有所有小于等于K的和加上,大于K的长度的竹子数量*K这个高度。

然后进行二分枚举K,注意一下eps即可。

再不了解的加主页Q或留言即可

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200005;
const int N = 200003;
const int M = maxn * 30;
int n, q, tot; long long sum, num;
int T[maxn], lson[M], rson[M]; long long c[M], nu[M], su[maxn];
int build(int l, int r) {int root = tot++;c[root] = 0;nu[M] = 0;if (l != r) {int mid = (l + r) >> 1;lson[root] = build(l, mid);rson[root] = build(mid + 1, r);}return root;
}
int update(int root, int pos, int val) {int newroot = tot++, tmp = newroot;c[newroot] = c[root] + val; nu[newroot] = nu[root] + 1;int l = 1, r = N;while (l < r) {int mid = (l + r) >> 1;if (pos <= mid) {lson[newroot] = tot++; rson[newroot] = rson[root];newroot = lson[newroot]; root = lson[root];r = mid;}else {rson[newroot] = tot++; lson[newroot] = lson[root];newroot = rson[newroot]; root = rson[root];l = mid + 1;}c[newroot] = c[root] + val;nu[newroot] = nu[root] + 1;}return tmp;
}
void query(int left_root, int right_root, int l, int r, int k) {if (l > k)return;if (r <= k) {sum += c[right_root] - c[left_root];num += nu[right_root] - nu[left_root];return;}int mid = (l + r) >> 1;query(lson[left_root], lson[right_root], l, mid, k);query(rson[left_root], rson[right_root], mid + 1, r, k);
}
int main() {scanf("%d%d", &n, &q); tot = 0;T[0] = build(1, N);for (int i = 1; i <= n; i++) {int hi; scanf("%d", &hi);su[i] = su[i - 1] + hi;T[i] = update(T[i - 1], hi, hi);}while (q--) {int l, r, x, y;scanf("%d%d%d%d", &l, &r, &x, &y);double li = 0, ri = 100000.0, eps = 1e-10;while (abs(li - ri) > eps) {double mid = (li + ri) / 2;sum = 0; num = 0;query(T[l - 1], T[r], 1, N, (int)mid);num = (r - l + 1) - num;double s = mid * num + sum;double step = 1.0*(su[r] - su[l - 1]) / y;if (step * (y - x) < s)ri = mid;else li = mid;}printf("%.15lf\n", li);}return 0;
}

2019牛客暑期多校训练营(第九场)H Cutting Bamboos(主席树+二分)相关推荐

  1. 2021牛客暑期多校训练营7 K-xay loves sequence(主席树+二分)

    K-xay loves sequence 首先不考虑模kkk的限制,容易想到对原数组做一个差分得到di=ai−ai−1d_i=a_i-a_{i-1}di​=ai​−ai−1​,显然对于∀1≤i≤nai ...

  2. 2020牛客暑期多校训练营(第九场)E题 Groundhog Chasing Death

    题意 计算 ∏ i = a b ∏ j = c d g c d ( x i , y j ) \prod_{i=a}^{b}\prod_{j=c}^{d}gcd(x^i,y^j) i=a∏b​j=c∏d ...

  3. [题解] 2019牛客暑期多校第三场H题 Magic Line

    题目链接:https://ac.nowcoder.com/acm/contest/883/H 题意:二维平面上有n个不同的点,构造一条直线把平面分成两个点数相同的部分. 题解:对这n个点以x为第一关键 ...

  4. 牛客多校第九场 H Cutting Bamboos —— 主席树

    题目链接:点我啊╭(╯^╰)╮ 题目大意: 给你一片竹林,编号 1 1 1 到 n n n ,给定初始高度     每次查询区间,问一共砍 y y y 刀的时候,第 x x x 刀的高度     要求 ...

  5. 2019牛客暑期多校训练营(第五场)C generator 2 (BSGS)

    2019牛客暑期多校训练营(第五场)C generator 2 思路 x0=x0x_0 = x_0x0​=x0​ x1=a∗x0∗bx_1 = a * x_0 * bx1​=a∗x0​∗b x2=a∗ ...

  6. 2019牛客暑期多校训练营(第四场)----E- triples II

    首先发出题目链接: 链接:https://ac.nowcoder.com/acm/contest/884/E 来源:牛客网 涉及:位运算,容斥定义,dp 点击这里回到2019牛客暑期多校训练营解题-目 ...

  7. 2019牛客暑期多校训练营(第三场)H.Magic Line

    2019牛客暑期多校训练营(第三场)H.Magic Line 题目链接 题目描述 There are always some problems that seem simple but is diff ...

  8. 2019牛客暑期多校训练营(第九场)A——The power of Fibonacci(循环节+中国剩余定理(互质)||广义BM)

    链接:https://ac.nowcoder.com/acm/contest/889/A 来源:牛客网 时间限制:C/C++ 2秒,其他语言4秒 空间限制:C/C++ 262144K,其他语言5242 ...

  9. 【2019牛客暑期多校训练营(第二场) - H】Second Large Rectangle(单调栈,全1子矩阵变形)

    题干: 链接:https://ac.nowcoder.com/acm/contest/882/H 来源:牛客网 题目描述 Given a N×MN \times MN×M binary matrix. ...

最新文章

  1. “十四五”要建设的「交通强国」,会让我们都坐上自动驾驶车么?
  2. review what i studied `date` - 2017-4-11
  3. springMVC之Interceptor拦截器
  4. 使用python 提取html文件中的特定数据
  5. 什么命令能把Linux搞死机,Linux常见死机原因
  6. 用Python采集了几千条相亲文案,终于发现了告别单身的秘密
  7. Chrome默认开启flash
  8. Python网络爬虫与信息提取 - requests库入门
  9. 推荐几款压箱底的IDEA插件,撸码利器
  10. 约瑟夫环(Data structure and algorithm -C language)—— #YU
  11. 三段式 matlab,1stopt三段式函数拟合
  12. 趣味python编程之经典俄罗斯方块
  13. Execl锁定单元格
  14. 对话深喉:中小App如何突围?(开发者必看)
  15. QGraphicsView使用详解
  16. 《大掌门》欧阳刘彬:基于Cocos2d-x引擎开发经验分享
  17. ,睹证ATRIX 4G 博访摩托罗拉副总裁沈斌
  18. airflow调度方案
  19. MPTCP iperf 发包方式
  20. vue2+vuecli3+elementUI后台管理系列之sidebar导航的开发(五)

热门文章

  1. 人工智能实验-使用遗传算法求函数最值
  2. appdesigner与simulink交互
  3. 无线电通信相关重要指标测试
  4. 统信UOS命令大全 麒麟系统命令大全 Linux常用命令操作大全(非常全非常详细) ubuntu命令大全常用操作命令大全
  5. web前端工程师基础知识点
  6. python time strptime_Python time.strptime方法代碼示例
  7. 灵魂讲师分享的:po是什么?自动化测试po分层如何实现?-带po详细源代码
  8. 不同tab页sessionStorage共享情况
  9. windows下载并安装JDK
  10. RevitAPI: 当前视图为透视图的时候IdlingEvent不会被触发