传送门

题目

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. 掌握sudo的使用

    “sudo”是Unix/Linux平台上的一个非常有用的工具,它允许系统管理员分配给普通用户一些合理的“权利”,让他们执行一些只有超级用户或其他 特许用户才能完成的任务,比如:运行一些像mount,h ...

  2. layui.js框架的启发

    最近做前台设计的MM,应用了layui.js框架,是一个可以按模块加载的js框架,可以实现UI上的一些效果,比如"手风琴折叠"面板.我看了下源码,抽出了其框架,应用到公司的项目中, ...

  3. ENTRYPOINT 与 CMD

    在Dockerfile中 ENTRYPOINT 只有最后一条生效,如果写了10条,前边九条都不生效 ENTRYPOINT 的定义为运行一个Docker容器像运行一个程序一样,就是一个执行的命令 两种写 ...

  4. 加密第四节_IPSec基本理论

    加密第四节_IPSec基本理论 本节内容 IPSec简介 IPSec两种工作模式 判断隧道模式和传输模式 IPSec两种模型 IPSec两个数据库 IPSec基本理论 IPSec简介 提供了网络层的安 ...

  5. Python函数 __import__()

    功能: __import__() 函数用于动态加载类和函数 .返回元组列表. 如果一个模块经常变化就可以使用 __import__() 来动态载入. __import__ 语法: __import__ ...

  6. Linux查找/扫描局域网打印机IP

    假设在 192.168.10.* 有一台网络打印机,但是我们不知道它的地址.一种笨方法就是在浏览器中依次输入 192.168.10.1 到 192.168.10.254,看是否出现管理页面. 另一种思 ...

  7. angular的$watch,$digest和$apply

    第一部分:$watch $watch是一个scope函数,用于监听模型变化,当你的模型部分发生变化时它会通知你. $watch(watchExpression, listener, objectEqu ...

  8. ODP.NET OracleBulkCopy

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using Oracle.DataA ...

  9. S2-045漏洞利用工具&解决方案

    简单的重复造一个轮子,漏洞危害蛮大的 影响版本:Struts 2.3.5 - Struts 2.3.31,Struts 2.5 - Struts 2.5.10 仅供学习测试使用,严禁非法操作! 下载链 ...

  10. C语言枚举类型enum-(转)-温故而知新

    在实际编程中,有些数据的取值往往是有限的,只能是非常少量的整数,并且最好为每个值都取一个名字,以方便在后续代码中使用,比如一个星期只有七天,一年只有十二个月,一个班每周有六门课程等. 以每周七天为例, ...