Codeforces Round 718C(维护矩阵的线段树)
2016-09-25 15:51
429 查看
C. 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 l r x — increase all integers on the segment from
l to r by values
x;
2 l r — find
![](http://codeforces.com/predownloaded/21/60/21609daba8f7bc13a6b19ca8be77d0d65a503521.png)
, 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.
Examples
Input
Output
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.
题目大意:
有一棵线段树,查询以每个端点为下标值的斐波那契值的和。
解题思路:
一般情况下,求任意斐波那契数我们使用的是矩阵快速幂,这题是一个对于斐波那契数的线段树,所以我们在线段树上维护矩阵即可。
AC代码(用了Q巨的模版):
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 l r x — increase all integers on the segment from
l to r by values
x;
2 l r — find
![](http://codeforces.com/predownloaded/21/60/21609daba8f7bc13a6b19ca8be77d0d65a503521.png)
, 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.
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.
题目大意:
有一棵线段树,查询以每个端点为下标值的斐波那契值的和。
解题思路:
一般情况下,求任意斐波那契数我们使用的是矩阵快速幂,这题是一个对于斐波那契数的线段树,所以我们在线段树上维护矩阵即可。
AC代码(用了Q巨的模版):
#include <iostream> #include <cstdio> #include <algorithm> #include <vector> #include <cstring> using namespace std; #define MOD 1000000007 const int MAXN=100000+3; int a[MAXN]; struct Matrix { int a[2][2];//矩阵大小根据需求修改 Matrix() { memset(a,0,sizeof(a)); } void init() { for(int i=0;i<2;i++) for(int j=0;j<2;j++) a[i][j]=(i==j); } Matrix operator + (const Matrix &B)const { Matrix C; for(int i=0;i<2;i++) for(int j=0;j<2;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=0;i<2;i++) for(int k=0;k<2;k++) for(int j=0;j<2;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&1)res=res*A; A=A*A; p>>=1; } return res; } }; Matrix f,s[MAXN<<2],lz[MAXN<<2]; void pushUp(int n) { s =s[n<<1]+s[n<<1|1]; } void pushDown(int n) { s[n<<1]=lz *s[n<<1]; s[n<<1|1]=lz *s[n<<1|1]; lz[n<<1]=lz *lz[n<<1]; lz[n<<1|1]=lz *lz[n<<1|1]; lz .init(); } void build(int l,int r,int n)//使用时输入(1,N,1),N为总长度 { lz .init(); if(l==r) { s =f^(a[l]-1); return; } int m=(l+r)/2; build(l,m,n<<1); build(m+1,r,n<<1|1); pushUp(n); } void update(int ql,int qr,int l,int r,Matrix v,int n)//使用时输入(l,r.1,N,x,1) { if(l==ql && r==qr) { s =v*s ; lz =v*lz ; return; } pushDown(n); int m=(l+r)/2; if(qr<=m) update(ql,qr,l,m,v,n<<1); else if(ql>m) update(ql,qr,m+1,r,v,n<<1|1); else { update(ql,m,l,m,v,n<<1); update(m+1,qr,m+1,r,v,n<<1|1); } pushUp(n); } int query(int ql,int qr,int l,int r,int n) { if(l==ql && r==qr) return s .a[0][0]; pushDown(n); int m=(l+r)/2; if(qr<=m) return query(ql,qr,l,m,n<<1); if(ql>m) return query(ql,qr,m+1,r,n<<1|1); return (query(ql,m,l,m,n<<1)+query(m+1,qr,m+1,r,n<<1|1))%MOD; } int N,M; int main() { f.a[0][0]=1; f.a[0][1]=1; f.a[1][0]=1; f.a[1][1]=0; scanf("%d%d",&N,&M); for(int i=1;i<=N;++i) scanf("%d",&a[i]); build(1,N,1); while(M--) { int type,l,r; scanf("%d%d%d",&type,&l,&r); if(type==1) { int x; scanf("%d",&x); update(l,r,1,N,f^x,1); } else printf("%d\n",query(l,r,1,N,1)); } return 0; }
相关文章推荐
- Codeforces Round 718C (矩阵线段树)
- cf/Codeforces Round #373 div1-C/div2-E Sasha and Array 线段树 + 维护矩阵快速幂
- Codeforces Round #343 (Div. 2) D. Babaei and Birthday Cake 线段树维护dp
- Educational Codeforces Round 37 (Rated for Div. 2)-F-SUM and REPLACE(线段树)
- Codeforces Round #275 Div.1 B Interesting Array --线段树
- Educational Codeforces Round 37 (Rated for Div. 2) F. SUM and REPLACE(线段树,区间更新)
- Educational Codeforces Round 23 F. MEX Queries(线段树区间设值)
- Codeforces Round #43 D题Parking Lot (线段树)解题报告
- Codeforces Round 232 Div 1 C On Changing Tree 树状数组 或 线段树
- Codeforces Round #222 (Div. 1) D. Developing Game 线段树有效区间合并
- Educational Codeforces Round 23 F. MEX Queries(离散化+线段树)
- 【Educational Codeforces Round 6E】【线段树 dfs序】New Year Tree 子树颜色修改子树颜色数
- Shifts-Codeforces Round #142 (Div. 2) C (矩阵)
- Educational Codeforces Round 20 G. Periodic RMQ Problem(线段树+主席树)
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
- Codeforces Round #413 C. Fountains(RMQ线段树)
- Codeforces Round #197 (Div. 2)(d线段树)
- Codeforces Round #458 D. Bash and a Tough Math Puzzle(线段树)
- Codeforces Round #343 (Div. 2) 629D. Babaei and Birthday Cake 线段树
- Codeforces Round #442E-dfs序&线段树的区间更新区间查询-Danil and a Part-time Job