标签:res ll 矩阵 CodeForces 719E second queries query type
Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types:
- 1 l r x — increase all integers on the segment from l to r by values x;
- 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 = 1corresponds 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.
题意:设f(x)为斐波那契数列的第x项。给你一个长度为n的序列a[],接下来有m个操作:
1 l r x:区间[l,r]中每个数都加x
2 l r:查询区间[l,r]中每个数的斐波那契和。例如区间[1,2]中数为1,2,那么答案就是f(1)+f(2)=2,答案对mod取模;
思路:这种斐波那契数列取模一想就能想到矩阵快速幂。我们可以用线段树维护一个矩阵,因为矩阵满足分配率,因此很好维护。
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int maxn=1e5+100;
ll a[maxn];int n,m;
const ll MOD=1e9+7;
struct Matrix
{
ll a[2][2];
void init()
{
memset(a,0,sizeof(a));
for(int i=0;i<2;i++) a[i][i]=1;
}
bool OK()
{
if(a[0][0]==1&&a[0][1]==0&&a[1][0]==0&&a[1][1]==1) return false;
return true;
}
};
struct node{
int l;
int r;
Matrix laz;
Matrix res;
}tree[maxn<<2];
Matrix mat_mul(Matrix x,Matrix y)
{
Matrix res;
memset(res.a,0,sizeof(res.a));
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
for(int k=0;k<2;k++)
res.a[i][j]=(res.a[i][j]+x.a[i][k]*y.a[k][j]%MOD)%MOD;
return res;
}
Matrix mat_pow(ll n)
{
Matrix c,res;
c.a[0][0]=c.a[0][1]=c.a[1][0]=1;
c.a[1][1]=0;
res.init();
while(n)
{
if(n&1) res=mat_mul(res,c);
c=mat_mul(c,c);
n=n>>1;
}
return res;
}
Matrix qpow(Matrix res,ll n)
{
Matrix c;
c.a[0][0]=c.a[0][1]=c.a[1][0]=1;
c.a[1][1]=0;
while(n)
{
if(n&1) res=mat_mul(res,c);
c=mat_mul(c,c);
n=n>>1;
}
return res;
}
void pushup(int cur)
{
tree[cur].res.a[0][0]=(tree[cur<<1].res.a[0][0]+tree[cur<<1|1].res.a[0][0])%MOD;
tree[cur].res.a[0][1]=(tree[cur<<1].res.a[0][1]+tree[cur<<1|1].res.a[0][1])%MOD;
tree[cur].res.a[1][0]=(tree[cur<<1].res.a[1][0]+tree[cur<<1|1].res.a[1][0])%MOD;
tree[cur].res.a[1][1]=(tree[cur<<1].res.a[1][1]+tree[cur<<1|1].res.a[1][1])%MOD;
}
void pushdown(int cur)
{
if(tree[cur].laz.OK())
{
tree[cur<<1].res=mat_mul(tree[cur<<1].res,tree[cur].laz);
tree[cur<<1].laz=mat_mul(tree[cur<<1].laz,tree[cur].laz);
tree[cur<<1|1].res=mat_mul(tree[cur<<1|1].res,tree[cur].laz);
tree[cur<<1|1].laz=mat_mul(tree[cur<<1|1].laz,tree[cur].laz);
tree[cur].laz.init();
}
}
void build(int l,int r,int cur)
{
tree[cur].l=l;
tree[cur].r=r;
tree[cur].laz.init();
if(l==r)
{
tree[cur].res=mat_pow(a[l]);
return ;
}
int m=(l+r)>>1;
build(l,m,cur<<1);
build(m+1,r,cur<<1|1);
pushup(cur);
}
void update(int L,int R,Matrix val,int cur)
{
if(L<=tree[cur].l&&tree[cur].r<=R)
{
tree[cur].laz=mat_mul(tree[cur].laz,val);
tree[cur].res=mat_mul(tree[cur].res,val);
return ;
}
pushdown(cur);
if(L<=tree[cur<<1].r) update(L,R,val,cur<<1);
if(R>tree[cur<<1].r) update(L,R,val,cur<<1|1);
pushup(cur);
}
ll query(int L,int R,int cur)
{
if(L<=tree[cur].l&&tree[cur].r<=R) return tree[cur].res.a[0][1]%MOD;
pushdown(cur);
ll res=0;
if(L<=tree[cur<<1].r) res=(res+query(L,R,cur<<1))%MOD;
if(R>tree[cur<<1].r) res=(res+query(L,R,cur<<1|1))%MOD;
return res%MOD;
}
int main()
{
int op,l,r;
ll x;
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%lld",&a[i]);
build(1,n,1);
while(m--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d%d%lld",&l,&r,&x);
Matrix tmp=mat_pow(x);
update(l,r,tmp,1);
}
else
{
scanf("%d%d",&l,&r);
printf("%lld\n",query(l,r,1));
}
}
return 0;
}
标签:res,ll,矩阵,CodeForces,719E,second,queries,query,type 来源: https://blog.csdn.net/why932346109/article/details/98960715
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。