码迷,mamicode.com
首页 > 其他好文 > 详细

wust 1420 We love FibonaQ(矩阵+线段树)

时间:2015-05-30 18:17:16      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:

1420: We love FibonaQ

Time Limit: 3 Sec  Memory Limit: 128 MB   64bit IO Format: %lld
Submitted: 7  Accepted: 3
[Submit][Status][Web Board]

Description

You will find CQ is a very bored man when you know him. One day, he was doing some thing boring as usual. He wrote down N integers A1 A2....Anand then chose an interval [L R] to calculate the sum of FibonaQ.You must have not heard it before because it was a boring function defined by CQ.

As for ALFibonaQ(AL)=AL

As for AL+1FibonaQ(AL+1)=AL+1

For all x >= L+2, FibonaQ(Ax) = ( FibonaQ(Ax-1) + FibonaQ(Ax-2) )*Ax

Now CQ wants to play this boring game with bored you. He will give you two numbers, L and R,then you should tell him the sum of FibonaQ of every number from L to R. Because the answer can be very large, you should output the reminder of the answer divided by 1000000007.

Input

There are multiple test cases. The first line of input is an integer T indicating the number of test cases. For each test case:

The first line contains two integers N, M (1<=N,M<=50000). The second line contains N integers AA2... .A(1<= Ai <=1000).

The next M lines, each line is a query with two integer parameters L, R(1<=L<=R<=N).

Output

For each test case, output the reminder of the answer divided by 1000000007.

Sample Input 技术分享

15 5 1 2 3 4 5
1 1
1 2
1 3
1 4
1 5

Sample Output

1
3
12
56
321

用线段树维护从le到ri的逆序的乘积

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<queue>
#include<stack>
#include<vector>
#include<set>
#include<map>


#define L(x) (x<<1)
#define R(x) (x<<1|1)
#define MID(x,y) ((x+y)>>1)

#define eps 1e-8
typedef long long ll;

#define fre(i,a,b)  for(i = a; i <b; i++)
#define free(i,b,a) for(i = b; i >= a;i--)
#define mem(t, v)   memset ((t) , v, sizeof(t))
#define ssf(n)      scanf("%s", n)
#define sf(n)       scanf("%d", &n)
#define sff(a,b)    scanf("%d %d", &a, &b)
#define sfff(a,b,c) scanf("%d %d %d", &a, &b, &c)
#define pf          printf
#define bug         pf("Hi\n")

using namespace std;
#define mod 1000000007
#define INF 0x3f3f3f3f
#define N 50005

int a[N],n,m;

struct mat{
  ll a[3][3];
  mat(){mem(a,0);}
  mat operator *(mat b)
  {
  	 mat c;
  	 int i,j,k;
  	 for(i=0;i<3;i++)
		for(j=0;j<3;j++)
		  for(k=0;k<3;k++)
		   c.a[i][j]=(c.a[i][j]+a[i][k]*b.a[k][j])%mod;
	 return c;
  }
  mat& operator =(mat b)
  {
  	 int i,j;
  	 for(i=0;i<3;i++)
		for(j=0;j<3;j++)
		 a[i][j]=b.a[i][j];
	 return *this;
  }
  void show()
  {
  	 int i,j;
  	 for(i=0;i<3;i++)
	 {
	 	for(j=0;j<3;j++)
			pf("%d ",a[i][j]);
		pf("\n");
	 }
  }
};

struct stud{
int le,ri;
mat va;
}f[N*4];

void pushup(int pos)
{
	f[pos].va=f[R(pos)].va*f[L(pos)].va;
}

void build(int pos,int le,int ri)
{
	f[pos].le=le;
	f[pos].ri=ri;
	if(le==ri)
	{
		f[pos].va.a[0][0]=f[pos].va.a[1][2]=1;
		f[pos].va.a[0][1]=f[pos].va.a[0][2]=a[le];
		f[pos].va.a[2][1]=f[pos].va.a[2][2]=a[le];
		return ;
	}
    int mid=MID(le,ri);
    build(L(pos),le,mid);
    build(R(pos),mid+1,ri);
    pushup(pos);

}

mat query(int pos,int le,int ri)
{
	if(f[pos].le==le&&f[pos].ri==ri)
		return f[pos].va;

	int mid=MID(f[pos].le,f[pos].ri);
	if(mid>=ri)
		return query(L(pos),le,ri);
	if(mid<le)
		return query(R(pos),le,ri);
	return query(R(pos),mid+1,ri)*query(L(pos),le,mid);
}

int main()
{
	int i,j,t;
	sf(t);
	while(t--)
	{
		scanf("%d%d",&n,&m);
		for(i=1;i<=n;i++)
		   sf(a[i]);

		build(1,1,n);

		int le,ri;
		while(m--)
		{
		   sff(le,ri);
		   if(le==ri)
		   {
		   	 printf("%d\n",a[le]);
		   	 continue;
		   }
           if(le+1==ri)
		   {
		   	printf("%d\n",a[le]+a[ri]);
		     continue;
		   }
           mat te;
           te.a[0][0]=a[le]+a[le+1];
           te.a[1][0]=a[le];
           te.a[2][0]=a[le+1];
           mat ans;
           ans=query(1,le+2,ri);
//           te.show();
//           ans.show();
           ans=ans*te;
           printf("%d\n",ans.a[0][0]);
		}
	}
  return 0;
}


/*

1
5 5
1 2 3 4 5
1 1
1 2
1 3
1 4
1 5

*/







wust 1420 We love FibonaQ(矩阵+线段树)

标签:

原文地址:http://blog.csdn.net/u014737310/article/details/46275831

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!