线段树 + 矩阵 --- ZOJ 3772 Calculate the Function
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的更多相关文章
- ZOJ 3772 Calculate the Function 线段树+矩阵
Calculate the Function Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %ll ...
- 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)转化为 ...
- Wannafly Winter Camp 2019.Day 8 div1 E.Souls-like Game(线段树 矩阵快速幂)
题目链接 \(998244353\)写成\(99824435\)然后调这个线段树模板1.5h= = 以后要注意常量啊啊啊 \(Description\) 每个位置有一个\(3\times3\)的矩阵, ...
- Z0J 3772 Calculate the Function 线段树+矩阵
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这种题目居然没想到,一开始往矩阵快速幂想去了,因为之前跪了太多矩阵快速幂 ...
- ZOJ3772 - Calculate the Function(线段树+矩阵)
题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...
- 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 ...
- CF719E(线段树+矩阵快速幂)
题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...
- 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...
- LOJ2980 THUSC2017大魔法师(线段树+矩阵乘法)
线段树每个节点维护(A,B,C,len)向量,操作即是将其乘上一个矩阵. #include<iostream> #include<cstdio> #include<cma ...
随机推荐
- 解决Electron加载带jquery的项目报错问题
<!-- Insert this line above script imports --> <script>if (typeof module === 'object') { ...
- nodejs express 框架解密3-中间件模块
本文档是基于express 3.4.6 的 在上篇中我们提到了中间件,这篇主要解释这个模块,middleware.js 为: var utils = require('./utils'); /** * ...
- C# Activex开发、打包、签名、发布 C# Activex开发、打包、签名、发布 [转]
C# Activex开发.打包.签名.发布 2013-06-22 12:01:20 浏览:3823 一.前言 最近有这样一个需求,需要在网页上面启动客户端的软件,软件之间的通信.调用,单单依靠HTML ...
- Android开发(二十九)——layout_weight的含义
首先声明只有在Linearlayout中,该属性才有效.之所以android:layout_weight会引起争议,是因为在设置该属性的同时,设置android:layout_width为wrap_c ...
- 菜鸟调错(八)—— Maven编译错误:不兼容的类型的解决方案
泛型在实际的工作中应用非常广泛,关于泛型就不在这里赘述了,感兴趣请戳<重新认识泛型>.项目中用到了如下的泛型: public <T> T query(String sql, R ...
- Linux环境的PHP执行
/usr/local/php5/bin/php -c /var/spool/php.ini -q /var/spool/auto.php
- android 中handler的用法分析 (二)
.Looper 的构造方法是私有的,不能在package外面直接初始化.一般通过Looper.prepare()初始化.Looper.myLooper()获取.2.Looper 中的静态变量 Thre ...
- MYSQL校对规则
一.前言 有时候遇到这种情况,你用一个like语句查询,查到的结果中有一些并没有包含你查询的关键词的纪录:有时候遇到这种情况,你的数据库自作聪明的大小写不敏感,让你在更新时把大小写不同的两条记录都更新 ...
- 在UWP应用中实现Gif播放
众所周知,在UWP应用框架中,Image控件是无法播放GIF的图片,只能显示静态图,这样的体验不是特别友好.我在Win8.WP8.1的时候实现过gif播放功能,但是最近发现性能和播放效果都差强人意,大 ...
- [转]安卓开发startservice 和bindservice详解
原文 作者:aikongmeng 来源:安卓中文网 博主暗表:搜到此文,终于为我解惑,bindService并不会真正启动service,不会调用onStartCommand!还需要再bind之前st ...