ZOJ - 3772 Calculate the Function 线段树

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
  • Fii(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

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

变系数斐波那契数列快速运算
对于普通的斐波那契数列,由于系数矩阵不变,所以我们可以用矩阵快速幂来计算。
但是在这里,由于系数矩阵发生变化,我们不能直接套用矩阵快速幂计算,观察到这个数列的长度1e5,所以我们可以区间查询记录系数矩阵。
记数列中第i位系数矩阵为Ai,根据线性代数中的运算规则,矩阵通常不满足交换律,所以要注意运算顺序。

在构造时,从右到左乘,即
A n ⋅ A n − 1 ⋅ A n − 2 ⋅ ⋅ ⋅ A 1 A_n \cdot A_n-1 \cdot A_n-2 \cdot\cdot\cdot A_1 AnAn1An2A1

在查询时,按照先左后右的顺序查找
A r i g h t ⋅ A l e f t A_{right} \cdot A_{left} ArightAleft

最后注意right-left<2的情况。


#include <stdio.h>
#include <climits>
#include <cstring>
#include <time.h>
#include <math.h>
#include <iostream>
#include <algorithm>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <utility>
#include <vector>
#include <string>

#define INF 0x3f3f3f3f
#define ll long long
#define Pair pair<int,int>
#define re return

#define getLen(name,index) name[index].size()
#define mem(a,b) memset(a,b,sizeof(a))
#define Make(a,b) make_pair(a,b)
#define Push(num) push_back(num)
#define rep(index,star,finish) for(register int index=star;index<finish;index++)
#define drep(index,finish,star) for(register int index=finish;index>=star;index--)
using namespace std;
const int mod=1000000007;
const int maxn=1e5+5;
struct Matrix{
    int S[2][2];
};
void ini(Matrix &m,int num){
    m.S[0][0]=m.S[1][0]=1;
    m.S[0][1]=num,m.S[1][1]=0;
}
Matrix operator *(Matrix a,Matrix b){
    Matrix temp;
    temp.S[0][0]=(1LL*a.S[0][0]*b.S[0][0]+1LL*a.S[0][1]*b.S[1][0])%mod;
    temp.S[0][1]=(1ll*a.S[0][0]*b.S[0][1]+1LL*a.S[0][1]*b.S[1][1])%mod;
    temp.S[1][0]=(1LL*a.S[1][0]*b.S[0][0]+1LL*a.S[1][1]*b.S[1][0])%mod;
    temp.S[1][1]=(1ll*a.S[1][0]*b.S[0][1]+1LL*a.S[1][1]*b.S[1][1])%mod;
    re temp;
}

int store[maxn];
Matrix tree[maxn*4];
void build(int le,int rg,int pos){
    if(le==rg){
        ini(tree[pos],store[le]);
        re;
    }

    int mid=le+(rg-le)/2;
    build(le,mid,pos<<1);
    build(mid+1,rg,pos<<1|1);
    tree[pos]=tree[pos<<1|1]*tree[pos<<1];
}
Matrix query(int left,int right,int inf,int sup,int pos){
    //inf and sup about section
    //left and right about in segment tree

    if(inf<=left && right<=sup){
        re tree[pos];
    }

    Matrix temp;
    temp.S[0][0]=temp.S[1][1]=1,temp.S[0][1]=temp.S[1][0]=0;
    int mid=left+(right-left)/2;
    if(inf<=mid)
        temp=query(left,mid,inf,sup,pos<<1)*temp;
    if(mid<sup)
        temp=query(mid+1,right,inf,sup,pos<<1|1)*temp;
    re temp;
}

int N,Q;
int main(){
    int _;
    scanf("%d",&_);
    while(_--){
        scanf("%d%d",&N,&Q);
        rep(i,1,N+1){
            scanf("%d",&store[i]);
        }
        build(1,N,1);
        int left,right;
        rep(i,0,Q){
            scanf("%d%d",&left,&right);
            if(right-left<2){
                printf("%d\n",store[right]);
                continue;
            }
            Matrix coop=query(1,N,left+2,right,1);
            int ans=(1LL*coop.S[0][0]*store[left+1]+1LL*coop.S[0][1]*store[left])%mod;
            printf("%d\n",ans);
        }
    }


    re 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值