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 = ;
const int MOD = ;
struct Mat
{
long long m[][];
Mat()
{
memset(m, , sizeof(m));
}
Mat operator * (const Mat &b)
{
Mat temp;
for(int i = ; i < ; i++)
for(int j = ; j < ; j++)
for(int k = ; k < ; 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 << ];
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[][] = ;
node[rt].mat.m[][] = a[l];
node[rt].mat.m[][] = ;
node[rt].mat.m[][] = ;
}
else
{
m = (l + r) >> ;
Build(rt << , l, m);
Build(rt << | , m + , r);
node[rt].mat = node[rt << | ].mat * node[rt << ].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) >> ;
if(qr <= m)
return Query(rt << , ql, qr);
else if(ql > m)
return Query(rt << | , ql, qr);
else
return Query(rt << | , m + , qr) * Query(rt << , 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 = ; i <= n; i++)
scanf("%d", &a[i]);
Build(, , n);
for(int i = ; i<= m; i++)
{
scanf("%d%d", &ql, &qr);
if(qr - ql >= )
{
f.m[][] = a[ql + ];
f.m[][] = a[ql];
res = Query(, ql + , qr) * f;
printf("%d\n", res.m[][]);
}
else
printf("%d\n", a[qr]);
}
}
return ;
}

线段树 + 矩阵 --- ZOJ 3772 Calculate the Function的更多相关文章

  1. ZOJ 3772 Calculate the Function 线段树+矩阵

    Calculate the Function Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %ll ...

  2. zoj 3772 Calculate the Function

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这道题需要构造矩阵:F(X)=F(X-1)+F(X-2)*A(X)转化为 ...

  3. Wannafly Winter Camp 2019.Day 8 div1 E.Souls-like Game(线段树 矩阵快速幂)

    题目链接 \(998244353\)写成\(99824435\)然后调这个线段树模板1.5h= = 以后要注意常量啊啊啊 \(Description\) 每个位置有一个\(3\times3\)的矩阵, ...

  4. Z0J 3772 Calculate the Function 线段树+矩阵

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这种题目居然没想到,一开始往矩阵快速幂想去了,因为之前跪了太多矩阵快速幂 ...

  5. ZOJ3772 - Calculate the Function(线段树+矩阵)

    题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...

  6. Codeforces Round #337 (Div. 2) D. Vika and Segments 线段树 矩阵面积并

    D. Vika and Segments     Vika has an infinite sheet of squared paper. Initially all squares are whit ...

  7. CF719E(线段树+矩阵快速幂)

    题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...

  8. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

  9. LOJ2980 THUSC2017大魔法师(线段树+矩阵乘法)

    线段树每个节点维护(A,B,C,len)向量,操作即是将其乘上一个矩阵. #include<iostream> #include<cstdio> #include<cma ...

随机推荐

  1. Codeforces Round #384 (Div. 2)B. Chloe and the sequence 数学

    B. Chloe and the sequence 题目链接 http://codeforces.com/contest/743/problem/B 题面 Chloe, the same as Vla ...

  2. 巧用css border

    上下左右边框交界处呈现平滑的斜线.利用这个特点,通过设置不同的上下左右边框宽度或颜色,可以得到小三角.梯形等. 调整宽度大小可以调节三角形形状. 实现三角形 示例1: #test1{ height:2 ...

  3. IOS从一个APP跳到另一个APP

    以下为跳转到大众点评APP代码如下: NSString *requestUrlString = @"dianping://shopinfo?id=1000"; NSURL *req ...

  4. BpBinder 转换为 BpCameraService 流程

    interface_cast<ICameraService>(binder) : 其中binder 为IBinder类型,实际为BpBinder interface_cast 定义在IIn ...

  5. Entity Framework: Joining in memory data with DbSet

    转载自:https://ilmatte.wordpress.com/2013/01/06/entity-framework-joining-in-memory-data-with-dbset/ The ...

  6. CvMat 矩阵的使用方法和简单程序

    一:CvMat* cvInitMatHeader( CvMat* mat, int rows, int cols, int type,void* data=NULL, int step=CV_AUTO ...

  7. 使用WebApi时Post和Put的区别

    简单的说Post是添加,Put是修改 吃不准的话,尝试用相同参数访问二次接口,结果不同的是Post(会产生多条记录),结果相同的是Put(仅为一条记录),例如:写博客就是Post:更新签名就是Put

  8. centos7.0 手动编译 lamp环境

    首先新建用户 lamper,并添加 sodu权限 两种方法:is not in the sudoers file 解决(转) xx is not in the sudoers file 问题解决[转载 ...

  9. (笔记)Linux内核学习(十一)之I/O层和I/O调度机制

    一 块I/O基本概念 字符设备:按照字符流的方式被有序访问的设备.如串口.键盘等. 块设备:系统中不能随机(不需要按顺序)访问固定大小的数据片(chunk 块)的设备. 如:硬盘.软盘.CD-ROM驱 ...

  10. EDM博主笔记:EDM邮件营销的几个细节问题

    其实说起EDM邮件营销很多做过的人都知道,目前国内邮件营销的效果其实是比较差的,为什么?因为国内没有多少使用邮件的习惯,如果不是工作所需估计很多的人都几天不碰邮件了,但是反观国外 邮件是其日常的一部分 ...