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时)。

 #include<iostream>
#include<cstdio>
#include<cstring>
using namespace std; #define Mod 1000000007
typedef long long LL;
const int maxn=;
LL a[maxn]; struct node
{
LL mat[][];
void set(int x)//初始化矩阵
{
mat[][]=;
mat[][]=a[x]%Mod;
mat[][]=;
mat[][]=; }
}; struct IntervalTree
{
int left,right;
node matrix;
}f[maxn<<]; node mat_mul_mod(node A,node B)//矩阵乘法取模
{
node ret;
ret.mat[][]=(A.mat[][]*B.mat[][]%Mod+A.mat[][]*B.mat[][]%Mod)%Mod;
ret.mat[][]=(A.mat[][]*B.mat[][]%Mod+A.mat[][]*B.mat[][]%Mod)%Mod;
ret.mat[][]=(A.mat[][]*B.mat[][]%Mod+A.mat[][]*B.mat[][]%Mod)%Mod;
ret.mat[][]=(A.mat[][]*B.mat[][]%Mod+A.mat[][]*B.mat[][]%Mod)%Mod;
return ret;
} void bulid(int left,int right,int i)//建树
{
int mid;
f[i].left=left;
f[i].right=right;
if(left==right)
{
f[i].matrix.set(left);
return;
}
mid=(left+right)>>;
bulid(left,mid,i<<);
bulid(mid+,right,i<<|);
f[i].matrix=mat_mul_mod(f[i<<|].matrix,f[i<<].matrix);
return ;
} node query(int left,int right,int i)//查询
{
int mid;
if(f[i].left==left && f[i].right==right) return f[i].matrix;
mid=(f[i].left+f[i].right)>>;
if(right<=mid) return query(left,right,i<<);
else if(left>mid) return query(left,right,i<<|);
else return mat_mul_mod(query(mid+,right,i<<|),query(left,mid,i<<));
} int main()
{
int t,n,m,i,lp,rp;
scanf("%d",&t);
while(t--)
{
scanf("%d %d",&n,&m);
for(i=;i<=n;i++)
scanf("%lld",&a[i]);
bulid(,n,);
while(m--)
{
scanf("%d %d",&lp,&rp);
if(lp==rp || lp+==rp)
{
printf("%lld\n",a[rp]%Mod);
continue;
}
node temp=query(lp+,rp,);
printf("%lld\n",(temp.mat[][]*a[lp+]%Mod+temp.mat[][]*a[lp]%Mod)%Mod);
}
}
return ;
}

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

  1. Z0J 3772 Calculate the Function 线段树+矩阵

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这种题目居然没想到,一开始往矩阵快速幂想去了,因为之前跪了太多矩阵快速幂 ...

  2. ZOJ3772 - Calculate the Function(线段树+矩阵)

    题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...

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

    Calculate the Function Problem's Link:   http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCod ...

  4. 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)转化为 ...

  5. Wannafly Winter Camp 2019.Day 8 div1 E.Souls-like Game(线段树 矩阵快速幂)

    题目链接 \(998244353\)写成\(99824435\)然后调这个线段树模板1.5h= = 以后要注意常量啊啊啊 \(Description\) 每个位置有一个\(3\times3\)的矩阵, ...

  6. 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 whit ...

  7. CF719E(线段树+矩阵快速幂)

    题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...

  8. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

  9. LOJ2980 THUSC2017大魔法师(线段树+矩阵乘法)

    线段树每个节点维护(A,B,C,len)向量,操作即是将其乘上一个矩阵. #include<iostream> #include<cstdio> #include<cma ...

随机推荐

  1. MFC:DISP_FUNCTION 参数

    /*#include <afxdisp.h>DISP_FUNCTION( theClass, pszName, pfnMember, vtRetVal, vtsParams )参数:the ...

  2. SpringMVC的controller层的方法返回值

    1.ModelAndView  既带着数据,又返回视图路劲 2.String 返回试图路径  model带数据  (官方或企业推荐使用此种方式 ,此方法符合解耦思想,即数据,视图,分离 MVC) 3. ...

  3. webpack开始一个项目的步骤

    这几天在学习Vue  用到了webpack打包工具  开始一个项目的时候  需要配置很多项  刚开始写的时候  配置文件总是缺什么再去配置什么  创建项目就用了半个小时  后来觉得应该有个步骤  这样 ...

  4. js cookie 操作

    <html> <head> <meta charset="utf-8"> <title>Javascript cookie</ ...

  5. 201621123080 《Java程序设计》第13周学习总结

    201621123080 <Java程序设计>第13周学习总结 1. 本周学习总结 以你喜欢的方式(思维导图.OneNote或其他)归纳总结多网络相关内容. 2. 为你的系统增加网络功能( ...

  6. 【linux】【rpm】确定程序是否 rpm 安装

    执行 rpm -qf 文件名如果结果显示出安装包那就说明是rpm (或者yum)安装 详情参看 rpm -v  (或者 man rpm) ​

  7. Jsoup -- 网络爬虫解析器

    需要下载jsoup-1.8.1.jar包 jsoup 是一款Java 的HTML解析器,可直接解析某个URL地址.HTML文本内容.它提供了一套非常省力的API,可通过DOM,CSS以及类似于jQue ...

  8. pyecharts用法,本人亲测,陆续更新

    主题 除了默认的白色底色和dark之外,还支持安装扩展包 pip install echarts-themes-pypkg echarts-themes-pypkg 提供了 vintage, maca ...

  9. 原来针对新唐mcu,keil有免费许可

    MDK for Nuvoton Cortex-M0/M23:The MDK for Nuvoton Cortex-M0/M23 is a license paid by Nuvoton. It is  ...

  10. OpenStack, kvm, qemu-kvm以及libvirt之关系

    OpenStack, kvm, qemu-kvm以及libvirt之关系: KVM是最底层的hypervisor,它是用来模拟CPU的运行,它缺少了对network和周边I/O的支持,所以我们是没法直 ...