題目連結:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235
Calculate the Function Time Limit: 2 Seconds Memory Limit: 65536 KB
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
Submit Status
題目意思:
給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;}