ZOJ 3772 Calculate the Function 线段树+矩阵

Calculate the Function
Time Limit:2000MS     Memory
Limit:
65536KB     64bit IO
Format:
%lld & %llu

Submit Status

Appoint description: 
System Crawler  (2014-04-09)

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) = ALi

  • Fi(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 NM (1
<= NM <= 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 LiRi (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

题目大意:给一个n的序列,若干查询(L,R)。输出F(R)的值。

函数关系:

     
  F(L)=A(L)

     
  F(L+1)=A(L+1)

     
  F(X)=F(X-1)+F(X-2)*A(X) (X-L>=2)

因此每段(L,R)区间        
      

| F(R)
|   | 1
A(R)|*| 1  A(R-1)| *......* | 1  A(L+2)|*|
A(L+1)|

|F(R-1)| = | 1
  0 | | 1    0   |
 ......  | 1    0   | | A(L)  |

每次查询(L+2,R)区间的矩阵乘积再稍微处理一下就行了(当R-L>1时)。


 1 #include<iostream>
2 #include<cstdio>
3 #include<cstring>
4 using namespace std;
5
6 #define Mod 1000000007
7 typedef long long LL;
8 const int maxn=100010;
9 LL a[maxn];
10
11 struct node
12 {
13 LL mat[2][2];
14 void set(int x)//初始化矩阵
15 {
16 mat[0][0]=1;
17 mat[0][1]=a[x]%Mod;
18 mat[1][0]=1;
19 mat[1][1]=0;
20
21 }
22 };
23
24 struct IntervalTree
25 {
26 int left,right;
27 node matrix;
28 }f[maxn<<2];
29
30
31 node mat_mul_mod(node A,node B)//矩阵乘法取模
32 {
33 node ret;
34 ret.mat[0][0]=(A.mat[0][0]*B.mat[0][0]%Mod+A.mat[0][1]*B.mat[1][0]%Mod)%Mod;
35 ret.mat[0][1]=(A.mat[0][0]*B.mat[0][1]%Mod+A.mat[0][1]*B.mat[1][1]%Mod)%Mod;
36 ret.mat[1][0]=(A.mat[1][0]*B.mat[0][0]%Mod+A.mat[1][1]*B.mat[1][0]%Mod)%Mod;
37 ret.mat[1][1]=(A.mat[1][0]*B.mat[0][1]%Mod+A.mat[1][1]*B.mat[1][1]%Mod)%Mod;
38 return ret;
39 }
40
41 void bulid(int left,int right,int i)//建树
42 {
43 int mid;
44 f[i].left=left;
45 f[i].right=right;
46 if(left==right)
47 {
48 f[i].matrix.set(left);
49 return;
50 }
51 mid=(left+right)>>1;
52 bulid(left,mid,i<<1);
53 bulid(mid+1,right,i<<1|1);
54 f[i].matrix=mat_mul_mod(f[i<<1|1].matrix,f[i<<1].matrix);
55 return ;
56 }
57
58 node query(int left,int right,int i)//查询
59 {
60 int mid;
61 if(f[i].left==left && f[i].right==right) return f[i].matrix;
62 mid=(f[i].left+f[i].right)>>1;
63 if(right<=mid) return query(left,right,i<<1);
64 else if(left>mid) return query(left,right,i<<1|1);
65 else return mat_mul_mod(query(mid+1,right,i<<1|1),query(left,mid,i<<1));
66 }
67
68 int main()
69 {
70 int t,n,m,i,lp,rp;
71 scanf("%d",&t);
72 while(t--)
73 {
74 scanf("%d %d",&n,&m);
75 for(i=1;i<=n;i++)
76 scanf("%lld",&a[i]);
77 bulid(1,n,1);
78 while(m--)
79 {
80 scanf("%d %d",&lp,&rp);
81 if(lp==rp || lp+1==rp)
82 {
83 printf("%lld\n",a[rp]%Mod);
84 continue;
85 }
86 node temp=query(lp+2,rp,1);
87 printf("%lld\n",(temp.mat[0][0]*a[lp+1]%Mod+temp.mat[0][1]*a[lp]%Mod)%Mod);
88 }
89 }
90 return 0;
91 }

ZOJ 3772 Calculate the Function 线段树+矩阵,布布扣,bubuko.com

时间: 2024-11-08 19:11:09

ZOJ 3772 Calculate the Function 线段树+矩阵的相关文章

[矩阵+线段树] zoj 3772 Calculate the Function

题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 Calculate the Function Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a list of numbers A1 A2 .. AN and M queries. For the i-th query: The query has two parameters

zoj 3772 Calculate the Function

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这道题需要构造矩阵:F(X)=F(X-1)+F(X-2)*A(X)转化为F(X)*A(X+2)+F(X+1)=F(X+2),然后在构造矩阵 {1, A[x]}  {F(x+1)}  {F(X+2)} {1,    0 }*{F(X)    }={F(X+1)} 然后用线段数维护乘号左边的乘积: 1 #include <cstdio> 2 #include <cs

线段树 + 矩阵 --- ZOJ 3772 Calculate the Function

Calculate the Function Problem's Link:   http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772 Mean: 略 analyse: 简单的线段树维护矩阵. 矩阵乘法的结合律(a * b * c == a * (b * c)),注意矩阵乘法不满足分配率(a *b != b * a). 令 M[x] = [1 A[x]]              [1     0 ] ,那么有 [ F

zoj 3772 Calculate the Function(线段树+矩阵乘法)

Calculate the Function Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a list of numbers A1 A2 .. 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,

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) = ALi Fi(Li + 1) = A(Li + 1) for all x >= Li +

ZOJ 3633 Alice&#39;s present(线段树)

As a doll master, Alice owns a wide range of dolls, and each of them has a number tip on it's back, the tip can be treated as a positive integer. (the number can be repeated). One day, Alice hears that her best friend Marisa's birthday is coming , so

zoj3772【线段树+矩阵相乘】

Calculate the Function Time Limit: 2 Seconds      Memory Limit: 65536 KB You are given a list of numbers A1 A2 .. 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,

Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

D. Vika and Segments Vika has an infinite sheet of squared paper. Initially all squares are white. She introduced a two-dimensional coordinate system on this sheet and drew n black horizontal and vertical segments parallel to the coordinate axes. All

zoj 3888 Twelves Monkeys 二分+线段树维护次小值

链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3888 Twelves Monkeys Time Limit: 5 Seconds      Memory Limit: 32768 KB James Cole is a convicted criminal living beneath a post-apocalyptic Philadelphia. Many years ago, the Earth's surf