给出一些数组a[i],每次询问为li,ri,定义f[li]=a[li],f[li+1]=a[li+1],对于其他不超过ri的位置,f[x]=f[x-1]+a[x]*f[x-2] 。

题目有着浓浓的矩阵气息。

f[x]=f[x-1]+a[x]*f[x-2]

f[x-1]=f[x-1]+0

根据上面两个我们就可以知道

f[x]=========|1,a[x]|         f[x-1]

f[x-1]=======|1 ,  0|         f[x-2]

这样我们就把矩阵构造出来了,相当于每次询问某一段区间的矩阵的乘积。

由于是连续的区间,线段树即可解决问题。

注意矩阵是放在左边,所以大的位置放在左边,线段树操作的时候也需要注意了。

召唤代码君:

#include <iostream>
#include <cstring>
#include <cstdio>
#define maxn 300300
#define mod 1000000007
typedef long long ll;
using namespace std; class Mat{
public:
ll f[][];
Mat() { f[][]=f[][]=f[][]=f[][]=; }
Mat(int f1,int f2,int f3,int f4){
f[][]=f1,f[][]=f2,f[][]=f3,f[][]=f4;
}
Mat operator * (Mat m1) const{
Mat m0;
for (int i=; i<; i++)
for (int j=; j<; j++)
for (int k=; k<; k++)
m0.f[i][j]=(m0.f[i][j]+f[i][k]*m1.f[k][j])%mod;
return m0;
}
void output(){
cout<<f[][]<<' '<<f[][]<<'\n'<<f[][]<<' '<<f[][]<<'\n';
}
}tree[maxn]; int n,m,T,a[maxn]; void build(int rt,int l,int r)
{
if (l==r){
tree[rt]=Mat(,a[l],,);
return;
}
int mid=(l+r)>>;
build(rt<<,l,mid);
build(rt<<|,mid+,r);
tree[rt]=tree[rt<<|]*tree[rt<<];
} Mat query(int rt,int l,int r,int L,int R)
{
if (L<=l && R>=r) return tree[rt];
int mid=(l+r)>>;
Mat tot(,,,);
if (R> mid) tot=query(rt<<|,mid+,r,L,R);
if (L<=mid) tot=tot*query(rt<<,l,mid,L,R);
return tot;
} int main()
{
int x,y;
scanf("%d",&T);
while (T--)
{
scanf("%d%d",&n,&m);
for (int i=; i<=n; i++) scanf("%d",&a[i]);
build(,,n);
while (m--)
{
scanf("%d%d",&x,&y);
if (y==x || y==x+){
if (y==x) printf("%d\n",a[x]);
else printf("%d\n",a[x+]);
continue;
}
Mat tmp=query(,,n,x+,y);
/*
cout<<" ans Mat is : \n";
tmp.output();
cout<<" ........... the end of Mat.";
*/
printf("%d\n",(int)((tmp.f[][]*a[x+]+tmp.f[][]*a[x])%mod));
}
}
return ;
}

ZOJ3772_Calculate the Function的更多相关文章

  1. 通过百度echarts实现数据图表展示功能

    现在我们在工作中,在开发中都会或多或少的用到图表统计数据显示给用户.通过图表可以很直观的,直接的将数据呈现出来.这里我就介绍说一下利用百度开源的echarts图表技术实现的具体功能. 1.对于不太理解 ...

  2. jsp中出现onclick函数提示Cannot return from outside a function or method

    在使用Myeclipse10部署完项目后,原先不出错的项目,会有红色的叉叉,JSP页面会提示onclick函数错误 Cannot return from outside a function or m ...

  3. JavaScript function函数种类

    本篇主要介绍普通函数.匿名函数.闭包函数 目录 1. 普通函数:介绍普通函数的特性:同名覆盖.arguments对象.默认返回值等. 2. 匿名函数:介绍匿名函数的特性:变量匿名函数.无名称匿名函数. ...

  4. 在ubuntu16.10 PHP测试连接MySQL中出现Call to undefined function: mysql_connect()

    1.问题: 测试php7.0 链接mysql数据库的时候发生错误: Fatal error: Uncaught Error: Call to undefined function mysqli_con ...

  5. jquery中的$(document).ready(function() {});

    当文档载入时执行function函数里的代码, 这部分代码主要声明,页面加载后 "监听事件" 的方法.例如: $(document).ready( $("a") ...

  6. Function.prototype.toString 的使用技巧

    Function.prototype.toString这个原型方法可以帮助你获得函数的源代码, 比如: function hello ( msg ){ console.log("hello& ...

  7. 转:ORA-15186: ASMLIB error function = [asm_open], error = [1], 2009-05-24 13:57:38

    转:ORA-15186: ASMLIB error function = [asm_open], error = [1], 2009-05-24 13:57:38http://space.itpub. ...

  8. [Xamarin] 透過Native Code呼叫 JavaScript function (转帖)

    今天我們來聊聊關於如何使用WebView 中的Javascript 來呼叫 Native Code 的部分 首先,你得先來看看這篇[Xamarin] 使用Webview 來做APP因為這篇文章至少講解 ...

  9. Oracle数据库自动备份SQL文本:Procedure存储过程,View视图,Function函数,Trigger触发器,Sequence序列号等

    功能:备份存储过程,视图,函数触发器,Sequence序列号等准备工作:--1.创建文件夹 :'E:/OracleBackUp/ProcBack';--文本存放的路径--2.执行:create or ...

随机推荐

  1. 另一种遍历Map的方式: Map.Entry 和 Map.entrySet()

    源网址: http://blog.csdn.net/mageshuai/article/details/3523116 今天看Think in java 的GUI这一章的时候,里面的TextArea这 ...

  2. Hihocoder 1035 [树形dp]

    /* 题意: 不要低头,不要放弃,不要气馁,不要慌张. PS:人生第一道自己独立做出来的树形dp... 给一棵树,标号1到n,每条边有两个权值,步行时间和驾车时间.车在1号点. 给m个必须访问的关键点 ...

  3. 配置 Gii 允许访问的 IP 地址

    通过本机以外的机器访问 Gii,请求会被出于安全原因拒绝,在 config/web.php 配置 Gii 为其添加允许访问的 IP 地址: if (YII_ENV_DEV) { // configur ...

  4. (转载)SQL Server 2005 如何启用xp_cmdshell组件

    原文地址:http://www.cnblogs.com/atree/p/SQL_SERVER_xp_cmdshell.html [错误描述]: SQL Server阻止了对组件‘xp_cmdshell ...

  5. 44. Decode Ways && Gray Code

    Decode Ways A message containing letters from A-Z is being encoded to numbers using the following ma ...

  6. Sublime Text对Python代码加注释的快捷键

    一直在Coursera上补基础课,发现很多课程都用Python作为教学语言,学了一下感觉果然好,简直是用英语在写代码.(我建Python目录的时候发现去年学过一点点Python,居然一点都不记得了= ...

  7. net.sf.json 时间格式的转化

    后台代码 //后台代码 response.setCharacterEncoding("UTF-8"); JsonConfig jsonConfig = new JsonConfig ...

  8. FIO使用指南

    前言 fio是测试IOPS的非常好的工具,用来对硬件进行压力测试和验证,支持13种不同的I/O引擎,包括:sync,mmap, libaio, posixaio, SG v3, splice, nul ...

  9. C# 操作pem 文件

    using Dscf.Bpl.InformationAuditBpl; using Dscf.Bpl.ProductBpl; using Dscf.Global.CommonAduit; using ...

  10. uniDBGrid导入数据库(转红鱼儿)

    有朋友问如何将excel导入数据库,这是我做的uniGUI项目中代码,实现uniDBGrid导入数据库的函数,因为用了kbmMW,所以你看到是将uniDBGrid导入kbmMWClientQuery, ...