forked from marioyc/Online-Judge-Solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path3772 - Calculate the Function.cpp
72 lines (55 loc) · 1.56 KB
/
3772 - Calculate the Function.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <cstdio>
using namespace std;
#define MOD 1000000007
#define MAXN 100000
struct Matrix{
long long a,b,c,d;
Matrix():
a(1), b(0), c(0), d(1){}
Matrix(long long _a, long long _b, long long _c, long long _d):
a(_a), b(_b), c(_c), d(_d){}
Matrix operator * (Matrix X){
return Matrix(
(a * X.a + b * X.c) % MOD,
(a * X.b + b * X.d) % MOD,
(c * X.a + d * X.c) % MOD,
(c * X.b + d * X.d) % MOD);
}
};
int a[MAXN];
Matrix T[4 * MAXN];
void init(int node, int l, int r){
if(l == r) T[node] = Matrix(0,a[l],1,1);
else{
int mi = (l + r) >> 1;
init(2 * node + 1,l,mi);
init(2 * node + 2,mi + 1,r);
T[node] = T[2 * node + 1] * T[2 * node + 2];
}
}
Matrix query(int node, int l, int r, int x, int y){
if(r < x || l > y) return Matrix();
if(x <= l && r <= y) return T[node];
int mi = (l + r) >> 1;
return query(2 * node + 1,l,mi,x,y) * query(2 * node + 2,mi + 1,r,x,y);
}
int main(){
int T,N,M,l,r;
scanf("%d",&T);
while(T--){
scanf("%d %d",&N,&M);
for(int i = 0;i < N;++i)
scanf("%d",&a[i]);
init(0,0,N - 1);
while(M--){
scanf("%d %d",&l,&r);
--l; --r;
if(r <= l + 1) printf("%d\n",a[r]);
else{
Matrix aux = query(0,0,N - 1,l + 2,r);
printf("%lld\n",(aux.b * a[l] + aux.d * a[l + 1]) % MOD);
}
}
}
return 0;
}