ZOJ 3772 Calculate the Function 线段树+矩阵
Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu
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 N, M (1 <= N, M <= 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 Li, Ri (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 线段树+矩阵的更多相关文章
- Z0J 3772 Calculate the Function 线段树+矩阵
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这种题目居然没想到,一开始往矩阵快速幂想去了,因为之前跪了太多矩阵快速幂 ...
- ZOJ3772 - Calculate the Function(线段树+矩阵)
题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...
- 线段树 + 矩阵 --- ZOJ 3772 Calculate the Function
Calculate the Function Problem's Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCod ...
- 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)转化为 ...
- Wannafly Winter Camp 2019.Day 8 div1 E.Souls-like Game(线段树 矩阵快速幂)
题目链接 \(998244353\)写成\(99824435\)然后调这个线段树模板1.5h= = 以后要注意常量啊啊啊 \(Description\) 每个位置有一个\(3\times3\)的矩阵, ...
- 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 ...
- CF719E(线段树+矩阵快速幂)
题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...
- 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...
- LOJ2980 THUSC2017大魔法师(线段树+矩阵乘法)
线段树每个节点维护(A,B,C,len)向量,操作即是将其乘上一个矩阵. #include<iostream> #include<cstdio> #include<cma ...
随机推荐
- js类型判别大合集
1.typeof number,string,boolean,undefined,symbol,object,function 对象中除了函数为function,其他对象都判别为object, 缺陷: ...
- MySql数据库中where的使用
SELECT * from runoob_tbl WHERE runoob_author='菜鸟教程'; MySQL 的 WHERE 子句的字符串比较是不区分大小写的. 你可以使用 BINARY 关键 ...
- python 产生随机数
Python中的random模块用于生成随机数.下面介绍一下random模块中最常用的几个函数. random.random random.random()用于生成一个0到1的随机符点数: 0 < ...
- cocos2dx 字体描边遇到的描边缺失的bug
在cocos中,设置字体描边可以用enableOutline(cc.c4b(30, 10, 0, 255), 2)函数设置,第一个参数是字体颜色,第二个参数是描边轮廓大小,单位是2个像素, 我在使用过 ...
- cocos2d-x中的字符串操作
1:循环体中字符串的构造. 通常用于多个有规律的文件的名字,诸如:[NSString stringWithFormat:@"filed.png",i].我们可以通过spr ...
- stataic 变量
static 是静态变量的的类型说明符 静态变量属于静态存储方式,(外部变量也是静态存储方式) 静态的局部变量 静态局部变量属于静态存储方式,它具有以下特点: (1)静态局部变量在函数内定义 它的生存 ...
- ipvsadm启动报错解决方法
Centos7 yum -y install ipvadm 安装后,启动ipvsadm却报错. Redirecting to /bin/systemctl start ipvsadm.service ...
- 教程笔记《JavaScript深入浅出》
一.数据类型 javascript是弱数据类型语言,不需要显式的定义类型,一共有如下六种数据类型 五种基本类型:number,string,boolean,null,undefined 一种复合类型: ...
- Python基础——判断和循环
判断 缩进代替大括号. 冒号(:)后换号缩进. if test=100 if test>50: print('OK') print('test') if-elif-else test=50 if ...
- 某比赛小记1- 挑选第N大数字
题目:给1000个数字(有重复),从小到大排列后,挑选第N个数字. 数字文件如下:numbers.rar ,挑选第727个数字. java版本: //数组初始化 String str = " ...