传送门

题目

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 modulo 109 + 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 tpi, li, ri 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.

题目大意

给你一个长度为n的数列an,有两种操作

1、将L到R的ai加上X

2、询问L到R之间,f(aL)+f(aL+1)+……+f(aR)的和

f是斐波那契函数

分析

我们可以将斐波那契数转化成它所对应的矩阵,对于每一次加x就是给原来矩阵乘上斐波那契矩阵的x次方。将为赋值的矩阵全部初始化为单位矩阵,然后进行朴素的线段树为何两节点之和即可。

代码

#pragma G++ optimize (2)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define rri register int
const int mod=1e9+;
struct mat {
int g[][];
};
mat d[],one,per;
mat add[];
inline mat operator * (const mat a,const mat b){
mat c;
c.g[][]=c.g[][]=c.g[][]=c.g[][]=;
for(rri i=;i<=;++i)
for(rri k=;k<=;++k)
for(rri j=;j<=;++j)
c.g[i][j]=(c.g[i][j]+(long long)a.g[i][k]*b.g[k][j]%mod)%mod;
return c;
}
inline mat operator + (const mat a,const mat b){
mat c;
for(rri i=;i<=;++i)
for(rri j=;j<=;++j)
c.g[i][j]=(a.g[i][j]+b.g[i][j])%mod;
return c;
}
inline mat pw(mat a,int p){
mat res=a;
p-=;
while(p){
if(p&)res=res*a;
a=a*a;
p>>=;
}
return res;
}
inline int read(){
int x=,f=;char s=getchar();
while(s<''||s>''){if(s=='-')f=-;s=getchar();}
while(s>=''&&s<=''){x=(x<<)+(x<<)+(s-'');s=getchar();}
return f*x;
}
inline void build(int le,int ri,int pl,mat k,int wh){
add[wh]=per;
if(le==ri){
d[wh]=k;
return;
}
int mid=(le+ri)>>;
if(mid>=pl)build(le,mid,pl,k,wh<<);
else build(mid+,ri,pl,k,wh<<|);
d[wh]=d[wh<<]+d[wh<<|];
}
inline void pd(int wh){
if(add[wh].g[][]!=||add[wh].g[][]!=||
add[wh].g[][]!=||add[wh].g[][]!=){
add[wh<<]=add[wh<<]*add[wh];
add[wh<<|]=add[wh<<|]*add[wh];
d[wh<<]=d[wh<<]*add[wh];
d[wh<<|]=d[wh<<|]*add[wh];
add[wh]=per;
}
}
inline void update(int le,int ri,int x,int y,mat k,int wh){
if(le>=x&&ri<=y){
add[wh]=add[wh]*k;
d[wh]=d[wh]*k;
return;
}
int mid=(le+ri)>>;
pd(wh);
if(mid>=x)update(le,mid,x,y,k,wh<<);
if(mid<y)update(mid+,ri,x,y,k,wh<<|);
d[wh]=d[wh<<]+d[wh<<|];
}
inline int q(int le,int ri,int x,int y,int wh){
if(le>=x&&ri<=y)return d[wh].g[][]%mod;
int mid=(le+ri)>>,ans=;
pd(wh);
if(mid>=x)ans=(ans+q(le,mid,x,y,wh<<))%mod;
if(mid<y)ans=(ans+q(mid+,ri,x,y,wh<<|))%mod;
d[wh]=d[wh<<]+d[wh<<|];
return ans%mod;
}
int main()
{ int n,m,x,l,r,k;
one.g[][]=,one.g[][]=one.g[][]=one.g[][]=;
per.g[][]=per.g[][]=,per.g[][]=per.g[][]=;
n=read(),m=read();
for(rri i=;i<=n;++i){
x=read();
build(,n,i,pw(one,x),);
}
for(rri i=;i<=m;++i){
k=read();
if(k==){
l=read(),r=read(),x=read();
update(,n,l,r,pw(one,x),);
}else {
l=read(),r=read();
printf("%d\n",q(,n,l,r,)%mod);
}
}
return ;
}

718C Sasha and Array的更多相关文章

  1. CodeForces 718C Sasha and Array

    线段树. 线段树维护区间矩阵和,操作都是最简单的线段树.$lazy$标记不要记录乘了几次,直接记录乘了几次之后的矩阵就可以了,不然每次下传的时候再算一遍时间复杂度会提高. #pragma commen ...

  2. Codeforces 718C. Sasha and Array(线段树)

    传送门 解题思路: 这道题给了我们一个崭新的角度来看线段树. 我们常常使用的线段树是维护区间的函数的. 这里呢,提示我们线段树其实还可以维护递推. 美好的矩阵递推性质支持了这一功能. 或者说,对于递推 ...

  3. [CF 718C] Sasha and Array

    传送门 Solution 用线段树维护矩阵 第一个操作相当于区间乘 第二个操作相当于区间求和 Code  #include<bits/stdc++.h> #define ll long l ...

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

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

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

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

  6. Sasha and Array

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

  7. 【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 ...

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

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

  9. 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 ...

随机推荐

  1. MKMapView缩放显示全部annotation(转)

    原文  http://blog.csdn.net/favormm/article/details/8028026#define MINIMUM_ZOOM_ARC 0.014 //approximate ...

  2. C语言的inline

    一.inline 关键字用来定义一个类的内联函数,引入它的主要原因是用它替代C中表达式形式的宏定义. 表达式形式的宏定义一例: #define ExpressionName(Var1,Var2) (( ...

  3. HDU - 5412 CRB and Queries (整体二分)

    题目链接 动态区间第k小,但是这道题的话用主席树+树状数组套线段树的空间复杂度是O(nlog2n)会爆掉. 另一种替代的方法是用树状数组套平衡树,空间复杂度降到了O(nlogn),但我感觉平衡树是个挺 ...

  4. HDU3577Fast Arrangement(线段树+lazy)

    Problem Description Chinese always have the railway tickets problem because of its' huge amount of p ...

  5. as3设计模式乱用之工厂模式

    好久没写技术相关的日记了,一忙,二懒,三则被这单调的生活熏得没什么感悟. 其实这篇日记早就想写了,项目开发初期的时候,带学生.经常看到那种乱用设计模式的现象.一方面,公司面试人的时候喜欢问设计模式,另 ...

  6. CentOS6.5下安装mongodb

    MongoDB是目前最常用的NoSQL-非关系型数据库. 本文将介绍在CentOS下如何通过yum安装MongoDB. 1.首先在CentOS6.5下,编辑Mongo的yum源: 在/etc/yum. ...

  7. POJ3249(DAG上的dfs)

    Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 10567   Accepted: 2482 Des ...

  8. java代码流类

    总结:读取到的是字节型转换成字符串. package com.c2; import java.io.*; public class tkrp { public static void main(Str ...

  9. C Primer Plus学习笔记(一)- C语言概述

    从一个简单的C语言程序开始 #include <stdio.h> /*这是一个简单的C语言程序*/ //注释 int main(void) { int num; num = 1; prin ...

  10. 2014.10.1 Spy找对话框

    IntPtr Diaw =FindWindow("#32770","Adobe Acrobat" );