关闭

ZOJ 3772 Calculate the Function(矩阵线段树)

559人阅读 评论(1) 收藏 举报
分类:

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 + 2Fi(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 NM (1 <= NM <= 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 LiRi (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<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<string>
#include<iostream>
#include<queue>
#include<cmath>
#include<map>
#include<stack>
#include<set>
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\n",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\n",ans);
            }
        }
    }
    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:280516次
    • 积分:7346
    • 等级:
    • 排名:第2991名
    • 原创:436篇
    • 转载:1篇
    • 译文:0篇
    • 评论:39条