线段树 + 矩阵 --- ZOJ 3772 Calculate the Function
Calculate the Function
Problem's Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772
Mean:
略
analyse:
简单的线段树维护矩阵。
矩阵乘法的结合律(a * b * c == a * (b * c)),注意矩阵乘法不满足分配率(a *b != b * a)。
令 M[x] = [1 A[x]]
[1 0 ] ,
那么有 [ F[R] ] = M[R] * M[R-1] * ... * M[L+2] * [F[L+1]]
[F[R-1]] [ F[L] ]
线段树节点维护上边等式右边前n - 1项的乘值(假设等式右边有n项)。每次询问O(log(n))。
Time complexity: O(n*logn)
Source code:
/* * this code is made by crazyacking * Verdict: Accepted * Submission Date: 2015-05-25-20.57 * Time: 0MS * Memory: 137KB */ #include <queue> #include <cstdio> #include <set> #include <string> #include <stack> #include <cmath> #include <climits> #include <map> #include <cstdlib> #include <iostream> #include <vector> #include <algorithm> #include <cstring> #define LL long long #define ULL unsigned long long using namespace std;const int MAXN = 100100; const int MOD = 1000000007; struct Mat {long long m[2][2];Mat(){memset(m, 0, sizeof(m));}Mat operator * (const Mat &b){Mat temp;for(int i = 0; i < 2; i++)for(int j = 0; j < 2; j++)for(int k = 0; k < 2; k++)temp.m[i][j] = ((m[i][k] * b.m[k][j]) + temp.m[i][j]) % MOD;return temp;} };struct Node {int l, r;Mat mat; }; Node node[MAXN << 2]; int a[MAXN];void Build(int rt, int l, int r) {int m;node[rt].l = l;node[rt].r = r;if(l == r){node[rt].mat.m[0][0] = 1;node[rt].mat.m[0][1] = a[l];node[rt].mat.m[1][0] = 1;node[rt].mat.m[1][1] = 0;}else{m = (l + r) >> 1;Build(rt << 1, l, m);Build(rt << 1 | 1, m + 1, r);node[rt].mat = node[rt << 1 | 1].mat * node[rt << 1].mat;//注意顺序 }} Mat Query(int rt, int ql, int qr) {int m;if(ql == node[rt].l && node[rt].r == qr)return node[rt].mat;else{m = (node[rt].l + node[rt].r) >> 1;if(qr <= m)return Query(rt << 1, ql, qr);else if(ql > m)return Query(rt << 1 | 1, ql, qr);elsereturn Query(rt << 1 | 1, m + 1, qr) * Query(rt << 1, ql, m);//注意顺序 } } int T, n, m, ql, qr;int main() {scanf("%d", &T);while(T--){Mat res, f;scanf("%d%d",&n, &m);for(int i = 1; i <= n; i++)scanf("%d", &a[i]);Build(1, 1, n);for(int i = 1; i<= m; i++){scanf("%d%d", &ql, &qr);if(qr - ql >= 2){f.m[0][0] = a[ql + 1];f.m[1][0] = a[ql];res = Query(1, ql + 2, qr) * f;printf("%d\n", res.m[0][0]);}elseprintf("%d\n", a[qr]);}}return 0; }
View Code
转载于:https://www.cnblogs.com/crazyacking/p/4529012.html
线段树 + 矩阵 --- ZOJ 3772 Calculate the Function相关推荐
- 牛客 - 求函数(线段树+区间合并/线段树+矩阵维护)
题目链接:点击查看 题目大意:现在有 n 个函数,每个函数都是诸如 f( x ) = k * x + b 的形式,只是每个函数的 k 和 b 都是相互独立的,现在给出两个操作: 1 pos k b:将 ...
- New Year and Old Subsequence CodeForces - 750E(线段树+矩阵dp)2019南昌icpc网络赛Hello 2019
A string t is called nice if a string "2017" occurs in t as a subsequence but a string &qu ...
- jzoj6293-迷宫【ddp,线段树,矩阵乘法】
正题 题目大意 一个n∗mn*mn∗m的迷宫,不能往左走,有墙,每次修改一个点或询问两个点之间的最短距离. 解题思路 考虑到nnn的值很小,所以我们可以用矩阵转移,然后要求支持修改和查询所以我们考虑d ...
- LOJ2980 THUSC2017大魔法师(线段树+矩阵乘法)
线段树每个节点维护(A,B,C,len)向量,操作即是将其乘上一个矩阵. #include<iostream> #include<cstdio> #include<cma ...
- 2018.10.09 ZYH的斐波那契数列(线段树+矩阵快速幂)
描述 ZYH最近研究数列研究得入迷啦! 现在有一个斐波拉契数列(f[1]=f[2]=1,对于n>2有f[n]=f[n-1]+f[n-2]), 但是斐波拉契数列太简单啦,于是ZYH把它改成了斐波拉 ...
- NOIP模拟题 [线段树][矩阵快速幂]
有一定难度,要深入挖掘问题特性. T1: 题意: 给定一个序列,每次操作把操作位置及其后面比它小的数按顺序排列(整体上仍在原来的位置),求每次操作后的逆序对数. 分析: 每个数对逆序对数都有一个贡献( ...
- 河南省多校联盟二-F 线段树+矩阵
---恢复内容开始--- 1284: SP教数学 时间限制: 2 秒 内存限制: 128 MB 提交: 24 解决: 4 题目描述 输入 输出 对于每组数据的2操作,输出一行对1e9 + 7取模的 ...
- 【HDU - 5875】Function(线段树,区间第一个小于某个数的数 或 RMQ二分)
题干: The shorter, the simpler. With this problem, you should be convinced of this truth. You a ...
- ZOJ 3597 Hit the Target! (线段树扫描线 -- 矩形所能覆盖的最多的点数)
ZOJ 3597 题意是说有n把枪,有m个靶子,每把枪只有一发子弹(也就是说一把枪最多只能打一个靶子), 告诉你第 i 把枪可以打到第j个靶, 现在等概率的出现一个连续的P把枪,在知道这P把枪之后,你 ...
最新文章
- 金蝶应收应付模块流程_金蝶KIS专业版应收应付的系统亮点功能
- 配置C++和C#开发ICE环境
- firefox显示 您的连接不安全 解决办法
- 8.6 多元高斯分布模型-机器学习笔记-斯坦福吴恩达教授
- 生物类似药产业发展策略及项目投资建设报告2021-2027年
- 如何在MacOS上创建第一个iOS Flutter应用
- Oracle结构设计技巧(访问数据库象访问内存一样 快)
- VC++实现对远程计算机屏幕的监视
- 腾讯云blog:孪生网络入门(上) Siamese Net及其损失函数
- 十分钟完成的操作系统
- 记录蚂蚁笔记服务端的搭建过程
- 百度AI人脸识别与检测十:学生人脸识别打卡签到系统之如何查看学生签到信息?
- studio3t到期解决办法
- DHCP工作原理和报文格式
- H5页面唤起微信等app
- 即时通讯开发之开源工程WebRTC原理
- 作为一个Java初学者,怎样从一个新手快速入门?
- Java开源工具库使用之Apache commons-lang3
- 凡走过必留下痕迹|2019年个人总结
- Microsoft Help Viewer help查看器所需的内容文件缺失或者损坏 解决办法
热门文章
- [Luogu1891]疯狂LCM[辗转相减法]
- php array_merge 与 + 区别
- luogu P1762 偶数
- 5.RabbitMQ 客户端控制消息
- C#重写Equals方法步骤
- 在javascript当中发现了一个没有调用者的方法。
- C#控件前缀命名规范
- [转]Using The Entity Framework With WCF
- 高中生活--第1篇--荣辱一身,悲尽兴来
- 兼容IE和FF的JS HTMLEncode和HTMLDecode的完整实例[转]