Codeforces Round #373 (Div. 1)C(线段树维护矩阵,矩阵快速幂)

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. 1 l r x — increase all integers on the segment from l to r by values x;
  2. 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 modulo109 + 7.

In this problem we define Fibonacci numbers as follows: f(1) = 1f(2) = 1f(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 0001 ≤ 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 tpiliri and may be xi (1 ≤ tpi ≤ 21 ≤ 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 11211.

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 13431.

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 denote 

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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
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<l) return query(i*2+1,l,r);
	else return query(i*2,l,m)+query(i*2+1,m+1,r);
}
inline void read(int &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';
}
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<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<vector>
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<l) return query(i*2+1,l,r);
	else return query(i*2,l,m)+query(i*2+1,m+1,r);
}

int main()
{
	f.mat[0][0]=0;f.mat[0][1]=f.mat[1][0]=f.mat[1][1]=1;
	I.mat[0][0]=I.mat[1][1]=1,I.mat[0][1]=I.mat[1][0]=0;    //I为单位矩阵 
	int n,m;
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++) scanf("%I64d",&a[i]);
	build(1,1,n);
	while(m--)
	{
		int op,l,r;
		scanf("%d%d%d",&op,&l,&r);
		if(op==1)
		{
			ll v;
			scanf("%I64d",&v);
			tmp=f^v;
			update(1,l,r);
		}
		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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值