ZOJ 3772 Calculate the Function(矩陣線段樹)
Description
You are given a list of numbers A1A2 .. AN and M queries. For the i-th query:
The query has two parameters
Li and
Ri.The query will define a function
Fi(x) on the domain
[Li, Ri] ∈
Z.
Fi(Li) =
ALiFi(Li + 1) =
A(Li + 1)for all
x >=
Li + 2,
Fi(x) =
Fi(x - 1) +
Fi(x - 2) ×
Ax
You task is to calculate Fi(Ri) for each query. Because the answer can be very large, you should output the remainder of the answer divided by 1000000007.
Input
There are multiple test cases. The first line of input is an integer T indicates the number of test cases. For each test case:
The first line contains two integers N, M (1 <= N, M <= 100000). The second line contains N integers A1A2 .. AN (1 <= Ai <= 1000000000).
The next M lines, each line is a query with two integer parameters Li, Ri (1 <= Li <= Ri <= N).
Output
For each test case, output the remainder of the answer divided by 1000000007.
Sample Input
1
4 7
1 2 3 4
1 1
1 2
1 3
1 4
2 4
3 4
4 4
Sample Output
1
2
5
13
11
4
4
題意:給你一段序列,每次問你一段區間構造的數列的最後一項。
分析:這題顯然不能暴力,正確做法就是將矩陣套到線段樹裡,每個節點
裡存的都是一個矩陣,然後求區間pushup時候利用矩陣乘法乘一次就行了
注意矩陣相乘的順序。
#include
#include
#include
#include
#include
#include
#include
#include
#include