Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types:
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?
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.
For each query of the second type print the answer modulo 109 + 7.
5 4 1 1 2 1 1 2 1 5 1 2 4 2 2 2 4 2 1 5
5 7 9
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个数,两个操作,1是区间[l,r]增加x,2是查询区间[l,r]内fib(a[i])的和
题解:
学习到了用矩阵处理斐波那契数的方法。
官网题解:
Let's recall how we can quickly find n-th Fibonacci number. To do this we need to find a matrix product .
In order to solve our problem let's create the following segments tree: in each leaf which corresponds to the element i we will store a vector and in all other nodes we will store the sums of all the vectors that correspond to a given segment.
Now, to perform the first request we should multiply all the vectors in a segment [l..r] by and to get an answer to the second request we have to find a sum in a segment [l..r].
Time Complexity: .
CF官方题解下面有个评论说的很清楚,大家可以看一下。
我写这一题的时候一开始对区间中的lazy标记里面的tag记录的是还有矩阵多少次幂没有在子区间中更新,写了好久终于写出来了,后来T了,然后我随便点开一个代码,发现其他人的tag是一个矩阵,于是我将tag改成矩阵就过了,可能是先前版本pushdown时重复计算了许多次幂所以超时了。
先前T了的版本:
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int MAXN=1e5+100;
struct Mat {
ll mat[2][2];
};
struct node
{
int l,r,lazy;
ll tag;
Mat sum;
}seg[MAXN*4];
Mat operator * (Mat a, Mat b) {
Mat c;
memset(c.mat, 0, sizeof(c.mat));
int i, j, k;
for(k = 0; k < 2; ++k) {
for(i = 0; i < 2; ++i) {
for(j = 0; j < 2; ++j) {
c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j])%mod;
}
}
}
return c;
}
Mat operator + (Mat a,Mat b)
{
Mat c;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
c.mat[i][j]=(a.mat[i][j]+b.mat[i][j])%mod;
return c;
}
Mat operator ^ (Mat a, ll k) {
Mat c;
int i, j;
for(i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j)
c.mat[i][j] = (i == j); //初始化为单位矩阵
for(; k; k >>= 1) {
if(k&1) c = c*a;
a = a*a;
}
return c;
}
ll a[MAXN];
Mat f;
void build(int i,int l,int r)
{
seg[i].l=l,seg[i].r=r,seg[i].lazy=0,seg[i].tag=0;
if(l==r)
{
seg[i].sum=f^(a[l]-1);
return;
}
int m=(l+r)/2;
build(i*2,l,m);
build(i*2+1,m+1,r);
seg[i].sum=seg[i*2].sum+seg[i*2+1].sum;
}
void update(int i,int l,int r,ll v)
{
if(seg[i].l==l&&seg[i].r==r)
{
if(seg[i].lazy)
{
seg[i].tag+=v;
}
else
{
seg[i].lazy=1;
seg[i].tag=v;
}
seg[i].sum=seg[i].sum*(f^v);
return;
}
int m=(seg[i].l+seg[i].r)/2;
if(seg[i].lazy)
{
//seg[i].sum=seg[i].sum*(f^(seg[i].tag));
if(seg[i].l!=seg[i].r)
{
update(i*2,seg[i].l,m,seg[i].tag);
update(i*2+1,m+1,seg[i].r,seg[i].tag);
}
seg[i].lazy=0;
seg[i].tag=0;
}
if(r<=m) update(i*2,l,r,v);
else if(l>m) update(i*2+1,l,r,v);
else
{
update(i*2,l,m,v);
update(i*2+1,m+1,r,v);
}
seg[i].sum=seg[i*2].sum+seg[i*2+1].sum;
return;
}
Mat query(int i,int l,int r)
{
int m=(seg[i].l+seg[i].r)/2;
if(seg[i].l==l&&seg[i].r==r)
{
return seg[i].sum;
}
if(seg[i].lazy)
{
if(seg[i].l!=seg[i].r)
{
update(i*2,seg[i].l,m,seg[i].tag);
update(i*2+1,m+1,seg[i].r,seg[i].tag);
}
seg[i].lazy=seg[i].tag=0;
}
if(r<=m) return query(i*2,l,r);
else if(m'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar())
x=(x<<1)+(x<<3)+c-'0';
}
inline void read1(ll &x){
register char c=getchar();
x=0;
for(;c<'0'||c>'9';c=getchar());
for(;c>='0'&&c<='9';c=getchar())
x=(x<<1)+(x<<3)+c-'0';
}
int main()
{
f.mat[0][0]=0;f.mat[0][1]=f.mat[1][0]=f.mat[1][1]=1;
int n,m;
read(n),read(m);
//scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) read1(a[i]);
build(1,1,n);
while(m--)
{
int op,l,r;
read(op),read(l),read(r);
//scanf("%d%d%d",&op,&l,&r);
if(op==1)
{
ll v;
read1(v);
//scanf("%I64d",&v);
update(1,l,r,v);
}
else
{
ll ans;
Mat res=query(1,l,r);
ans=(res.mat[0][0]+res.mat[1][0])%mod;
printf("%I64d\n",ans);
}
}
return 0;
}
#include
#include
#include
#include
#include
using namespace std;
typedef long long ll;
const ll mod=1e9+7;
const int MAXN=1e5+100;
struct Mat {
ll mat[2][2];
};
struct node
{
int l,r,lazy;
Mat tag;
Mat sum;
}seg[MAXN*4];
Mat operator * (Mat a, Mat b) {
Mat c;
memset(c.mat, 0, sizeof(c.mat));
int i, j, k;
for(k = 0; k < 2; ++k) {
for(i = 0; i < 2; ++i) {
for(j = 0; j < 2; ++j) {
c.mat[i][j] = (c.mat[i][j] + a.mat[i][k] * b.mat[k][j])%mod;
}
}
}
return c;
}
Mat operator + (Mat a,Mat b)
{
Mat c;
for(int i=0;i<2;i++)
for(int j=0;j<2;j++)
c.mat[i][j]=(a.mat[i][j]+b.mat[i][j])%mod;
return c;
}
Mat operator ^ (Mat a, ll k) {
Mat c;
int i, j;
for(i = 0; i < 2; ++i)
for(j = 0; j < 2; ++j)
c.mat[i][j] = (i == j); //初始化为单位矩阵
for(; k; k >>= 1) {
if(k&1) c = c*a;
a = a*a;
}
return c;
}
ll a[MAXN];
Mat f,I,tmp;
void build(int i,int l,int r)
{
seg[i].l=l,seg[i].r=r,seg[i].tag=I;
if(l==r)
{
seg[i].sum=f^(a[l]-1); //初始化
return;
}
int m=(l+r)/2;
build(i*2,l,m);
build(i*2+1,m+1,r);
seg[i].sum=seg[i*2].sum+seg[i*2+1].sum;
}
void push_down(int i)
{
if(seg[i].l!=seg[i].r)
{
seg[i*2].tag=seg[i*2].tag*seg[i].tag;seg[i*2].sum=seg[i*2].sum*seg[i].tag; //不要忘记更新sum
seg[i*2+1].tag=seg[i*2+1].tag*seg[i].tag;seg[i*2+1].sum=seg[i*2+1].sum*seg[i].tag;
}
seg[i].tag=I; //重新赋值为单位矩阵
}
void update(int i,int l,int r)
{
if(seg[i].l==l&&seg[i].r==r)
{
seg[i].sum=seg[i].sum*tmp;
seg[i].tag=seg[i].tag*tmp;
return;
}
int m=(seg[i].l+seg[i].r)/2;
push_down(i);
if(r<=m) update(i*2,l,r);
else if(l>m) update(i*2+1,l,r);
else
{
update(i*2,l,m);
update(i*2+1,m+1,r);
}
seg[i].sum=seg[i*2].sum+seg[i*2+1].sum;
return;
}
Mat query(int i,int l,int r)
{
int m=(seg[i].l+seg[i].r)/2;
if(seg[i].l==l&&seg[i].r==r)
{
return seg[i].sum;
}
push_down(i);
if(r<=m) return query(i*2,l,r);
else if(m