HDU 6155 DP+矩阵+线段树
来源:互联网 发布:linux 调用内核定时器 编辑:程序博客网 时间:2024/05/24 04:55
题目链接
思路:
设
若第
怎么来理解这个递推式呢?考虑把第
加在
若加在
另外还可以加在空串的后面。
这样就能做到不重不漏地
相应的,当第
然后考虑将线性递推式用矩阵表示:(此处参考博客链接)
当第
当第
故将递推式转化为矩阵的乘法,考虑用线段树维护。
对于翻转操作观察易发现就是先将矩阵的前两行互换,然后将矩阵的前两列互换。
还有一个关于最终答案的细节。
线段树维护的是一个3x3的矩阵,而最终我们只需要其中的两个值,位置分别是最后一行的第一个元素和第二个元素。
可以这么来理解这件事。
假设我们已经通过不断的矩阵连乘得到了一个最终的转移矩阵,设其为:
而我们要得到最终答案,可以参考一开始的矩阵递推式,左乘一个:
因为
故最终表达式为:
故
以上。
代码:
#include<cstdio>#include<cmath>#include<cstring>#include<algorithm>using namespace std;typedef long long ll;#define lson rt<<1#define rson rt<<1|1const int mod = 1e9 + 7;const int A = 1e5 + 10;class Mat{public: int a[3][3]; void init(int val){ memset(a,0,sizeof(a)); if(!val) a[0][0] = a[1][0] = a[2][0] = a[1][1] = a[2][2] = 1; else if(val == 1) a[0][0] = a[0][1] = a[1][1] = a[2][1] = a[2][2] = 1; else a[0][0] = a[1][1] = a[2][2] = 1; //单位矩阵 } Mat operator*(const Mat& rhs) const{ Mat res; for(int i=0 ;i<3 ;i++) for(int j=0 ;j<3 ;j++){ res.a[i][j] = 0; for(int k=0 ;k<3 ;k++) res.a[i][j] = (res.a[i][j] + 1LL*a[i][k]*rhs.a[k][j])%mod; } return res; } void rev(){ //矩阵翻转 for(int i=0 ;i<3 ;i++) swap(a[i][0],a[i][1]); for(int i=0 ;i<3 ;i++) swap(a[0][i],a[1][i]); }};class Seg_Tree{public: int l,r,mark; Mat mat;}Tree[A<<2];char s[A];inline void push_up(int rt){Tree[rt].mat = Tree[lson].mat*Tree[rson].mat;}inline void push_down(int rt){ if(Tree[rt].mark){ Tree[rt].mark = 0; Tree[lson].mark ^= 1;Tree[lson].mat.rev(); Tree[rson].mark ^= 1;Tree[rson].mat.rev(); }}void build_Tree(int rt,int l,int r){ Tree[rt].l = l,Tree[rt].r = r; Tree[rt].mark = 0; if(l == r){Tree[rt].mat.init(s[l]-'0');return;} int mid = (l+r)>>1; build_Tree(lson,l,mid);build_Tree(rson,mid+1,r); push_up(rt);}void update(int rt,int st,int ed){ int l = Tree[rt].l,r = Tree[rt].r; if(st<=l && r<=ed){ Tree[rt].mat.rev(); Tree[rt].mark ^= 1; return; } push_down(rt); int mid = (l+r)>>1; if(st<=mid) update(lson,st,ed); if(ed> mid) update(rson,st,ed); push_up(rt);}Mat query(int rt,int st,int ed){ int l = Tree[rt].l,r = Tree[rt].r; if(st<=l && r<=ed) return Tree[rt].mat; push_down(rt); Mat res;res.init(2); int mid = (l+r)>>1; if(st<=mid) res = query(lson,st,ed)*res; if(ed> mid) res = res*query(rson,st,ed); return res;}int main(){ int T;scanf("%d",&T); while(T--){ int n,q; scanf("%d%d",&n,&q); scanf("%s",s+1); build_Tree(1,1,n); while(q--){ int op,l,r;scanf("%d%d%d",&op,&l,&r); if(op == 1) update(1,l,r); else{ Mat ans = query(1,l,r); printf("%d\n",(ans.a[2][0]+ans.a[2][1])%mod); } } } return 0;}
阅读全文
0 0
- HDU 6155 DP+矩阵+线段树
- HDU 6155 dp+矩阵+线段树
- hdu 6155(线段树+dp+矩阵)
- HDU 6155 Subsequence Count dp + 矩阵+ 线段树
- Hdu 6155 Subsequence Count 矩阵计算DP+线段树维护
- Hdu 6155 线段树维护转移矩阵
- HDU 6155 Subsequence Count(线段树 dp)
- HDU 3607 线段树+DP
- HDU 3698 DP+线段树
- HDU 3564 线段树+DP
- hdu 4521(dp+线段树)
- hdu 3016(线段树+dp)
- hdu 3016 dp+线段树
- HDU 6155 Subsequence Count [线段树维护矩阵]
- hdu 6155 线段树加矩阵快速幂
- hdu 5068 线段树维护矩阵乘积
- HDU上的专题训练(背包问题-线段树+树状数组+DP优化+网络流+字符匹配+最短路+矩阵
- HDU 3016 线段树+简单dp
- popwindow的布局
- mysql函数之六:mysql插入数据后返回自增ID的方法,last_insert_id(),selectkey
- 如何成为架构师系列:框架演进3
- svn is not a working copy directory 的解决方法
- Android------Binder 如何在binder架构下写service
- HDU 6155 DP+矩阵+线段树
- eclipse maven plugin 插件 安装 和 配置
- A Policy Update Strategy in Model-free Policy Search: Policy Gradient
- JAVA实现DES加密实现详解
- CocosCreator资源管理(个人经验总结)
- python高级特性(4):生成器
- Code::Blocks 中文字符输出乱码的问题求解?
- 操作系统内存管理——分区、页式、段式管理
- 优秀API的几大特征