思路来源:http://blog.csdn.net/u013654696/article/details/23037407#comments

【做浙大校赛的时候没有看这道题,事后做的。思路不是自己的,但代码是自己敲的,由于伦家不懂如何用TeX敲出如此优美的公式,所以具体请看上面的博客链接(づ ̄3 ̄)づ╭。虽然说思路对应下的代码很好敲,但如果在比赛中我肯定不一定想得到这么做。在这道题中,线段树的节点与区间存的不是具体的值,是对应的一个矩阵。做了这道题也让我明白了线段树原来还可以那么好用吖(づ ̄3 ̄)づ╭❤~。以后对于这样的矩阵都可以用线段树来呢,建矩阵线段树的时间复杂度是O(nlogn)。】

总结:(ー`´ー)敲了一天,开始是体会错公式了,结果乘多了最后两个矩阵,后来又忘记取余了~~~这个逗比的教训告诉我们,做事要细心~~还有代码写矬了果断删掉重写!

下面上AC代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define maxn 100010
#define mod 1000000007
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1 long long fun[maxn];
long long num[maxn<<2];
int n, m; class matrix{
public:
long long mat[2][2];
matrix()
{
mat[0][0] = mat[1][1] = 1;
mat[0][1] = mat[1][0] = 0;
}
matrix(long long a)
{
mat[0][0] = mat[1][0] = 1;
mat[0][1] = a;
mat[1][1] = 0;
}
matrix operator*(const matrix& m)const
{
matrix tmp;
for(int i = 0; i < 2; i++)
for(int j = 0; j < 2; j++){
tmp.mat[i][j] = 0;
for(int k = 0; k < 2; k++)
{
tmp.mat[i][j] += mat[i][k] * m.mat[k][j] % mod;
tmp.mat[i][j] %= mod;
}
}
return tmp;
}
}; matrix sgt[maxn<<2];
void pushup(int rt)
{
sgt[rt] = sgt[rt<<1|1] * sgt[rt<<1]; //注意矩阵乘的方向!!
} void build(int l, int r, int rt)
{
if(l == r)
{
sgt[rt] = matrix(fun[l]);
return;
}
int m = (l+r)>>1;
build(lson);
build(rson);
pushup(rt);
} matrix query(int l, int r, int rt, int L, int R)
{
if(L <= l && r <= R)
{
return sgt[rt];
}
int m = (l + r)>>1;
matrix tmp;
if(m < R) tmp = tmp * query(rson, L, R); //注意矩阵乘的方向!!
if(L <= m) tmp = tmp * query(lson, L, R);
return tmp;
} long long result(int l, int r, int rt, int L, int R)
{
if(R - L < 2) //判断区间长度,小于等于二的直接输出右区间;
{
return fun[R];
}
matrix tmp;
tmp = query(l, r, rt, L+2, R);
long long a;
a = tmp.mat[0][0] * fun[L+1] + tmp.mat[0][1] * fun[L];
a %= mod;
return a;
} int main()
{
int T;
cin>>T;
while(T--)
{
scanf("%d%d", &n, &m);
for(int i = 1; i <= n; i++)
scanf("%d", &fun[i]);
build(1, n, 1);
long long a;
for(int i = 0; i < m; i++)
{
a = 0;
int c, d;
scanf("%d%d", &c, &d);
a = result(1, n, 1, c, d);
printf("%d\n",a);
}
}
return 0;
}

作者:u011652573 发表于2014-4-8 21:24:15 原文链接
阅读:54 评论:0 查看评论

[原]zoj3772--【水题】线段树区间查询+矩阵乘法的更多相关文章

  1. Codeforces 750E - New Year and Old Subsequence(线段树维护矩阵乘法,板子题)

    Codeforces 题目传送门 & 洛谷题目传送门 u1s1 我做这道 *2600 的动力是 wjz 出了道这个套路的题,而我连起码的思路都没有,wtcl/kk 首先考虑怎样对某个固定的串计 ...

  2. Codeforces 1368H - Breadboard Capacity(最小割+线段树维护矩阵乘法)

    Easy version:Codeforces 题面传送门 & 洛谷题面传送门 Hard version:Codeforces 题面传送门 & 洛谷题面传送门 首先看到这种从某一种颜色 ...

  3. CF633H Fibonacci-ish II 莫队、线段树、矩阵乘法

    传送门 这题除了暴力踩标程和正解卡常数以外是道很好的题目 首先看到我们要求的东西与\(Fibonacci\)有关,考虑矩阵乘法进行维护.又看到\(n \leq 30000\),这告诉我们正解算法其实比 ...

  4. 【CF1252K】Addition Robot(线段树,矩阵乘法)

    题意: 思路:因为线段树上每一段的矩阵之积只有两种,预处理一下,翻转的时候下传tag然后把另一种可能性换上来就好 #include<bits/stdc++.h> using namespa ...

  5. poj 2155:Matrix(二维线段树,矩阵取反,好题)

    Matrix Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 17880   Accepted: 6709 Descripti ...

  6. 线段树维护矩阵【CF718C】 Sasha and Array

    Description 有一个长为\(n\)的数列\(a_{1},a_{2}...a_{n}\),你需要对这个数列维护如下两种操作: \(1\space l \space r\space x\) 表示 ...

  7. poj 1195:Mobile phones(二维线段树,矩阵求和)

    Mobile phones Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 14391   Accepted: 6685 De ...

  8. 「CQOI2006」简单题 线段树

    「CQOI2006」简单题 线段树 水.区间修改,单点查询.用线段树维护区间\([L,R]\)内的所有\(1\)的个数,懒标记表示为当前区间是否需要反转(相对于区间当前状态),下方标记时懒标记取反即可 ...

  9. Subsequence Count 2017ccpc网络赛 1006 dp+线段树维护矩阵

    Problem Description Given a binary string S[1,...,N] (i.e. a sequence of 0's and 1's), and Q queries ...

随机推荐

  1. 02.Apache FtpServer使用数据库管理用户

    1.创建数据库及表 使用\apache-ftpserver-1.0.6\res\ftp-db.sql建表,内容如下: CREATE TABLE FTP_USER ( userid VARCHAR(64 ...

  2. poj 1470 Closest Common Ancestors LCA

    题目链接:http://poj.org/problem?id=1470 Write a program that takes as input a rooted tree and a list of ...

  3. ZOJ 1111 Poker Hands

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1111 A poker hand consists of 5 ca ...

  4. jquery的ajax向后台提交时,乱码的解决方案

    1. 可以给每个参数加上encodeURIComponent(),然后在后台获得参数后用URLDecoder.decode(string, 'utf-8')解码. 2. 后台不用解码. $.ajax( ...

  5. ios7 自定义UINavigationBar UIBarButtonItem 10px的偏移纠正

    为UINavigationBar 写一个分类.UINavigationItem+correct_offset.h  转载 http://www.colabug.com/thread-1112420-1 ...

  6. HDOJ1050

    Moving Tables Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tot ...

  7. C# 对委托的BeginInvoke,EndInvoke 及Control 的BeginInvoke,EndInvoke 的理解

    using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; usin ...

  8. 如何修正导入模型的旋转? How do I fix the rotation of an imported model?

    原地址:http://game.ceeger.com/Manual/HOWTO-FixZAxisIsUp.html Some 3D art packages export their models s ...

  9. NWR协议

    NWR是一种在分布式存储系统中用于控制一致性级别的一种策略.在Amazon的Dynamo云存储系统中,就应用NWR来控制一致性. 让我们先来看看这三个字母的含义:N:在分布式存储系统中,有多少份备份数 ...

  10. Git 10 周年之际,创始人 Linus Torvalds 访谈

    点这里 十年前的这一周,linux 内核社区面临一个根本性的挑战:他们不再能够使用他们的修复控制系统:BitKeeper,同时其他的软件配置管理遇到了对分布式系统的新需求.Linus Torvalds ...