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 + 2Fi(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 }

 

                     

转载于:https://www.cnblogs.com/xiong-/p/3752965.html

已经博主授权,源码转载自 https://pan.quark.cn/s/a4b39357ea24 QueueForMcu 基于单片机实现的队列功能模块,主要用于8位、16位、32位非运行RTOS的单片机应用,兼容大多数单片机平台。 开源代码:https://.com/xiaoxinpro/QueueForMcu 一、特性 动态创建队列对象 动态设置队列数据缓冲区 静态指定队列元素数据长度 采用值传递的方式保存队列数据 二、快速使用 三、配置说明 目前QueueForMcu只有一个静态配置项,具体如下: 在文件 中有一个宏定义 用于指定队列元素的数据长度,默认是 ,可以根据需要更改为其他数据类型。 四、数据结构 队列的数据结构为 用于保存队列的状态,源码如下: 其中 为配置项中自定义的数据类型。 五、创建队列 1、创建队列缓存 由于我们采用值传递的方式保存队列数据,因此我们在创建队列前要手动创建一个队列缓存区,用于存放队列数据。 以上代码即创建一个大小为 的队列缓存区。 2、创建队列结构 接下来使用 创建队列结构,用于保存队列的状态: 3、初始化队列 准备好队列缓存和队列结构后调用 函数来创建队列,该函数原型如下: 参数说明: 参考代码: 六、压入队列 1、单数据压入 将数据压入队列尾部使用 函数,该函数原型如下: 参数说明: 返回值说明: 该函数会返回一个 枚举数据类型,返回值会根据队列状态返回以下几个值: 参考代码: 2、多数据压入 若需要将多个数据(数组)压入队列可以使用 函数,原理上循环调用 函数来实现的,函数原型如下: 参数说明: 当数组长度大于队列剩余长度时,数组多余的数据将被忽略。 返回值说明: 该函数将返回实际被压入到队列中的数据长度。 当队列中的剩余长度富余...
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值