程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> ZOJ 3772 Calculate the Function(矩陣線段樹)

ZOJ 3772 Calculate the Function(矩陣線段樹)

編輯:C++入門知識

ZOJ 3772 Calculate the Function(矩陣線段樹)


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) = ALiFi(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

題意:給你一段序列,每次問你一段區間構造的數列的最後一項。

分析:這題顯然不能暴力,正確做法就是將矩陣套到線段樹裡,每個節點

裡存的都是一個矩陣,然後求區間pushup時候利用矩陣乘法乘一次就行了

注意矩陣相乘的順序。

 

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define REPF( i , a , b ) for ( int i = a ; i <= b ; ++ i )
#define REP( i , n ) for ( int i = 0 ; i < n ; ++ i )
#define CLEAR( a , x ) memset ( a , x , sizeof a )
const int INF=0x3f3f3f3f;
typedef long long LL;
const int mod=1e9+7;
const int maxn=1e5+100;
LL a[maxn];
int t,n,m;
struct Matrix{
    LL mat[2][2];
    Matrix(){};
    Matrix(int x)
    {
        mat[0][0]=mat[1][0]=1;
        mat[0][1]=x;mat[1][1]=0;
    }
}A[maxn<<2];
Matrix mult(Matrix m1,Matrix m2)
{
    Matrix ans;
    for(int i=0;i<2;i++)
    {
        for(int j=0;j<2;j++)
        {
            ans.mat[i][j]=0;
            for(int k=0;k<2;k++)
                ans.mat[i][j]=(ans.mat[i][j]+m1.mat[i][k]*m2.mat[k][j])%mod;
        }
    }
    return ans;
}
void build(int rs,int l,int r)
{
    if(l==r)
    {
        A[rs]=Matrix(a[l]);
        return ;
    }
    int mid=(l+r)>>1;
    build(rs<<1,l,mid);
    build(rs<<1|1,mid+1,r);
    A[rs]=mult(A[rs<<1|1],A[rs<<1]);
}
Matrix query(int x,int y,int l,int r,int rs)
{
    if(l>=x&&r<=y)
        return A[rs];
    int mid=(l+r)>>1;
    if(y<=mid) return query(x,y,l,mid,rs<<1);
    if(x>mid) return query(x,y,mid+1,r,rs<<1|1);
    return mult(query(mid+1,y,mid+1,r,rs<<1|1),query(x,mid,l,mid,rs<<1));
}
int main()
{
    int l,r;
    scanf(%d,&t);
    while(t--)
    {
        Matrix hehe;
        scanf(%d%d,&n,&m);
        REPF(i,1,n)
          scanf(%lld,&a[i]);
        build(1,1,n);
        while(m--)
        {
            scanf(%d%d,&l,&r);
            if(r-l<=1)
                printf(%lld
,a[r]);
            else
            {
                hehe=query(l+2,r,1,n,1);
                LL ans=(hehe.mat[0][0]*a[l+1]%mod+hehe.mat[0][1]*a[l]%mod)%mod;
                printf(%lld
,ans);
            }
        }
    }
    return 0;
}


 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved