F - Calculate the Function ZOJ - 3772

You are given a list of numbers A1 A2 .. AN and M queries. For the i-th query:

  • The query has two parameters Li and Ri.
  • The query will define a function Fi(x) on the domain [Li, Ri]Z.
  • F i ( L i ) = A L i
  • F i (L i + 1) = A (L i + 1)
  • for all x >= Li + 2, Fi(x) = Fi(x - 1) + Fi(x - 2) × Ax

You task is to calculate Fi(Ri) for each query. Because the answer can be very large, you should output the remainder of the answer divided by 1000000007.


Input

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

The first line contains two integers N, M (1 <= N, M <= 100000). The second line contains N integers A1 A2 .. AN (1 <= Ai <= 1000000000).

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

Output

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

Sample Input
1
4 7
1 2 3 4
1 1
1 2
1 3
1 4
2 4
3 4
4 4
Sample Output
1
2
5
13
11
4
4
#include<stdio.h>
#include<string.h>
#include<vector>
#include<iostream>
#include<algorithm>

#define ls(x) (x<<1)
#define rs(x) (x<<1|1)
using namespace std;
typedef long long ll;

const int mod=1e9+7;
const int maxn=1e5+10;
int n,m;
/*
I forgot the beautiful structure of the line segment tree. This question was a pity at the time. It was within the scope of my ability, but I didn't do it.
At that time, I thought about the line segment tree, but I quickly jumped over it, mainly because of the complexity, I don't know what I thought at the time
It feels like it will exceed, it is a log(n) complexity, there is no problem
*/

struct Martix{
	ll mar[3][3];
	Martix operator *(const Martix &b){
		Martix c;
		for(int i=1;i<=2;i++){
			for(int j=1;j<=2;j++){
				c.mar[i][j]=0;
				for(int k=1;k<=2;k++){
					c.mar[i][j]=(c.mar[i][j]+mar[i][k]*b.mar[k][j]%mod)%mod;
				}
			}
		}
		return c;
	}
};
struct node{
	Martix m;
	int l, r;
}tr[maxn*4];

ll val[maxn];

void build(int l,int r,int o){
	tr[o].l=l,tr[o].r=r;
	if(l==r){
		tr [o] .m.mar [1] [1] = 1;
		tr [o] .m.mar [2] [1] = 1;
		tr [o] .m.mar [1] [2] = val [n-l + 1];
		tr [o] .m.mar [2] [2] = 0;
		return ;
	}
	int mid=(l+r)>>1;
	build(l,mid,ls(o));
	build(mid+1,r,rs(o));
	tr[o].m=tr[ls(o)].m*tr[rs(o)].m;
}

Martix query (int l, int r, int o) {
	if(tr[o].l>=l&&tr[o].r<=r)
		return tr[o].m;
	int mid=(tr[o].l+tr[o].r)>>1;
	if(r<=mid)
		return query(l,r,ls(o));
	else if(l>mid)
		return query(l,r,rs(o));
	else
		return query(l,mid,ls(o))*query(mid+1,r,rs(o));
}

intmain()
{
    int T;
    scanf("%d",&T);
    while(T--){
    	scanf("%d %d",&n,&m);
    	for(int i=1;i<=n;i++){
    		scanf("%lld",&val[i]);
    	}
    	build(1,n,1);
    	for(int i=1;i<=m;i++){
    		int l, r;
    		scanf("%d %d",&l,&r);
			if(r-l<2){
				printf("%lld\n",val[r]);
			}
			else{
				Martix m = query (n-r + 1, nl-1,1);
				ll ans=(val[l+1]*m.mar[1][1]+val[l]*m.mar[1][2]%mod+mod)%mod;
				printf("%lld\n",ans);
			}
    	}
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325823363&siteId=291194637
ZOJ
ZOJ