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. MongoDB副本集配置系列十:MongoDB local库详解和数据同步原理

    1:local库是MongoDB的系统库,记录着时间戳和索引和复制集等信息 gechongrepl:PRIMARY> use local switched to db local gechong ...

  2. android里TextView加下划线的几种方式

    如果是在资源文件里: <resources> <string name="hello"><u>phone:0123456</u>&l ...

  3. spring之ControllerAdvice注解

    @ControllerAdvice是Spring 3.2新增的注解,主要是用来Controller的一些公共的需求的低侵入性增强提供辅助,作用于@RequestMapping标注的方法上. Contr ...

  4. How to set colors of HTML tables

    There is a simple way to set the color of tables: use the bgcolor attribute of tag <table>.   ...

  5. c#列举和迭代器

    列举 - Enumeration 迭代器是一个值序列(集合)上的一个只读且只向前移动的游标.迭代器要么实现了IEnumerator接口,要么实现了IEnumerator<T>接口. 从技术 ...

  6. 旧文—冬日感怀

    冬日感怀   下雪了,虽不大,可那雪花落在手上,融在心里,便泛起淡淡的温柔,因为这是冬日的馈赠啊,是上苍的礼物,是往昔记忆的灰烬化作天地间的精灵装点纷繁琐碎的世界.于是,透过汽车上雾气氤氲的玻璃窗,人 ...

  7. 本人独立博客:http://www.zjmainstay.cn

    为了方便各种管理,本人创建了独立博客,博客地址:http://www.zjmainstay.cn 欢迎新老朋友围观.

  8. [转]GeoHash核心原理解析

    原文出处: zhanlijun    引子 机机是个好动又好学的孩子,平日里就喜欢拿着手机地图点点按按来查询一些好玩的东西.某一天机机到北海公园游玩,肚肚饿了,于是乎打开手机地图,搜索北海公园附近的餐 ...

  9. 使用docker搭建nfs实现容器间共享文件

    首先介绍下今天的两个主角:nfs和docker nfs 是什么 NFS(Network File System)即网络文件系统,是FreeBSD支持的文件系统中的一种,它允许网络中的计算机之间通过TC ...

  10. HTML5[2]:使用viewport控制手机浏览器布局

    基本 <meta name="viewport" content="width=device-width, initial-scale=1">192 ...