线段树 + 矩阵 --- 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 ...
随机推荐
- linux上挂载windows共享文件夹
linux上挂载windows共享文件夹 1.共享windows目录 挂载之前得创建一个有password的用户(当前用户也能够),并将你要挂载的目录进行共享,并赋予读写权限 如图. watermar ...
- Ubuntu server解决不能访问外网问题
Ubuntu server解决不能访问外网问题 在Ubuntu Server上设置访问外网时,需要设置dns,通常是将dns添加到/etc/resolv.conf文件中. 但是将dns添加至/etc/ ...
- 2.C#自定义Attribute
阅读目录 一:C#自定义Attribute 二:AttributeUsageAttribute中的3个属性(Property)中的AttributeTargets 三:Attribut ...
- dapper 操作类封装
using System; using System.Collections.Generic; using System.Data; using System.Data.SQLite; using S ...
- 用Jquery load text文本到網頁遇到的問題
HTML <div id="divText"></div> Javascript $('#divText').load(fileName ,function ...
- 【Cocos2d-Js基础教学(2)类的使用和面向对象】
类的使用和面向对象 大家都知道在cocos2d-x 底层是C++编写的,那么就有类的概念和继承机制. 但是在JS中,是没有类这个概念的,没有提供类,没有C++的类继承机制. 那么JS是通过什么方式实现 ...
- Angular 使用
tks: 使用: http://developer.51cto.com/art/201302/380661.htm http://www.infoq.com/cn/news/2013/02/angul ...
- Android Studio开发第四篇版本管理Git(下)
前面一片介绍了在as下如何关联远程仓库,这篇就介绍在开发过程中怎么应用. 提交+Push 如果本地开发代码有改动了或者你觉得某功能做完了,你打算把改动代码提交到远程仓库,这个时候很简单, 还是在工具栏 ...
- IEEEtran模版中添加中文:\usepackage{CJKutf8}
\documentclass[conference]{IEEEtran} \usepackage{cite} \usepackage{graphicx} \usepackage{CJKutf8} \b ...
- 自己动手搭建 Redis 环境,并建立一个 .NET HelloWorld 程序测试
关于 Redis ,下面来自百度百科: redis是一个key-value存储系统.和Memcached类似,它支持存储的value类型相对更多,包括string(字符串).list(链表).set( ...