zoj 3772 Calculate the Function 矩阵相乘+ 线段树查询

题目来源:

http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772

 

分析:

公式来源:http://blog.csdn.net/u013491262/article/details/23089957#comments

 

代码如下:

const int Mod=1000000007;
struct Mat{
    LL a[2][2];
    Mat(){
        memset(a,0,sizeof(a));
    }
    Mat( LL a01)
    {
        a[0][0]=1;
        a[0][1]=a01;
        a[1][0]=1;
        a[1][1]=0;
    }
    Mat operator *( Mat m){
        Mat ans;
        for(int i=0; i<2; i++){
            for(int j=0; j<2 ;j++){
                for(int k=0 ;k<2; k++){
                    ans.a[i][j]=( ans.a[i][j] +  a[i][k]*m.a[k][j] ) % Mod;
                }
            }
        }
        return ans;
    }
};
const int Max_N = 100008;
Mat sum[Max_N <<2]; // 存的是区间点的矩阵连乘积
LL x[Max_N];
int N;
void push_up(int root){
    sum[root]=  sum[root<<1|1] * sum[root<<1];
}
void make_tree(int L, int R, int root){
    if(L == R){
        sum[root]=Mat(x[L]);
        return;
    }
    int mid= (L + R)>>1;
    make_tree(L, mid , root<<1);
    make_tree(mid+1, R, root<<1|1);
    push_up(root);
}
Mat query(int l, int r, int L, int R, int root){
    if(l<=L && R <= r)
        return sum[root];
    int mid=(L + R)>>1;
    if(mid >= r)
        return query(l,r,L, mid, root<<1);
    else if(mid < l)
        return query(l,r,mid+1, R, root<<1|1);
    else
        return query(l,r,mid+1, R, root<<1|1) * query(l , r, L, mid, root<<1);
}
LL Ans(int L, int R){
    if(L > R)
        swap(L,R);
    if(R==L || R==L+1)
        return x[R] % Mod;
    Mat m=query(L+2, R , 1,N,1);
    return (m.a[0][0] * x[L+1] + m.a[0][1]* x[L]) % Mod;
}
int main(){
    int t,m;
    //cin>>t;
    scanf("%d",&t);
    while(t--){
        //cin>>N>>m;
        scanf("%d%d",&N,&m);
        for(int i=1 ; i<=N ; i++)
            scanf("%lld",&x[i]);
        make_tree(1,N,1);
        while(m--){
            int l,r;
            //cin>>l>>r;
            scanf("%d%d",&l,&r);
            //cout<<Ans(l,r)<<endl;//用cin,cout超时
            printf("%lld\n",Ans(l,r)); // 用%I64d 提交zoj g++ 4.4.5居然是PE
        }
    }
    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值