关闭

CodeForces-719E Sasha and Array(线段树+矩阵快速幂)

标签: 线段树矩阵快速幂
408人阅读 评论(0) 收藏 举报
分类:
E. Sasha and Array
time limit per test
5 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types:

  1. 1 l r x — increase all integers on the segment from l to r by values x;
  2. 2 l r — find , where f(x) is the x-th Fibonacci number. As this number may be large, you only have to find it modulo109 + 7.

In this problem we define Fibonacci numbers as follows: f(1) = 1f(2) = 1f(x) = f(x - 1) + f(x - 2) for all x > 2.

Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha?

Input

The first line of the input contains two integers n and m (1 ≤ n ≤ 100 0001 ≤ m ≤ 100 000) — the number of elements in the array and the number of queries respectively.

The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Then follow m lines with queries descriptions. Each of them contains integers tpiliri and may be xi (1 ≤ tpi ≤ 21 ≤ li ≤ ri ≤ n,1 ≤ xi ≤ 109). Here tpi = 1 corresponds to the queries of the first type and tpi corresponds to the queries of the second type.

It's guaranteed that the input will contains at least one query of the second type.

Output

For each query of the second type print the answer modulo 109 + 7.

Examples
input
5 4
1 1 2 1 1
2 1 5
1 2 4 2
2 2 4
2 1 5
output
5
7
9
Note

Initially, array a is equal to 11211.

The answer for the first query of the second type is f(1) + f(1) + f(2) + f(1) + f(1) = 1 + 1 + 1 + 1 + 1 = 5.

After the query 1 2 4 2 array a is equal to 13431.

The answer for the second query of the second type is f(3) + f(4) + f(3) = 2 + 3 + 2 = 7.

The answer for the third query of the second type is f(1) + f(3) + f(4) + f(3) + f(1) = 1 + 2 + 3 + 2 + 1 = 9.

传送门:http://codeforces.com/problemset/problem/719/E
写过的最有意思的线段树,没有之一!
题意:给出有n个元素的数列ai(1<=i<=n)以及m次操作,操作分为两种:①将区间[l,r]的数加x;②询问∑f(ai)(l<=i<=r),其中f(x)是斐波那契数列的第x个数
题解:斐波那契数列可以由2*2矩阵A={1 1,1 0}相乘得到,因此可以想到用线段树存储矩阵,每次更新加x就可以用乘以A^x代替
#include<cstdio>
#include<string.h>
#include<stdlib.h>
#include<math.h>
#include<algorithm>
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
using namespace std;
typedef long long LL;
const LL mod = 1e9 + 7;
const int maxn = 1e5 +5 ;
struct Mat{
    LL x[2][2];
    void init(){
        x[0][0]=x[1][1]=1;
        x[1][0]=x[0][1]=0;
    }
    Mat operator*(const Mat& m2)const{
        Mat m;
        m.x[0][0]=m.x[0][1]=m.x[1][0]=m.x[1][1]=0;
        for(int k=0;k<2;k++)
            for(int i=0;i<2;i++)
                for(int j=0;j<2;j++)
                    m.x[i][j]=(m.x[i][j]+x[i][k]*m2.x[k][j])%mod;
        return m;
    }
    Mat operator+(const Mat& m2)const{
        Mat m;
        for(int i=0;i<2;i++)
            for(int j=0;j<2;j++)
                m.x[i][j]=(x[i][j]+m2.x[i][j])%mod;
        return m;
    }
};
Mat sum[maxn<<2],add[maxn<<2];
Mat pow(LL n){
    Mat m,ret;
    m.x[0][0]=1;m.x[0][1]=1;
    m.x[1][0]=1;m.x[1][1]=0;
    ret.init();
    while(n){
        if(n&1) ret=ret*m;
        m=m*m;
        n>>=1;
    }
    return ret;
}
LL solve(LL n){
    Mat ret=pow(n);
    return ret.x[0][0];
}
void PushUP(int rt){
    sum[rt]=sum[rt<<1]+sum[rt<<1|1];
}
void build(int l,int r,int rt){
    sum[rt].init();
    add[rt].init();
    if(l==r){
        LL x;
        scanf("%I64d",&x);
        sum[rt]=pow(x-1);
        return;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    PushUP(rt);
}

void PushDown(int rt){
    sum[rt<<1]=sum[rt<<1]*add[rt];
    sum[rt<<1|1]=sum[rt<<1|1]*add[rt];
    add[rt<<1]=add[rt<<1]*add[rt];
    add[rt<<1|1]=add[rt<<1|1]*add[rt];
    add[rt].init();
}

void update(int L,int R,Mat c,int l,int r,int rt){
    if(L<=l&&R>=r){
        sum[rt]=sum[rt]*c;
        add[rt]=add[rt]*c;
        return;
    }
    PushDown(rt);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,c,lson);
    if(R>m) update(L,R,c,rson);
    PushUP(rt);
}

LL query(int L,int R,int l,int r,int rt){
    if(L<=l&&R>=r){
        return sum[rt].x[0][0];
    }
    PushDown(rt);
    int m=(l+r)>>1;
    LL ret=0;
    if(L<=m) ret=(ret+query(L,R,lson))%mod;
    if(R>m) ret=(ret+query(L,R,rson))%mod;
    PushUP(rt);
    return ret;
}
void print(int l,int r,int rt){
    if(l==r){
        for(int i=0;i<2;i++){
            for(int j=0;j<2;j++)
                printf("%I64d ",sum[rt].x[i][j]);
            printf("\n");
        }
        printf("\n");
        return;
    }
    int m=(l+r)>>1;
    print(lson);
    print(rson);
}
int main(){
    int n,m;
   // freopen("in.txt","r",stdin);
    while(~scanf("%d%d",&n,&m)){
        build(1,n,1);
        int op,a,b;
        LL x;
        while(m--){
            scanf("%d%d%d",&op,&a,&b);
            if(op==1) {
                scanf("%I64d",&x);
                Mat m=pow(x);
                update(a,b,m,1,n,1);
               // print(1,n,1);
            }
            else printf("%I64d\n",query(a,b,1,n,1));
        }

    }

    return 0;
}


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:41288次
    • 积分:2614
    • 等级:
    • 排名:第15371名
    • 原创:228篇
    • 转载:4篇
    • 译文:0篇
    • 评论:7条
    最新评论