题目大意

给定一个序列A1 A2 .. AN 和M个查询

  • 每个查询含有两个数 LiRi.
  • 查询定义了一个函数 Fi(x) 在区间 [Li, Ri]Z.
  • Fi(Li) = ALi
  • Fi(Li + 1) = A(Li + 1)
  • 对于所有的x >= Li + 2, Fi(x) = Fi(x - 1) + Fi(x - 2) × Ax

求Fi(Ri)

题解

根据递推式可以构造一个矩阵:

继续展开,最终矩阵就是这个样子的了

因此每次查询就是求矩阵的连乘

普通的做法就是每查询一次线性计算一次上式,时间复杂度O(n),所以总的时间复杂度为O(m*n),显然要跪。。。线段树就很好的解决了这个问题,每个结点保存的都是一个矩阵,这样查询的时候就只需要O(logn)的时间了!

代码:

#include <iostream>
#include <string>
#include <cstring>
#include <stdio.h>
using namespace std;
#define maxn 100005
#define MOD 1000000007
#define lson l,m,s<<1
#define rson m+1,r,s<<1|1
typedef long long LL;
struct Matrix
{
LL mat[2][2];
int r;
void init(int n)
{
memset(mat,0,sizeof(mat));
r=n;
}
};
Matrix matrix_mul(Matrix a,Matrix b)
{
Matrix ans;
ans.init(a.r);
for(int i=0; i<a.r; i++)
for(int j=0; j<a.r; j++)
for(int k=0; k<a.r; k++)
if(a.mat[i][k]&&b.mat[k][j])
ans.mat[i][j]=(ans.mat[i][j]+a.mat[i][k]*b.mat[k][j])%MOD;
return ans;
}
LL a[maxn];
Matrix sum[maxn<<2];
void Pushup(int s)
{
sum[s]=matrix_mul(sum[s<<1|1],sum[s<<1]);
}
void build(int l,int r,int s)
{
sum[s].init(2);
if(l==r)
{
sum[s].mat[0][0]=sum[s].mat[1][0]=1;
sum[s].mat[0][1]=a[r];
return;
}
int m=(l+r)>>1;
build(lson);
build(rson);
Pushup(s);
}
Matrix query(int ql,int qr,int l,int r,int s)
{
if(ql<=l&&r<=qr) return sum[s];
int m=(l+r)>>1;
Matrix ret;
ret.init(2);
ret.mat[0][0]=ret.mat[1][1]=1;
if(qr>m) ret=matrix_mul(ret,query(ql,qr,rson));
if(ql<=m) ret=matrix_mul(ret,query(ql,qr,lson));
return ret;
}
int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int n,m;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++) scanf("%lld",&a[i]);
build(1,n,1);
while(m--)
{
int l,r;
scanf("%d%d",&l,&r);
if(l==r||(l+1)==r)
{
printf("%lld\n",a[r]);
continue;
}
Matrix ans=query(l+2,r,1,n,1);
//printf("%I64d %I64d\n",ans.mat[0][0],ans.mat[0][1]);
// printf("%I64d %I64d\n",ans.mat[1][0],ans.mat[1][1]);
printf("%lld\n",(ans.mat[0][0]*a[l+1]+ans.mat[0][1]*a[l])%MOD);
}
}
return 0;
}

ZOJ3772 - Calculate the Function(线段树+矩阵)的更多相关文章

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

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

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

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

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

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

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

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

  5. 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 ...

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

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

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

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

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

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

  9. CF718C Sasha and Array 线段树+矩阵加速

    正解:线段树 解题报告: 传送门! 首先这种斐波拉契,又到了1e9的范围,又是求和什么的,自然而然要想到矩阵加速昂 然后这里主要是考虑修改操作,ai+=x如果放到矩阵加速中是什么意思呢QAQ? 那不就 ...

随机推荐

  1. Java API —— Random类

    1.Random类概述         此类用于产生随机数         如果用相同的种子创建两个 Random 实例,则对每个实例进行相同的方法调用序列,它们将生成并返回相同的数字序列. 2.构造 ...

  2. PHP输出缓冲控制- Output Control 函数应用详解

    说到输出缓冲,首先要说的是一个叫做缓冲器(buffer)的东西.举个简单的例子说明他的作用:我们在编辑一篇文档时,在我们没有保存之前,系统是不会向磁盘写入的,而是写到buffer中,当buffer写满 ...

  3. GridLayoutManager

    GridLayoutManager Class Overview A RecyclerView.LayoutManager implementations that lays out items in ...

  4. BZOJ 3207 花神的嘲讽计划Ⅰ(函数式线段树)

    题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=3207 题意:给出一个数列,若干询问.每个询问查询[L,R]区间内是否存在某个长度为K的子 ...

  5. R: count number of distinct values in a vector

    numbers <- c(4,23,4,23,5,43,54,56,657,67,67,435,         453,435,324,34,456,56,567,65,34,435) a & ...

  6. Android开发之ProgressDialog与ProgressBar

    ProgressDialog,继承AlertDialog.所以ProgressDialog就是一个在对话框中显示ProgressDialog,并显示进度的文本信息. 并且没有取消和确定按钮,只能通过b ...

  7. hdu 4143 A Simple Problem (变形)

    题目 题意:给n,求x; 直接枚举肯定超时, 把给的式子变形, (y+x)(y-x) = n; 令y-x = b, y+x = a; 枚举b, b 的范围肯定是sqrt(n),  y = (a+b)/ ...

  8. Androidstudio下Generate signed apk提示Error: Expected resource of type id [ResourceType]解决办法

    只需要在报错位置所在的类上面添加: @SuppressWarnings("ResourceType") 即可实现Generate signed apk.

  9. libogg.so fro android编译方法

    在网站下载源代码http://www.xiph.org/downloads/ 解压然后打开cygwin配置 CXX=arm-linux-androideabi-g++.exe LD=arm-linux ...

  10. Brew 编译mod错误Error: L6265E: Non-RWPI Section libspace.o(.bss) cannot be assigned to PI Exec region ER_ZI

    Error: L6265E: Non-RWPI Section libspace.o(.bss) cannot be assigned to PI Exec region ER_ZI.: Error: ...