Cutting Bamboos(牛客多校第九场H主席树+二分+思维)
链接: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 h_{i}h
i
.
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.
题意:有n棵树,每棵树有高度hi。有m次操作,每次要从l到r砍树,每次砍相同的总高度,砍y次砍完。问砍到第x次的时候该砍哪儿。
看到l到r,不自觉的就想到线段树,主席树等等。
对于l到r区间内的树,总高度我们是知道的,求一个前缀就可以求出来。砍y次砍完,那么我们每一次砍树的总高度我们是知道的。砍掉x次之后剩余的树的总高度我们也是可以求出来的。第x次该砍哪儿,我们二分去找这个高度。找出来之后,我们可以求出来低于这个高度的树的数量和总高度(主席树),那么我们就能求出来根据这个高度求出砍完之后剩余的树高度,我们可以去和标准的答案去比较。
代码如下:
努力加油a啊,(o)/~
总结
以上是生活随笔为你收集整理的Cutting Bamboos(牛客多校第九场H主席树+二分+思维)的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: All men are brothers
- 下一篇: Super Mario HDU - 44