zoj3772Calculate the Function(矩阵+线段树)
表达式类似于斐波那契 但是多了一个变量 不能用快速幂来解 不过可以用线段树进行维护
对于每一个点够一个2*2的矩阵
1 a[i]
1 0 这个矩阵应该不陌生 类似于构造斐波那契的那个数列 还是比较容易能想到的
然后就用线段树进行维护 注意矩阵不满足交换律 在乘的时候要倒序。
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<stdlib.h>
#include<vector>
#include<cmath>
#include<queue>
#include<set>
using namespace std;
#define N 100010
#define LL long long
#define INF 0xfffffff
#define mod 1000000007
const double eps = 1e-;
const double pi = acos(-1.0);
const double inf = ~0u>>;
struct Mat
{
LL c[][];
}s[N<<];
LL o[N];
Mat operator * (Mat a,Mat b)
{
Mat c;
memset(c.c,,sizeof(c.c));
int i,j,k;
for(k = ; k < ; k++)
{
for(i = ; i < ;i++)
{
if(a.c[i][k]==) continue;//优化
for(j = ;j < ;j++)
{
if(b.c[k][j]==) continue;//优化
c.c[i][j] = (c.c[i][j]+a.c[i][k]*b.c[k][j])%mod;
}
}
}
return c;
}
void up(int w)
{
s[w] = s[w<<|]*s[w<<];
}
void build(int l,int r,int w)
{
if(l==r)
{
s[w].c[][] = s[w].c[][] = ;
s[w].c[][] = o[l];
s[w].c[][] = ;
return ;
}
int m = (l+r)>>;
build(l,m,w<<);
build(m+,r,w<<|);
up(w);
}
Mat getsum(int a,int b,int l,int r,int w)
{
if(a<=l&&b>=r)
{
return s[w];
}
int m = (l+r)>>,i,j;
Mat c;
for(i= ;i < ; i++)
{
for(j = ;j < ; j++)
{
if(i==j)
c.c[i][j] = ;
else
c.c[i][j] = ;
}
}
if(b>m)
c=c*getsum(a,b,m+,r,w<<|);
if(a<=m)
c=c*getsum(a,b,l,m,w<<);
return c; }
int main()
{
int i,n,m,t;
cin>>t;
while(t--)
{
scanf("%d%d",&n,&m);
for(i = ;i <= n; i++)
scanf("%lld",&o[i]);
build(,n,);
while(m--)
{
int a,b;
scanf("%d%d",&a,&b);
if(b-a<=)
{
printf("%lld\n",o[b]%mod);
continue;
}
Mat x;
x.c[][] = o[a+];
x.c[][] = o[a];
x.c[][] = ;x.c[][] = ;
Mat y = getsum(a+,b,,n,);
x = y*x;
printf("%lld\n",(x.c[][])%mod);
}
}
return ;
}
zoj3772Calculate the Function(矩阵+线段树)的更多相关文章
- 2014 Super Training #7 E Calculate the Function --矩阵+线段树
原题:ZOJ 3772 http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3772 这题算是长见识了,还从没坐过矩阵+线段树的题 ...
- CF 316E3 Summer Homework(斐波那契矩阵+线段树)
题目链接:http://codeforces.com/problemset/problem/316/E3 题意:一个数列A三种操作:(1)1 x y将x位置的数字修改为y:(2)2 x y求[x,y] ...
- 牛客多校第四场 J.Hash Function(线段树优化建图+拓扑排序)
题目传送门:https://www.nowcoder.com/acm/contest/142/J 题意:给一个hash table,求出字典序最小的插入序列,或者判断不合法. 分析: eg.对于序列{ ...
- 2016 ACM-ICPC Asia Regional Dalian Online HDU 5875 Function(线段树)
题意 求区间l~r的a[l]%a[l+1]%--%a[r]的值 思路 因为取模的变化是很快的,所以线段树查找区间内第一个小于等于a[l]的数的位置,更新ans后继续查找即可. 注意查询满足某种条件的位 ...
- [原]zoj3772--【水题】线段树区间查询+矩阵乘法
思路来源:http://blog.csdn.net/u013654696/article/details/23037407#comments [做浙大校赛的时候没有看这道题,事后做的.思路不是自己的, ...
- 线段树 + 矩阵 --- ZOJ 3772 Calculate the Function
Calculate the Function Problem's Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCod ...
- ZOJ3772 - Calculate the Function(线段树+矩阵)
题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...
- Z0J 3772 Calculate the Function 线段树+矩阵
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235 这种题目居然没想到,一开始往矩阵快速幂想去了,因为之前跪了太多矩阵快速幂 ...
- CF719E(线段树+矩阵快速幂)
题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...
随机推荐
- 求a + aa + aaa + aaaa + aaaaa ...的值,例如:1 + 11 + 111,2 + 22 + 222 + 2222 + 22222
#include <stdio.h> unsigned superposition(unsigned m, unsigned n); int main() { printf("1 ...
- js appendChild 自带remove和append两个功能
一提到appendChild ,我们就知道是在父节点上增加子节点. 经常使用到它就是动态增加一个元素,将其加入到指定的父节点下. 其实appendChild除了这个功能之外,他还有一个功能:如果子节点 ...
- leetcode 681. Next Closest Time
Given a time represented in the format "HH:MM", form the next closest time by reusing the ...
- uCos临界区保护
定义有三种method,stm32f4采用的是第三种:将当前中断的状态标志保存在一个局部变量cpu_sr中,然后再关闭中断.cpu_sr是一个局部变量,存在于所有需要关中断的函数中.注意到,在使用了该 ...
- Java 通过 HTTP 下载文件
1. [代码]Download.java package core.spider; import java.io.*;import java.net.*;import java.util.*; / ...
- I.MX6 不一定要设置BOOT_MODE进入烧录模式
/************************************************************************* * I.MX6 不一定要设置BOOT_MODE进入 ...
- SPOJ:Ada and Orange Tree (LCA+Bitset)
Ada the Ladybug lives near an orange tree. Instead of reading books, she investigates the oranges. T ...
- 微服务框架go-micro
微服务框架go-micro https://www.cnblogs.com/li-peng/p/9558421.html 产品嘴里的一个小项目,从立项到开发上线,随着时间和需求的不断激增,会越来越复杂 ...
- NOIP2007 矩阵取数游戏(区间DP)
传送门 这道题第一眼看上去可能让人以为是贪心……不过贪心并不行,因为每次的操作是有2的幂次方的权值的.这样的话直接每次贪心最小的就目光短浅.所以那我们自然想到了DP. 据说这是一道很正常的区间DP? ...
- oracle 在insert into的时候报ORA-00928: missing SELECT keyword错 [问题点数:100分,结帖人dm520]
转自:https://bbs.csdn.net/topics/310095274 INSERT INTO SA_Table(uniPositionCode,transferGroupName,appC ...