Codeforces Round 718C (矩阵线段树)

来源:互联网 发布:linux c代码创建目录 编辑:程序博客网 时间:2024/05/22 17:33

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

  1. 1 l r x — increase all integers on the segment froml tor by valuesx;
  2. 2 l r — find , wheref(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) = 1, f(2) = 1, f(x) = f(x - 1) + f(x - 2) for allx > 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 andm (1 ≤ n ≤ 100 000,1 ≤ 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 integerstpi,li,ri and may bexi (1 ≤ tpi ≤ 2,1 ≤ li ≤ ri ≤ n,1 ≤ xi ≤ 109). Heretpi = 1 corresponds to the queries of the first type andtpi 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 41 1 2 1 12 1 51 2 4 22 2 42 1 5
Output
579
Note

Initially, array a is equal to 1, 1, 2, 1, 1.

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 1, 3, 4, 3, 1.

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.



题意:让你用线段树维护一个下标序列,每次区间加或者查询以这个区间所有数为下标的菲波那切数列和。



分析:线段树中每个节点存放一个矩阵就可以了。


#include <cstdio>#include <iostream>#include <algorithm>#include <cstdlib>#include <cstdio>#include <set>#include <map>#include <vector>#include <stack>#include <cmath>#include <queue>#include <cstring>#define MOD 1000000007#define N 200005using namespace std;typedef long long ll;int T,n,m,k,num,a[N];struct Matrix{    ll val[3][3];    int x ,y;    void init()    {        memset(val,0,sizeof(val));        val[1][1] = val[2][2] = 1;    }    Matrix(int w,int v)    {        x = w,y = v;        memset(val,0,sizeof(val));    }    Matrix(){memset(val,0,sizeof(val));}    friend Matrix operator *(const Matrix a,const Matrix b)    {        Matrix c;        c.x = a.x,c.y = b.y;        for(int i = 1;i <= a.x;i++)         for(int j = 1;j <= b.y;j++)          for(int k = 1;k <= b.x;k++)           c.val[i][j] = (c.val[i][j] + a.val[i][k]*b.val[k][j]) % MOD;        return c;    }    friend Matrix operator +(const Matrix a,const Matrix b)    {        Matrix c;        c.x = a.x,c.y = b.y;        for(int i = 1;i <= a.x;i++)         for(int j = 1;j <= b.y;j++)          c.val[i][j] = (a.val[i][j] + b.val[i][j]) % MOD;        return c;    }};Matrix ksm(Matrix a,ll b){    Matrix c = a;    b--;    while(b)    {        if(b & 1) c = c * a;        a = a * a;        b >>= 1;    }    return c;}struct Segtree{    int l,r;    bool down;    Matrix dow = Matrix(2,2);    Matrix sta = Matrix(2,2);}tree[4*N];void push_up(int i){    tree[i].sta = tree[2*i].sta + tree[2*i+1].sta;}void build(int i,int l,int r){    tree[i].l = l;    tree[i].r = r;    tree[i].down = 0;    tree[i].dow.init();    if(l == r)    {        Matrix temp = Matrix(2,2);        temp.val[1][1] = temp.val[1][2] = temp.val[2][1] = 1;        if(a[l] != 1) tree[i].sta = ksm(temp,a[l] - 1);        else tree[i].sta.init();        return;    }    int mid = (l+r)>>1;    build(2*i,l,mid);    build(2*i+1,mid+1,r);    push_up(i);}void push_down(int i){    if(!tree[i].down) return;    tree[2*i].down = tree[2*i+1].down = true;    tree[2*i].dow = tree[2*i].dow * tree[i].dow;    tree[2*i+1].dow = tree[2*i+1].dow * tree[i].dow;    tree[2*i].sta = tree[2*i].sta * tree[i].dow;    tree[2*i+1].sta = tree[2*i+1].sta * tree[i].dow;    tree[i].down = false;    tree[i].dow.init();}void deal_add(int i,int x,int y,Matrix val){    int l = tree[i].l,r = tree[i].r;    if(l == x && r == y)    {        tree[i].sta = tree[i].sta * val;        tree[i].dow = tree[i].dow * val;        tree[i].down = true;        return;    }    push_down(i);    int mid = (l+r)/2;    if(y <= mid) deal_add(2*i,x,y,val);    else     if(x <= mid)     {       deal_add(2*i,x,mid,val);       deal_add(2*i+1,mid+1,y,val);     }     else deal_add(2*i+1,x,y,val);     push_up(i);}ll Find(int i,int x,int y){    int l = tree[i].l,r = tree[i].r;    if(l == x && r == y) return tree[i].sta.val[1][1];    push_down(i);    int mid = (l+r)/2;    if(y <= mid) return Find(2*i,x,y);    else     if(x <= mid) return (Find(2*i,x,mid) + Find(2*i+1,mid+1,y)) % MOD;     else return Find(2*i+1,x,y);}int main(){    scanf("%d%d",&n,&m);    for(int i = 1;i <= n;i++) scanf("%d",&a[i]);    build(1,1,n);    for(int i = 1;i <= m;i++)    {        scanf("%d",&k);        if(k == 1)        {            int l,r,x;            scanf("%d%d%d",&l,&r,&x);            Matrix temp = Matrix(2,2);            temp.val[1][1] = temp.val[1][2] = temp.val[2][1] = 1;            temp = ksm(temp,x);            deal_add(1,l,r,temp);        }        else        {            int l,r;            scanf("%d%d",&l,&r);            printf("%I64d\n",Find(1,l,r));        }    }}


0 0
原创粉丝点击