[codeforces] 719E Sasha and Array 线段树+快速斐波那契
来源:互联网 发布:文言文字典软件 编辑:程序博客网 时间:2024/05/24 06:50
Description
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 = 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.
Sample Input
5 41 1 2 1 12 1 51 2 4 22 2 42 1 5
579
Hint
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<bits/stdc++.h>using namespace std;const int mod = 1e9+7;const int maxn = 1e5+5;struct node{ long long a[2][2]; void reset()//矩阵清零 { memset(a,0,sizeof(a)); } void one()//矩阵初始化 { reset(); a[0][0]=a[1][1]=1; }};node add(node A,node B)//矩阵相加 { node k;k.reset(); for(int i=0;i<2;i++) for(int j=0;j<2;j++) k.a[i][j]=(A.a[i][j]+B.a[i][j])%mod; return k;}node mul(node A,node B)//矩阵相乘 { node k;memset(k.a,0,sizeof(k.a)); for(int i=0;i<2;i++) for(int j=0;j<2;j++) for(int t=0;t<2;t++) k.a[i][j]=(k.a[i][j]+A.a[i][t]*B.a[t][j])%mod; return k;}node qpow(int p)//二分快速矩阵 { node A; A.a[0][0]=0,A.a[1][0]=1,A.a[0][1]=1,A.a[1][1]=1; node K; K.one(); while(p) { if(p%2)//如果是奇数 K=mul(K,A); A=mul(A,A);p/=2; } return K;}typedef node SgTreeDataType;struct treenode{ int L , R , flag; SgTreeDataType sum , lazy; void update(SgTreeDataType v)//这儿变换成矩阵运算 { sum=mul(sum,v); //sum更新 lazy=mul(lazy,v); //lazy更新 flag=1; //标志置1 }};treenode tree[maxn*4];int a[maxn];inline void push_down(int o){ if(tree[o].flag) { tree[2*o].update(tree[o].lazy); tree[2*o+1].update(tree[o].lazy); tree[o].flag = 0;//标志置0tree[o].lazy.one();//初始化lazy }}inline void push_up(int o){ tree[o].sum = add(tree[o*2].sum,tree[o*2+1].sum);//更新父节点 }node tmp;inline void build_tree(int L , int R , int o){ tree[o].L = L , tree[o].R = R,tree[o].sum.reset(),tree[o].lazy.one(),tree[o].flag=0; if(L==R) { tree[o].sum=qpow(a[L]);//初始化 } if (R > L) { int mid = (L+R) >> 1; build_tree(L,mid,o*2); build_tree(mid+1,R,o*2+1); push_up(o); }}inline void update(int QL,int QR,SgTreeDataType v,int o){ int L = tree[o].L , R = tree[o].R; if (QL <= L && R <= QR) tree[o].update(v); else { push_down(o); int mid = (L+R)>>1; if (QL <= mid) update(QL,QR,v,o*2); if (QR > mid) update(QL,QR,v,o*2+1); push_up(o); }}inline SgTreeDataType query(int QL,int QR,int o){ int L = tree[o].L , R = tree[o].R; if (QL <= L && R <= QR) return tree[o].sum; else { push_down(o);//important 向下传递lazy int mid = (L+R)>>1; SgTreeDataType res; res.reset(); if (QL <= mid) res=add(res,query(QL,QR,2*o)); if (QR > mid) res=add(res,query(QL,QR,2*o+1)); push_up(o);//important 更新父节点 return res; }}int n,q;int main(){ tmp.a[0][0]=0,tmp.a[1][0]=1,tmp.a[0][1]=1,tmp.a[1][1]=1; scanf("%d%d",&n,&q); for(int i=1;i<=n;i++) scanf("%d",&a[i]); build_tree(1,n,1); for(int i=1;i<=q;i++) { int op;scanf("%d",&op); if(op==2){ int a,b;scanf("%d%d",&a,&b); printf("%lld\n",query(a,b,1).a[1][0]); } else{ int a,b,c;scanf("%d%d%d",&a,&b,&c); update(a,b,qpow(c),1); } } return 0;}
- [codeforces] 719E Sasha and Array 线段树+快速斐波那契
- [Codeforces 719 E. Sasha and Array] 矩阵快速幂+线段树
- CodeForces-719E Sasha and Array(线段树+矩阵快速幂)
- 【CodeForces 719E】【线段树+矩阵快速幂】 Sasha and Array
- CodeForces 719E Sasha and Array 【线段树】【快速矩阵幂】
- CodeForces - 719E Sasha and Array 线段树 + 矩阵快速幂
- 【Codeforces 718C&719E】Sasha and Array【线段树成段更新+矩阵快速幂】
- cf/Codeforces Round #373 div1-C/div2-E Sasha and Array 线段树 + 维护矩阵快速幂
- Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵
- E. Sasha and Array——矩阵+线段树
- Codeforces 718C Sasha and Array(线段树维护矩阵)
- codeforces 373div1 Sasha and Array 矩阵+线段树
- 线段树+矩阵快速幂 codeforces718C Sasha and Array
- Codeforces Round #373 (Div. 2) E. Sasha and Array
- Codeforces Round #373 (Div. 2) E. Sasha and Array
- CodeForces 718C Sasha and Array(矩阵类+线段树)
- Codeforces 718C C. Sasha and Array
- codeforces 718 C.Sasha and Array
- 403 forbidden解决办法
- spark 问题
- grep参数
- iOS 给测试人员测试手机APP的四种方法:真机运行(略),打ipa包,(testFlighe)邮件,蒲公英(二)testflight
- RecyclerView的万能分割线
- [codeforces] 719E Sasha and Array 线段树+快速斐波那契
- 浅谈java的23种设计模式
- Hibernate Spatial 中加入 SQL Server2008的配置
- bzoj 2761: [JLOI2011]不重复数字 排序
- golang 接力棒玩法 小例子
- Unity3d学习笔记-无限循环滚动背景(完整的商店广告牌组件)
- android 获取process,service,memory,task的信息
- hive PURGE关键字
- CentOS 7最小化安装对系统初始化