Problem
DZY Loves Fibonacci Numbers
Description
In mathematical terms, the sequence of is defined by the recurrence relation .
loves Fibonacci numbers very much. Today gives you an array consisting of integers: . Moreover, there are queries, each query has one of the two types:
- Format of the query “ ”. In reply to the query, you need to add to each element , where .
- Format of the query “ ”. In reply to the query you should output the value of modulo .
Help reply to all the queries.
Input
The first line of the input contains two integers and . The second line contains n integers — initial array .
Then, lines follow. A single line describes a single query in the format given in the statement. It is guaranteed that for each query inequality holds.
Output
For each query of the second type, print the value of the sum on a single line.
Example
Input
1 | 4 4 |
Output
1 | 17 |
Note
After the first query, .
For the second query, .
After the third query, .
For the fourth query, .
标签:线段树
Translation
给出一个长为级别的初始数组,要求维护两种操作:
- 将中的每个数对应加上从的斐波那契数,即使加上
- 询问模的值
Solution
不难想到此题需要用线段树维护。不过难点在于如何合并标记。
初步想法是每次打标记时记录下此区间是从斐波那契数列的多少项开始一一对应地加进去,不过这样是无法合并标记的,每个结点只能有一个标记,可以被卡成。
考虑把标记换一种存法。对于一个数列,我们将其称为一个“伪斐波那契数列”,不难发现其等于几个斐波那契数列的子序列之和,即在原题中,不管如何加,每个区间最后加的数列都是一个伪斐波那契数列。而此序列可以仅通过最前面的两项和推出后面的任意项以及前若干项之和,即
用这两个公式我们可以计算任意项及前任意项的和。
每个标记为一个数对,那么合并标记的时候将两个标记的和分别相加,得到即可。
总时间复杂度。
Code
1 |
|