zoj3772Calculate the Function(矩阵+线段树)
表达式类似于斐波那契 但是多了一个变量 不能用快速幂来解 不过可以用线段树进行维护
对于每一个点够一个2*2的矩阵
1 a[i]
1 0 这个矩阵应该不陌生 类似于构造斐波那契的那个数列 还是比较容易能想到的
然后就用线段树进行维护 注意矩阵不满足交换律 在乘的时候要倒序。
- #include <iostream>
- #include<cstdio>
- #include<cstring>
- #include<algorithm>
- #include<stdlib.h>
- #include<vector>
- #include<cmath>
- #include<queue>
- #include<set>
- using namespace std;
- #define N 100010
- #define LL long long
- #define INF 0xfffffff
- #define mod 1000000007
- const double eps = 1e-;
- const double pi = acos(-1.0);
- const double inf = ~0u>>;
- struct Mat
- {
- LL c[][];
- }s[N<<];
- LL o[N];
- Mat operator * (Mat a,Mat b)
- {
- Mat c;
- memset(c.c,,sizeof(c.c));
- int i,j,k;
- for(k = ; k < ; k++)
- {
- for(i = ; i < ;i++)
- {
- if(a.c[i][k]==) continue;//优化
- for(j = ;j < ;j++)
- {
- if(b.c[k][j]==) continue;//优化
- c.c[i][j] = (c.c[i][j]+a.c[i][k]*b.c[k][j])%mod;
- }
- }
- }
- return c;
- }
- void up(int w)
- {
- s[w] = s[w<<|]*s[w<<];
- }
- void build(int l,int r,int w)
- {
- if(l==r)
- {
- s[w].c[][] = s[w].c[][] = ;
- s[w].c[][] = o[l];
- s[w].c[][] = ;
- return ;
- }
- int m = (l+r)>>;
- build(l,m,w<<);
- build(m+,r,w<<|);
- up(w);
- }
- Mat getsum(int a,int b,int l,int r,int w)
- {
- if(a<=l&&b>=r)
- {
- return s[w];
- }
- int m = (l+r)>>,i,j;
- Mat c;
- for(i= ;i < ; i++)
- {
- for(j = ;j < ; j++)
- {
- if(i==j)
- c.c[i][j] = ;
- else
- c.c[i][j] = ;
- }
- }
- if(b>m)
- c=c*getsum(a,b,m+,r,w<<|);
- if(a<=m)
- c=c*getsum(a,b,l,m,w<<);
- return c;
- }
- int main()
- {
- int i,n,m,t;
- cin>>t;
- while(t--)
- {
- scanf("%d%d",&n,&m);
- for(i = ;i <= n; i++)
- scanf("%lld",&o[i]);
- build(,n,);
- while(m--)
- {
- int a,b;
- scanf("%d%d",&a,&b);
- if(b-a<=)
- {
- printf("%lld\n",o[b]%mod);
- continue;
- }
- Mat x;
- x.c[][] = o[a+];
- x.c[][] = o[a];
- x.c[][] = ;x.c[][] = ;
- Mat y = getsum(a+,b,,n,);
- x = y*x;
- printf("%lld\n",(x.c[][])%mod);
- }
- }
- return ;
- }
zoj3772Calculate the Function(矩阵+线段树)的更多相关文章
- 2014 Super Training #7 E Calculate the Function --矩阵+线段树
原题:ZOJ 3772 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772 这题算是长见识了,还从没坐过矩阵+线段树的题 ...
- CF 316E3 Summer Homework(斐波那契矩阵+线段树)
题目链接:http://codeforces.com/problemset/problem/316/E3 题意:一个数列A三种操作:(1)1 x y将x位置的数字修改为y:(2)2 x y求[x,y] ...
- 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)
题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...
- 2016 ACM-ICPC Asia Regional Dalian Online HDU 5875 Function(线段树)
题意 求区间l~r的a[l]%a[l+1]%--%a[r]的值 思路 因为取模的变化是很快的,所以线段树查找区间内第一个小于等于a[l]的数的位置,更新ans后继续查找即可. 注意查询满足某种条件的位 ...
- [原]zoj3772--【水题】线段树区间查询+矩阵乘法
思路来源:http://blog.csdn.net/u013654696/article/details/23037407#comments [做浙大校赛的时候没有看这道题,事后做的.思路不是自己的, ...
- 线段树 + 矩阵 --- ZOJ 3772 Calculate the Function
Calculate the Function Problem's Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCod ...
- ZOJ3772 - Calculate the Function(线段树+矩阵)
题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...
- Z0J 3772 Calculate the Function 线段树+矩阵
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这种题目居然没想到,一开始往矩阵快速幂想去了,因为之前跪了太多矩阵快速幂 ...
- CF719E(线段树+矩阵快速幂)
题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...
随机推荐
- Python 中的字节与字节数组
Python 中的字节与字节数组 - Python - 伯乐在线 http://python.jobbole.com/84839/
- 使用POCO发送HTTP(S)请求
POCO GitHub地址https://github.com/pocoproject/poco http_example.cpp #include <iostream> #include ...
- WIFI的通信知识整理
这两天在解决wifi芯片的一个底层问题,看了很多资料,下面做一个简要记录: 1.信号调制的基本原理 链接:http://wenku.baidu.com/link?url=3K6Z5fBIN20lPzB ...
- ios很好的开源库
Tim9Liu9/TimLiu-iOS 自己总结的iOS.mac开源项目及库,持续更新.. 目录 UI 下拉刷新 模糊效果 AutoLayout 富文本 图表 表相关与Tabbar 隐藏与显示 HUD ...
- spring mvc带参数重定向
http://blog.csdn.net/jackpk/article/details/19121777/ https://isudox.com/2017/02/16/spring-mvc-redir ...
- hdu 1963 Investment 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1963 题目意思:有 本金 money,还有一些股票的种类,第 i 种股票买入需要 value[i] 这 ...
- 书写优雅的shell脚本(六)- shell中的命令组合(&&、||、())
shell 在执行某个命令的时候,会返回一个返回值,该返回值保存在 shell 变量 $? 中.当 $? == 0 时,表示执行成功:当 $? == 1 时,表示执行失败. 有时候,下一条命令依赖前 ...
- 【前端】CentOS 7 系列教程之四: 配置 git 服务器自动部署
转载请注明出处:http://www.cnblogs.com/shamoyuu/p/linux_4.html 安装pm2守护进程,备用 npm install -g pm2 创建/srv/www文件夹 ...
- Java Container ***
Java Container ArrayList 和Vector是采用数组方式存储数据,此数组元素数大于实际存储的数据以便增加和插入元素,都允许直接序号索引元素,但是插入数据要设计到数组元素移动等内存 ...
- vue项目中的路径别名
每次写引入组件的路径,如果路径嵌套比较深,那么会比较麻烦,我们可以在webpack.base.conf.js,中设置路径的别名,默认webpack设置src的别名为@ 建议配置src下一级目录的别名, ...