[矩阵+线段树] zoj 3772 Calculate the Function
来源:互联网 发布:牛排一份多少钱 知乎 编辑:程序博客网 时间:2024/06/10 06:11
题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235
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, 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 A1 A2 .. 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
14 71 2 3 41 11 21 31 42 43 44 4
Sample Output
125131144
Author: CHEN, Weijie
Source: The 14th Zhejiang University Programming Contest
题目意思:
给n个数,有m个查询,每次查找区间Li,Ri内的F(Ri),其中F(Li)=save[Li],F[Li+1]=save[Li+1],对于x>=Li+2 F(x)=F(x-1)+F(x-2)*save[x].
1=<n,m<=100000,Ai<=1000000000
解题思路:
矩阵+线段树。
显然
线段树区间维护从右往左的连成后得到的矩阵,建树的时候就更新好,然后来个区间就直接查找就行了,注意矩阵乘法要从右开始,因为最开始的先乘,然后后面的放在开始。
ps:很容易想到矩阵乘法,但基矩阵在变,没想到线段树维护一个矩阵。
代码:
//#include<CSpreadSheet.h>#include<iostream>#include<cmath>#include<cstdio>#include<sstream>#include<cstdlib>#include<string>#include<string.h>#include<cstring>#include<algorithm>#include<vector>#include<map>#include<set>#include<stack>#include<list>#include<queue>#include<ctime>#include<bitset>#include<cmath>#define eps 1e-6#define INF 0x3f3f3f3f#define PI acos(-1.0)#define ll __int64#define LL long long#define lson l,m,(rt<<1)#define rson m+1,r,(rt<<1)|1#define M 1000000007//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;#define Maxn 110000struct Mar{ LL s[3][3]; int row,col; void init(int a,int b) { row=a; col=b; memset(s,0,sizeof(s)); }};int n,m;Mar operator *(const Mar &a ,const Mar &b){ Mar c; c.init(a.row,b.col); for(int k=1;k<=a.col;k++) { for(int i=1;i<=a.row;i++) { if(!a.s[i][k]) continue; for(int j=1;j<=b.col;j++) { if(!b.s[k][j]) continue; c.s[i][j]=(c.s[i][j]+a.s[i][k]*b.s[k][j])%M; } } } return c;}struct Node{ Mar a;}node[Maxn<<2];LL save[Maxn];int cnt;void pushup(int rt){ node[rt].a.init(2,2); node[rt].a=node[rt<<1|1].a*node[rt<<1].a; //右边的矩阵乘以左边的}void build(int l,int r,int rt){ if(l==r) { scanf("%lld",&save[++cnt]); node[rt].a.init(2,2); node[rt].a.s[1][1]=1; node[rt].a.s[1][2]=save[cnt]; node[rt].a.s[2][1]=1; node[rt].a.s[2][2]=0; return ; } int m=(l+r)>>1; build(lson); build(rson); pushup(rt); //构造区间矩阵}Mar query(int L,int R,int l,int r,int rt){ if(l>=L&&r<=R) return node[rt].a; int m=(l+r)>>1; Mar res; res.init(2,2); res.s[1][1]=1,res.s[1][2]=0,res.s[2][1]=0,res.s[2][2]=1; if(R>m) //先查找右边的 res=res*(query(L,R,rson)); if(L<=m) res=res*(query(L,R,lson)); return res;}int main(){ //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t; scanf("%d",&t); while(t--) { cnt=0; scanf("%d%d",&n,&m); build(1,n,1); LL a,b; while(m--) { scanf("%lld%lld",&a,&b); if(b==a) { printf("%lld\n",save[a]); continue; } if(b==(a+1)) { printf("%lld\n",save[a+1]); continue; } Mar temp=query(a+2,b,1,n,1); //printf("%lld %lld %lld %lld\n",temp.s[1][1],temp.s[1][2],temp.s[2][1],temp.s[2][2]); printf("%lld\n",(temp.s[1][1]*save[a+1]%M+temp.s[1][2]*save[a]%M)%M); } } return 0;}
- zoj-3772-Calculate the Function-线段树+矩阵
- ZOJ Problem Set - 3772 Calculate the Function 矩阵+线段树
- zoj 3772 Calculate the Function 线段树+矩阵
- ZOJ 3772 Calculate the Function(线段树 + 矩阵)
- ZOJ 3772 Calculate the Function (线段树 + 矩阵)
- [矩阵+线段树] zoj 3772 Calculate the Function
- zoj 3772 Calculate the Function 矩阵相乘+ 线段树查询
- zoj 3772 Calculate the Function(矩阵+线段树)
- zoj 3772 Calculate the Function(线段树+矩阵乘法)
- ZOJ 3772 Calculate the Function(线段树+矩阵)
- ZOJ 3772 Calculate the Function(矩阵线段树)
- 线段树 + 矩阵 --- ZOJ 3772 Calculate the Function
- ZOJ 3772 Calculate the Function [线段树+矩阵乘法]【思维?】
- ZOJ-3772 Calculate the Function(线段树,矩阵乘法)
- ZOJ 3772 Calculate the Function( 线段树 )
- ZOJ 3772Calculate the Function(线段树)
- ZOJ 3722 Calculate the Function(线段树+矩阵)
- zoj 3772 Calculate the Function(矩阵乘法)
- [BASIC-11] 十六进制转十进制
- onKeyDown-再按一次退出程序
- 3.1 共性规则
- ObjectARX和.Net混合编程在AutoCAD二次开发中的应用
- Linux进程的uid和euid
- [矩阵+线段树] zoj 3772 Calculate the Function
- perl:DBI模块安装与卸载
- 第 3 章命名规则索引
- 2014百度之星资格赛1003
- 类和接口以及之间的关系
- 海驾科目三训练及考试:
- HDU1687 Lucky Light 【贪心】
- 【模式识别】Boosting
- 2.8 类的版式