标签:style blog class code c tar
题目链接:
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235
You are given a list of numbers A1 A2 .. AN and M queries. For the i-th query:
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.
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).
For each test case, output the remainder of the answer divided by 1000000007.
1 4 7 1 2 3 4 1 1 1 2 1 3 1 4 2 4 3 4 4 4
1 2 5 13 11 4 4
题目意思:
给n个数,有m个查询,每次查找区间Li,Ri内的F(Ri),其中F(Li)=save[Li],F[Li+1]=save[Li+1],对于x>=Li+2 F(x)=F(x-1)+F(x-2)*save[x].
1=<n,m<=100000,Ai<=1000000000
解题思路:
矩阵+线段树。
显然
线段树区间维护从右往左的连成后得到的矩阵,建树的时候就更新好,然后来个区间就直接查找就行了,注意矩阵乘法要从右开始,因为最开始的先乘,然后后面的放在开始。
ps:很容易想到矩阵乘法,但基矩阵在变,没想到线段树维护一个矩阵。
代码:
//#include<CSpreadSheet.h> #include<iostream> #include<cmath> #include<cstdio> #include<sstream> #include<cstdlib> #include<string> #include<string.h> #include<cstring> #include<algorithm> #include<vector> #include<map> #include<set> #include<stack> #include<list> #include<queue> #include<ctime> #include<bitset> #include<cmath> #define eps 1e-6 #define INF 0x3f3f3f3f #define PI acos(-1.0) #define ll __int64 #define LL long long #define lson l,m,(rt<<1) #define rson m+1,r,(rt<<1)|1 #define M 1000000007 //#pragma comment(linker, "/STACK:1024000000,1024000000") using namespace std; #define Maxn 110000 struct Mar { LL s[3][3]; int row,col; void init(int a,int b) { row=a; col=b; memset(s,0,sizeof(s)); } }; int n,m; Mar operator *(const Mar &a ,const Mar &b) { Mar c; c.init(a.row,b.col); for(int k=1;k<=a.col;k++) { for(int i=1;i<=a.row;i++) { if(!a.s[i][k]) continue; for(int j=1;j<=b.col;j++) { if(!b.s[k][j]) continue; c.s[i][j]=(c.s[i][j]+a.s[i][k]*b.s[k][j])%M; } } } return c; } struct Node { Mar a; }node[Maxn<<2]; LL save[Maxn]; int cnt; void pushup(int rt) { node[rt].a.init(2,2); node[rt].a=node[rt<<1|1].a*node[rt<<1].a; //右边的矩阵乘以左边的 } void build(int l,int r,int rt) { if(l==r) { scanf("%lld",&save[++cnt]); node[rt].a.init(2,2); node[rt].a.s[1][1]=1; node[rt].a.s[1][2]=save[cnt]; node[rt].a.s[2][1]=1; node[rt].a.s[2][2]=0; return ; } int m=(l+r)>>1; build(lson); build(rson); pushup(rt); //构造区间矩阵 } Mar query(int L,int R,int l,int r,int rt) { if(l>=L&&r<=R) return node[rt].a; int m=(l+r)>>1; Mar res; res.init(2,2); res.s[1][1]=1,res.s[1][2]=0,res.s[2][1]=0,res.s[2][2]=1; if(R>m) //先查找右边的 res=res*(query(L,R,rson)); if(L<=m) res=res*(query(L,R,lson)); return res; } int main() { //freopen("in.txt","r",stdin); //freopen("out.txt","w",stdout); int t; scanf("%d",&t); while(t--) { cnt=0; scanf("%d%d",&n,&m); build(1,n,1); LL a,b; while(m--) { scanf("%lld%lld",&a,&b); if(b==a) { printf("%lld\n",save[a]); continue; } if(b==(a+1)) { printf("%lld\n",save[a+1]); continue; } Mar temp=query(a+2,b,1,n,1); //printf("%lld %lld %lld %lld\n",temp.s[1][1],temp.s[1][2],temp.s[2][1],temp.s[2][2]); printf("%lld\n",(temp.s[1][1]*save[a+1]%M+temp.s[1][2]*save[a]%M)%M); } } return 0; }
[矩阵+线段树] zoj 3772 Calculate the Function,布布扣,bubuko.com
[矩阵+线段树] zoj 3772 Calculate the Function
标签:style blog class code c tar
原文地址:http://blog.csdn.net/cc_again/article/details/26093941