Algorithm 更新数组中的范围

Algorithm 更新数组中的范围,algorithm,Algorithm,给定长度为N的数组。我需要在此数组上执行某些操作,这些操作有两种类型: Update a range [x...y]: In which we must divide each number in the given range [x...y] by K.(This division is integer division) Query a range [x...y]: In which we must output the sum of all the numbers presently i

给定长度为N的数组。我需要在此数组上执行某些操作,这些操作有两种类型:

Update a range [x...y]: In which we must divide each number in the given range [x...y] by K.(This division is integer division)

Query a range [x...y]: In which we must output the sum of all the numbers presently in range [x...y].
示例:假设我们有N=5,K=2,数组为{1,1,1,5}。我们有Q(=5)查询,如下所示:

2 1 5

1 1 2

2 1 5

155

2 1 5

然后,在该输出中将:

九,

七,

四,


现在我知道如何解决数量较少的查询,但如果Q和N较大,如何解决。

我不能准确理解您的问题。但我了解查询

->当您对这样的输入运行多个查询时

例如: 如果在[200010000]范围内有第一个查询,在[20004000]范围内有第二个查询 你把这些数字加了很多次,这很糟糕

->尝试使用累积频率的概念

例:


我很困惑。如果有五个查询,为什么只有三行输出?如果只有两个参数
x
y
,为什么所有查询都有三个数字?还是说你有五次手术?如果是这样,您如何区分更新和查询?@Kevin Update以“1”表示,查询以“2”表示。我知道查询,主要问题是更新值,其中我需要将一系列元素中的每个值除以K。我认为分段树实现会起作用,但我不知道如何实现它
1. Compute the cumulative sum for all indexes [0...n-1] in Array.

2. Now, When you need sum for a certain query [x...y] then just do
   Array[y]-Array[x-1].....which would be direct result.

3. Believe me this would save you lot of computation.

4. This concept is similar to Dynamic Programming(try Googling it!!)