【发布时间】:2016-12-11 09:48:11
【问题描述】:
一排有N个花园,编号从1到N,每个第i个花园都有Ci胡萝卜。 我们必须将总胡萝卜的值存储在数组的每个可能的连续子序列的花园中。
现在我们必须对获得的数组进行排序并回答以下 Q 查询。在每个查询中,我们都想知道上面获得的排序数组中从 L 到 R(包括两者)的值的总和。
示例测试用例
Input:
3 3 //First number is the total no. of gardens. Second is no. of queries
4 9 1 //No. of carrots in each of the gardens
1 6 // Query return sum from L to R.
2 4
3 3
Output:
51 23 9 // Respective Output for 3 queries.
说明
Gardens [1, 2, 3] has [4, 9, 1] carrots respectively.
All possible continuous gardens are { [1], [2], [3], [1, 2], [2, 3], [1, 2, 3] } .
Sum of carrots in each subgardens is {4, 9, 1, 13, 10, 14}
Sorted array is {1, 4, 9, 10, 13, 14} .
Now Queries for 1 6 sum is 1+4+9+10+13+14 which is 51,
next 2 4 so 4+9+10 hence 23, and 3 3 which is 9.
现在我已经使用模拟/后缀总和解决了这个问题,但原来的问题有很大的约束
1 ≤ No. of gardens ≤ 2*10^5
1 ≤ Carrots in a particular garden ≤ 100
1 ≤ Li ≤ Ri ≤ N(N+1)/2
1 ≤ No. of queries ≤ 20
现在,当我尝试为大到 2*10^5 的 N 创建所有可能的连续子序列时。我得到的连续子序列约为 10^10,太大而无法存储在数组中。
对此有什么可能的解决方法,如何在不实际存储所有连续子序列的总和的情况下回答查询?
【问题讨论】:
-
查询次数有什么限制?
-
为查询添加约束
标签: algorithm sorting binary-search