zoj 3772 Calculate the Function 线段树+矩阵
来源:互联网 发布:希尔瓦娜斯 知乎 编辑:程序博客网 时间:2024/04/29 05:03
Time Limit: 2 Seconds Memory Limit: 65536 KB
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.
Fi(Li) = ALi
Fi(Li + 1) = A(Li + 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
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.
Fi(Li) = ALi
Fi(Li + 1) = A(Li + 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 <stdlib.h>#include <iostream>#define ls(x) ((x) << 1)#define rs(x) ((x) << 1 | 1)using namespace std;typedef long long ll;struct Mat { int m[2][2]; void setE() { m[0][0] = m[1][1] = 1; m[0][1] = m[1][0] = 0; } void setZ() { m[0][0] = m[0][1] = m[1][0] = m[1][1] = 0; }};const int maxn = 100000 + 10;const int mod = 1000000007;int a[maxn];Mat mat[maxn * 8];Mat mul(Mat a, Mat b) { Mat ret; ret.setZ(); for (int i = 0; i < 2; i++) for (int j = 0; j < 2; j++) for (int k = 0; k < 2; k++) ret.m[i][j] = ((ll)a.m[i][k] * b.m[k][j] + ret.m[i][j]) % mod; return ret;}void Build(int cur, int l, int r) { int m; if(l == r) { mat[cur].m[0][0] = 0; mat[cur].m[0][1] = 1; mat[cur].m[1][0] = a[l]; mat[cur].m[1][1] = 1; } else { m = l + ((r - l) >> 1); Build(ls(cur), l, m); Build(rs(cur), m + 1, r); mat[cur] = mul(mat[rs(cur)], mat[ls(cur)]); }}Mat Query(int cur, int l, int r, int ql, int qr) { int m; Mat ret; if (ql <= l && r <= qr) return mat[cur]; else { m = l + ((r - l) >> 1); ret.setE(); if (ql <= m) ret = mul(ret, Query(ls(cur), l, m, ql, qr)); if (m < qr) ret = mul(Query(rs(cur), m + 1, r, ql, qr), ret); } return ret;}int main() {// freopen("data.in", "r", stdin); int tcas, n, m, l, r; Mat L, R, ret; scanf("%d", &tcas); while(tcas--) { scanf("%d%d", &n, &m); for (int i = 1; i <= n; i++) scanf("%d", &a[i]); Build(1, 1, n); while (m--) { scanf("%d%d", &l, &r); R.m[0][0] = a[l]; R.m[0][1] = 0; R.m[1][0] = a[l+1]; R.m[1][1] = 0; if (r - l <= 1) printf("%d\n", a[r]); else { L = Query(1, 1, n, l + 2, r); ret = mul(L, R); printf("%d\n", ret.m[1][0]); } } } return 0;}
0 0
- zoj-3772-Calculate the Function-线段树+矩阵
- ZOJ Problem Set - 3772 Calculate the Function 矩阵+线段树
- zoj 3772 Calculate the Function 线段树+矩阵
- ZOJ 3772 Calculate the Function(线段树 + 矩阵)
- ZOJ 3772 Calculate the Function (线段树 + 矩阵)
- [矩阵+线段树] zoj 3772 Calculate the Function
- zoj 3772 Calculate the Function 矩阵相乘+ 线段树查询
- zoj 3772 Calculate the Function(矩阵+线段树)
- zoj 3772 Calculate the Function(线段树+矩阵乘法)
- ZOJ 3772 Calculate the Function(线段树+矩阵)
- ZOJ 3772 Calculate the Function(矩阵线段树)
- 线段树 + 矩阵 --- ZOJ 3772 Calculate the Function
- ZOJ 3772 Calculate the Function [线段树+矩阵乘法]【思维?】
- ZOJ-3772 Calculate the Function(线段树,矩阵乘法)
- ZOJ 3772 Calculate the Function( 线段树 )
- ZOJ 3772Calculate the Function(线段树)
- ZOJ 3722 Calculate the Function(线段树+矩阵)
- zoj 3772 Calculate the Function(矩阵乘法)
- matlab调用c++涉及到的mex文件
- SQL查询表中最后5条记录
- 耦合和内聚
- python 中文乱码 问题深入分析
- odex 转换为 dex
- zoj 3772 Calculate the Function 线段树+矩阵
- iWatch将至!苹果WWDC14将于六月二号开场
- java设计模式之模板方法模式Template method
- bitset类学习(补充)
- wifi 基础知识
- tomcat相关
- 关于Mongodb
- Android 4.3中的视觉边界布局(Optical bounds layout)
- 浅谈技术工程师的进步