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. [界面开发新秀]免费的AYUI,开发360领航版系列教程[2/40]

            <界面开发风AYUI-基于WPF By AY> 大家好! 距离上篇博客发布有10天了,因为我在开发AYUI4.X效果更惊艳 我是AY,很高兴,终于可以写自己的作品的,网络博 ...

  2. 深入剖析 redis AOF 持久化策略

    本篇主要讲的是 AOF 持久化,了解 AOF 的数据组织方式和运作机制.redis 主要在 aof.c 中实现 AOF 的操作. 数据结构 rio redis AOF 持久化同样借助了 struct ...

  3. java单例之enum实现方式

    传统的两私有一公开(私有构造方法.私有静态实例(懒实例化/直接实例化).公开的静态获取方法)涉及线程安全问题(即使有多重检查锁也可以通过反射破坏单例), 目前最为安全的实现单例的方法是通过内部静态en ...

  4. Apache shiro之权限校验流程

    从张开涛blog学习后整理:http://jinnianshilongnian.iteye.com/blog/2018398 图片原图比较大,建议将图片在新的选项卡打开后100%大小浏览 在权限校验中 ...

  5. Windows 7下安装MongoDB

    1.下载mongodb-win32-x86_64-2008plus-2.6.7-signed.msi(如今最新版本号已经到了3.0) 2.如果为64位操作系统则双击 mongodb-win32-x86 ...

  6. 高吞吐量的分布式发布订阅消息系统Kafka--spring-integration-kafka的应用

    一.概述 Spring Integration Kafka 是基于 Apache Kafka 和Spring Integration来集成Kafka,对开发配置提供了方便. 二.配置 1.spring ...

  7. ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" can not work

    ValidationExpression="http(s)?://([\w-]+\.)+[\w-]+(/[\w- ./?%&=]*)?" can not work &quo ...

  8. Hybris电商方案介绍(企业全渠道) B2B B2C O2O建设

    1). 什么是Hybris: hybris software成立于1997年,2013年与SAP整合,成为SAP旗下的一份子,提供全渠道客户互动与商务解决方案,该解决方案能够为各机构提供客户的实时背景 ...

  9. 关于cnas培训

    关于cnas(2014-12-9----2014-12-12) 目的:完成内审相关知识培训,可以做一个合格的内审人员 过程: 1.为什么会存在实验室 2.iso9000是怎么产生的,以及存在的意义 3 ...

  10. dissmiss a UISearchBar with an SearchBarController

    If you want do dissmiss a UISearchBar with an SearchBarController, just use this Code: [self.searchD ...