codeforces 718C - Sasha and Array
2016-10-01 10:33
330 查看
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
, 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.
题意:给出n个数字,有m个询问,每个询问:
1: 区间(l,r)内,每个数,增加c
2:区间(l,r)内,查询对应的f(x)之和(f()指的是斐波那契数列)
分析:我们可以用过,矩阵快速幂来快速求出对应的斐波那契数,那么我们可以用线段树维护一个区间乘法矩阵。
代码:
#include <algorithm>
#include <cstring>
#include <string.h>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cstdio>
#include <cmath>
#define N 60005
#define INF 0x3ffffff
using namespace std;
const int matX = 2;
typedef __int64 LL;
const int mod = 1e9+7;
const int maxn = 1e5 +10;
LL ans;
struct Matrix
{
int n, m;
LL a[matX][matX];
Matrix() {}
void init(int _n, int _m)
{
n = _n;
m = _m;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++) a[i][j] = 0;
}
}
Matrix operator + (const Matrix &B)const
{
Matrix C;
C.init(n,m);
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
C.a[i][j]=(a[i][j]+B.a[i][j])%mod;
return C;
}
Matrix operator*(const Matrix &P)const
{
Matrix ret;
ret.init(n,m);
for(int i = 0; i < n; i++)
{
for(int k = 0; k < m; k++)
{
if(a[i][k])
{
for(int j = 0; j < P.m; j++)
{
ret.a[i][j] = ((LL)a[i][k] * P.a[k][j] + ret.a[i][j]) % mod;
}
}
}
}
return ret;
}
Matrix operator^(const LL &P)const
{
LL num = P;
Matrix ret, tmp = *this;
ret.init(n,m);
for(int i = 0; i < n; i++) ret.a[i][i] = 1;
while(num)
{
if(num & 1) ret = ret * tmp;
tmp = tmp * tmp;
num >>= 1;
}
return ret;
}
};
struct node
{
int l,r;
Matrix add,sum;
} s[maxn*4];
LL h[maxn];
void pushup(int x)
{
int tmp=x*2;
s[x].sum=s[tmp].sum+s[tmp+1].sum;
}
bool judge(Matrix x)
{
for(int i=0;i<x.n;i++)
for(int j=0;j<x.m;j++)
if(x.a[i][j]) return false;
return true;
}
void pushdown(int x)
{
if(judge(s[x].add)) return;
int tmp=x*2;
if(judge(s[tmp].add)) s[tmp].add=s[x].add;//这里原本是加法的,但是我们要维护乘法,所以要变成乘
else //同时还要考虑原矩阵是0矩阵的情况
s[tmp].add=s[tmp].add*s[x].add;
if(judge(s[tmp+1].add)) s[tmp+1].add=s[x].add;
else
s[tmp+1].add=s[tmp+1].add*s[x].add;
s[tmp].sum=s[x].add*s[tmp].sum;
s[tmp+1].sum=s[x].add*s[tmp+1].sum;
s[x].add.init(2,2);
}
void buildtree(int l,int r,int x)
{
s[x].l=l;
s[x].r=r;
s[x].add.init(2,2);
s[x].sum.init(2,2);
if(r==l)
{
Matrix as;
as.init(2,2);
as.a[0][0]=1;
as.a[0][1]=1;
as.a[1][0]=1;
s[x].sum.a[0][0]=1;
s[x].sum=(as^(h[l]-1))*s[x].sum;
return;
}
int tmp=x<<1;
int mid=(l+r)/2;
buildtree(l,mid,tmp);
buildtree(mid+1,r,tmp+1);
pushup(x);
}
void query(int l,int r,int x)
{
if(s[x].l==l&&s[x].r==r)
{
ans+=s[x].sum.a[0][0];
ans%=mod;
return ;
}
pushdown(x);
int mid=(s[x].l+s[x].r)/2;
if(r<=mid) query(l,r,2*x);
else if(l>=mid+1) query(l,r,2*x+1);
else
{
query(l,mid,2*x);
query(mid+1,r,2*x+1);
}
}
void updata(int l,int r,Matrix c,int x)
{
if(r<s[x].l||l>s[x].r) return;
if(l<=s[x].l&&r>=s[x].r)
{
if(judge(s[x].add)) s[x].add=c;
else
s[x].add=c*s[x].add;
s[x].sum=c*s[x].sum;
return;
}
pushdown(x);
int tmp=x*2;
updata(l,r,c,tmp);
updata(l,r,c,tmp+1);
pushup(x);
}
int main()
{
int n,m,l,r,q;
LL c;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
scanf("%I64d",&h[i]);
buildtree(1,n,1);
while(m--)
{
scanf("%d",&q);
if(q==1)
{
scanf("%d%d%I64d",&l,&r,&c);
Matrix as;
as.init(2,2);
as.a[0][0]=1;
as.a[0][1]=1;
as.a[1][0]=1;
as=as^c;
updata(l,r,as,1);//这里要把数字预处理矩阵带入,不然会被卡常
}
else
{
scanf("%d%d",&l,&r);
ans=0;
query(l,r,1);
printf("%I64d\n",(ans%mod+mod)%mod);
}
}
return 0;
}
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
, 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.
题意:给出n个数字,有m个询问,每个询问:
1: 区间(l,r)内,每个数,增加c
2:区间(l,r)内,查询对应的f(x)之和(f()指的是斐波那契数列)
分析:我们可以用过,矩阵快速幂来快速求出对应的斐波那契数,那么我们可以用线段树维护一个区间乘法矩阵。
代码:
#include <algorithm>
#include <cstring>
#include <string.h>
#include <iostream>
#include <list>
#include <map>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>
#include <cstdio>
#include <cmath>
#define N 60005
#define INF 0x3ffffff
using namespace std;
const int matX = 2;
typedef __int64 LL;
const int mod = 1e9+7;
const int maxn = 1e5 +10;
LL ans;
struct Matrix
{
int n, m;
LL a[matX][matX];
Matrix() {}
void init(int _n, int _m)
{
n = _n;
m = _m;
for(int i = 0; i < n; i++)
{
for(int j = 0; j < n; j++) a[i][j] = 0;
}
}
Matrix operator + (const Matrix &B)const
{
Matrix C;
C.init(n,m);
for(int i=0; i<n; i++)
for(int j=0; j<m; j++)
C.a[i][j]=(a[i][j]+B.a[i][j])%mod;
return C;
}
Matrix operator*(const Matrix &P)const
{
Matrix ret;
ret.init(n,m);
for(int i = 0; i < n; i++)
{
for(int k = 0; k < m; k++)
{
if(a[i][k])
{
for(int j = 0; j < P.m; j++)
{
ret.a[i][j] = ((LL)a[i][k] * P.a[k][j] + ret.a[i][j]) % mod;
}
}
}
}
return ret;
}
Matrix operator^(const LL &P)const
{
LL num = P;
Matrix ret, tmp = *this;
ret.init(n,m);
for(int i = 0; i < n; i++) ret.a[i][i] = 1;
while(num)
{
if(num & 1) ret = ret * tmp;
tmp = tmp * tmp;
num >>= 1;
}
return ret;
}
};
struct node
{
int l,r;
Matrix add,sum;
} s[maxn*4];
LL h[maxn];
void pushup(int x)
{
int tmp=x*2;
s[x].sum=s[tmp].sum+s[tmp+1].sum;
}
bool judge(Matrix x)
{
for(int i=0;i<x.n;i++)
for(int j=0;j<x.m;j++)
if(x.a[i][j]) return false;
return true;
}
void pushdown(int x)
{
if(judge(s[x].add)) return;
int tmp=x*2;
if(judge(s[tmp].add)) s[tmp].add=s[x].add;//这里原本是加法的,但是我们要维护乘法,所以要变成乘
else //同时还要考虑原矩阵是0矩阵的情况
s[tmp].add=s[tmp].add*s[x].add;
if(judge(s[tmp+1].add)) s[tmp+1].add=s[x].add;
else
s[tmp+1].add=s[tmp+1].add*s[x].add;
s[tmp].sum=s[x].add*s[tmp].sum;
s[tmp+1].sum=s[x].add*s[tmp+1].sum;
s[x].add.init(2,2);
}
void buildtree(int l,int r,int x)
{
s[x].l=l;
s[x].r=r;
s[x].add.init(2,2);
s[x].sum.init(2,2);
if(r==l)
{
Matrix as;
as.init(2,2);
as.a[0][0]=1;
as.a[0][1]=1;
as.a[1][0]=1;
s[x].sum.a[0][0]=1;
s[x].sum=(as^(h[l]-1))*s[x].sum;
return;
}
int tmp=x<<1;
int mid=(l+r)/2;
buildtree(l,mid,tmp);
buildtree(mid+1,r,tmp+1);
pushup(x);
}
void query(int l,int r,int x)
{
if(s[x].l==l&&s[x].r==r)
{
ans+=s[x].sum.a[0][0];
ans%=mod;
return ;
}
pushdown(x);
int mid=(s[x].l+s[x].r)/2;
if(r<=mid) query(l,r,2*x);
else if(l>=mid+1) query(l,r,2*x+1);
else
{
query(l,mid,2*x);
query(mid+1,r,2*x+1);
}
}
void updata(int l,int r,Matrix c,int x)
{
if(r<s[x].l||l>s[x].r) return;
if(l<=s[x].l&&r>=s[x].r)
{
if(judge(s[x].add)) s[x].add=c;
else
s[x].add=c*s[x].add;
s[x].sum=c*s[x].sum;
return;
}
pushdown(x);
int tmp=x*2;
updata(l,r,c,tmp);
updata(l,r,c,tmp+1);
pushup(x);
}
int main()
{
int n,m,l,r,q;
LL c;
scanf("%d%d",&n,&m);
for(int i=1; i<=n; i++)
scanf("%I64d",&h[i]);
buildtree(1,n,1);
while(m--)
{
scanf("%d",&q);
if(q==1)
{
scanf("%d%d%I64d",&l,&r,&c);
Matrix as;
as.init(2,2);
as.a[0][0]=1;
as.a[0][1]=1;
as.a[1][0]=1;
as=as^c;
updata(l,r,as,1);//这里要把数字预处理矩阵带入,不然会被卡常
}
else
{
scanf("%d%d",&l,&r);
ans=0;
query(l,r,1);
printf("%I64d\n",(ans%mod+mod)%mod);
}
}
return 0;
}
相关文章推荐
- Codeforces 718C Sasha and Array(线段树维护矩阵)
- CodeForces 718C Sasha and Array
- codeforces 718c Sasha and Array
- [题解]codeforces 718c Sasha and Array
- codeforces 718 C.Sasha and Array
- Codeforces 718C C. Sasha and Array
- Codeforces 719 E. Sasha and Array (线段树+矩阵运算)
- codeforces 373div1 Sasha and Array 矩阵+线段树
- [Codeforces 719 E. Sasha and Array] 矩阵快速幂+线段树
- 【Codeforces 718C&719E】Sasha and Array【线段树成段更新+矩阵快速幂】
- 【CodeForces 719E】【线段树+矩阵快速幂】 Sasha and Array
- 【codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry
- 【codeforces 719E】Sasha and Array
- CodeForces 220B(B. Little Elephant and Array)
- codeforces-302A-Eugeny and Array
- Codeforces 295A Greg and Array 线段树区间累加,单点询问
- Codeforces 221 D. Little Elephant and Array
- codeforces 832A ——Sasha and Sticks
- CodeForces 360A - Levko and Array Recovery (模拟)
- CodeForces 498C Array and Operations(最大流)