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) = 1, f(2) = 1, f(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 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 integers tpiliri and may be xi (1 ≤ tpi ≤ 2, 1 ≤ 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 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 <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,a[maxn];
struct Matrix
{
int a[][];
Matrix()
{
memset(a,,sizeof(a));
}
void init()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
a[i][j]=(i==j);
}
Matrix operator + (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int j=;j<;j++)
C.a[i][j]=(a[i][j]+B.a[i][j])%mod;
return C;
}
Matrix operator * (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int k=;k<;k++)
for(int j=;j<;j++)
C.a[i][j]=(C.a[i][j]+1LL*a[i][k]*B.a[k][j])%mod;
return C;
}
Matrix operator ^ (const int &t)const
{
Matrix A=(*this),res;
res.init();
int p=t;
while(p)
{
if(p&)res=res*A;
A=A*A;
p>>=;
}
return res;
}
};
Matrix f;
struct Node
{
Matrix sum,lazy;
} T[maxn<<]; void PushUp(int rt)
{
T[rt].sum = T[rt<<].sum + T[rt<<|].sum;
} void PushDown(int L, int R, int rt)
{
int mid = (L + R) >> ;
Matrix t = T[rt].lazy;
T[rt<<].sum = t * T[rt<<].sum;
T[rt<<|].sum = t * T[rt<<|].sum;
T[rt<<].lazy = t * T[rt<<].lazy;
T[rt<<|].lazy = t *T[rt<<|].lazy;
T[rt].lazy.init();
} void Build(int L, int R, int rt)
{
T[rt].lazy.init();
if(L == R)
{
T[rt].sum=f^(a[L]-);
return ;
}
int mid = (L + R) >> ;
Build(Lson);
Build(Rson);
PushUp(rt);
} void Update(int l, int r, Matrix v, int L, int R, int rt)
{
if(l==L && r==R)
{
T[rt].lazy = v * T[rt].lazy;
T[rt].sum = v * T[rt].sum;
return ;
}
int mid = (L + R) >> ;
if(T[rt].lazy.a[][]||T[rt].lazy.a[][])PushDown(L, R, rt);
if(r <= mid) Update(l, r, v, Lson);
else if(l > mid) Update(l, r, v, Rson);
else
{
Update(l, mid, v, Lson);
Update(mid+, r, v, Rson);
}
PushUp(rt);
} ll Query(int l, int r, int L, int R, int rt)
{
if(l==L && r== R)
{
return T[rt].sum.a[][];
}
int mid = (L + R) >> ;
if(T[rt].lazy.a[][]||T[rt].lazy.a[][]) PushDown(L, R, rt);
if(r <= mid) return Query(l, r, Lson);
else if(l > mid) return Query(l, r, Rson);
return (Query(l, mid, Lson) + Query(mid + , r, Rson))%mod;
}
struct node
{
int a,x,y,z;
}op[maxn];
void init()
{
f.a[][]=,f.a[][]=;
f.a[][]=,f.a[][]=;
}
int main()
{
int i,j;
init();
scanf("%d%d",&n,&m);
rep(i,,n)scanf("%d",&a[i]);
Build(,n,);
rep(i,,m)
{
scanf("%d",&op[i].a);
if(op[i].a==)
{
scanf("%d%d%d",&op[i].x,&op[i].y,&op[i].z);
Update(op[i].x,op[i].y,f^op[i].z,,n,);
}
else
{
scanf("%d%d",&op[i].x,&op[i].y);
printf("%lld\n",Query(op[i].x,op[i].y,,n,));
}
}
//system("Pause");
return ;
}

Sasha and Array的更多相关文章

  1. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

  2. codeforces 719E E. Sasha and Array(线段树)

    题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...

  3. 【codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry

    C. Sasha and Array 题目大意&题目链接: http://codeforces.com/problemset/problem/718/C 长度为n的正整数数列,有m次操作,$o ...

  4. CF719E. Sasha and Array [线段树维护矩阵]

    CF719E. Sasha and Array 题意: 对长度为 n 的数列进行 m 次操作, 操作为: a[l..r] 每一项都加一个常数 C, 其中 0 ≤ C ≤ 10^9 求 F[a[l]]+ ...

  5. Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵

    E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...

  6. Codeforces Round #373 (Div. 2) E. Sasha and Array 矩阵快速幂+线段树

    E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input standard ...

  7. 【题解】[CF718C Sasha and Array]

    [题解]CF718C Sasha and Array 对于我这种喜欢写结构体封装起来的选手这道题真是太对胃了\(hhh\) 一句话题解:直接开一颗线段树的矩阵然后暴力维护还要卡卡常数 我们来把\(2 ...

  8. E. Sasha and Array 矩阵快速幂 + 线段树

    E. Sasha and Array 这个题目没有特别难,需要自己仔细想想,一开始我想了一个方法,不对,而且还很复杂,然后lj提示了我一下说矩阵乘,然后再仔细想想就知道怎么写了. 这个就是直接把矩阵放 ...

  9. 718C Sasha and Array

    传送门 题目 Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be ...

随机推荐

  1. C#未将对象引用设置到对象的实例

    未将对象引用设置到对象的实例,这个错误的意思是对象为null,但你还要去取里面的值,所以计算机就不干了.解决办法一般是:用一个你不能确定是不是为null的对象时,尽量做个判断.if(object!=n ...

  2. “Cannot load php5apache2_4.dll into server”问题的解决方法

    摘要 PHP5.5.0+Apache Httpd 2.4.3,完成配置文件的修改后,启动Apache服务器,报Cannot load php5apache2_4.dll into server错误,记 ...

  3. 样式的操作-访问外部定义的css样式

    JS对css的控制力非常强,甚至可以控制外部定义的css样式 ———————————————————————— <style>            .myclass{           ...

  4. hdu_5787_K-wolf Number(数位DP)

    题目链接:hdu_5787_K-wolf Number 题意: 给你一个区间,让你找满足任意k个数位内都没有相同的数字的个数 题解: 因为k不大,就直接将当前pos的前k-1个数传进去就行了 #inc ...

  5. Number-guessing Game

    Number-guessing Game Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Othe ...

  6. http response

    关键词:http response header 下载文件 案例1: 访问某个链接,然后下载文件,需要特定的http头: header("Content-Type:application/z ...

  7. [转]php 在各种web服务器的运行模式

    一.php在apache中运行模式 php在apache中一共有三种工作方式:CGI模式.FastCGI模式.Apache 模块DLL) 以下分别比较: 1. CGI模式与模块模式比较: php在ap ...

  8. linux expr命令参数及用法详解

    expr用法 expr命令一般用于整数值,但也可用于字符串.一般格式为: #expr argument operator argument expr也是一个手工命令行计数器. #$expr 10 + ...

  9. perl命令+关键字

    来源: http://www.cnblogs.com/itech/archive/2012/11/01/2749666.html http://www.garybeene.com/vb/tut-per ...

  10. centos7 python

      yum -y install gcc cd /usr/local/src  wget  https://www.python.org/ftp/python/3.6.0/Python-3.6.0a1 ...