http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=5235

这种题目居然没想到,一开始往矩阵快速幂想去了,因为之前跪了太多矩阵快速幂,后来就。。哎,擦。怎么没想到就是个线段树呢

因为1 A[x]  *     A[x-1]  这个是很容易推出的,比赛的时候看到这个就想那个快速幂去了,根本没往线段树上想,其实用线段树存储前面的矩阵,得到一个询问

1    0          A[x-2]

L R,则访问 L+2 ,R的矩阵部分提取出来,再跟A[L] A[L+1]相乘就是结果了

则建树为 nlogn,访问为mlogn,由于n和m都在10^5,所以可以承受

要注意的是矩阵不满足交换律,在这个线段树里面矩阵相乘的时候必须从大的到小的来乘。

#include <iostream>
#include <cstdio>
#include <cstring>
#define lson rt<<1,l,mid
#define rson (rt<<1|1),mid+1,r
#define N 100010
#define ll unsigned long long
using namespace std;
ll A[N],n,m;
const ll M=;
struct MAT
{
ll mat[][];
}tree[N*],E;
void init(int rt,int x)
{
tree[rt].mat[][]=tree[rt].mat[][]=;
tree[rt].mat[][]=A[x];
tree[rt].mat[][]=;
}
MAT operator *(MAT a,MAT b)
{
MAT c;
memset(c.mat,,sizeof (c.mat));
ll tmp;
for (int i=;i<;i++)
{
for (int j=;j<;j++)
{
for (int k=;k<;k++)
{
tmp=(a.mat[i][k]*b.mat[k][j])%M;
c.mat[i][j]+=tmp;
if (c.mat[i][j]>M)
c.mat[i][j]%=M;
}
}
}
return c;
}
void up(int l,int r,int rt)
{
tree[rt]=tree[rt<<|]*tree[rt<<];
}
void build(int rt,int l,int r)
{
if (l>=r)
{
init(rt,l);
return;
}
int mid=(l+r)/;
build(lson);
build(rson);
up(l,r,rt);
}
void test(int rt,int l,int r)
{
cout<<l<<" lr "<<r<<":"<<endl;
for (int i=;i<;i++)
{
for (int j=;j<;j++)
{
cout<<tree[rt].mat[i][j]<<" ";
}
cout<<endl;
}
cout<<endl; if (l>=r)
{
return;
}
int mid=(l+r)/;
test(lson);
test(rson);
}
MAT query(int L,int R,int rt,int l,int r)
{
MAT c=E;
if (L<=l && r<=R)
{
c=tree[rt];
return c;
}
int mid=(l+r)/;
if (R>mid)
c=query(L,R,rson);
if (L<=mid)
c=c*query(L,R,lson);
return c;
}
int main()
{
int t,a,b;
scanf("%d",&t);
memset(E.mat,,sizeof E.mat);
E.mat[][]=E.mat[][]=;
while (t--)
{
scanf("%llu%llu",&n,&m);
for (ll i=;i<=n;i++)
scanf("%llu",&A[i]); build(,,n);
//test(1,1,n);
for (ll i=;i<m;i++)
{
scanf("%d%d",&a,&b);
if (a>b) swap(a,b);
if (b-a<=)
{
if (b==a)
printf("%llu\n",A[a]);
else
printf("%llu\n",A[b]);
continue;
}
MAT c;
c=query(a+,b,,,n);
ll ans=((c.mat[][]*A[a+])%M)+((c.mat[][]*A[a])%M);
ans%=M;
printf("%llu\n",ans);
}
}
return ;
}

Z0J 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. ZOJ3772 - Calculate the Function(线段树+矩阵)

    题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...

  3. Wannafly Winter Camp 2019.Day 8 div1 E.Souls-like Game(线段树 矩阵快速幂)

    题目链接 \(998244353\)写成\(99824435\)然后调这个线段树模板1.5h= = 以后要注意常量啊啊啊 \(Description\) 每个位置有一个\(3\times3\)的矩阵, ...

  4. 线段树 + 矩阵 --- ZOJ 3772 Calculate the Function

    Calculate the Function Problem's Link:   http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCod ...

  5. 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 ...

  6. CF719E(线段树+矩阵快速幂)

    题意:给你一个数列a,a[i]表示斐波那契数列的下标为a[i],求区间对应斐波那契数列数字的和,还要求能够维护对区间内所有下标加d的操作 分析:线段树 线段树的每个节点表示(f[i],f[i-1])这 ...

  7. 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法

    C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...

  8. LOJ2980 THUSC2017大魔法师(线段树+矩阵乘法)

    线段树每个节点维护(A,B,C,len)向量,操作即是将其乘上一个矩阵. #include<iostream> #include<cstdio> #include<cma ...

  9. CF718C Sasha and Array 线段树+矩阵加速

    正解:线段树 解题报告: 传送门! 首先这种斐波拉契,又到了1e9的范围,又是求和什么的,自然而然要想到矩阵加速昂 然后这里主要是考虑修改操作,ai+=x如果放到矩阵加速中是什么意思呢QAQ? 那不就 ...

随机推荐

  1. javascript中退出语句break,continue和return 比较

    在 break,continue和return 三个关键字中, break,continue是一起的,return 是函数返回语句,但是返回的同时也将函数停止. 首先:break和continue两个 ...

  2. python中groupby函数详解(非常容易懂)

    一.groupby 能做什么? python中groupby函数主要的作用是进行数据的分组以及分组后地组内运算! 对于数据的分组和分组运算主要是指groupby函数的应用,具体函数的规则如下: df[ ...

  3. wamp修改端口localhost

    一.修改Apache端口 1.在界面中选Apache,弹出隐藏菜单选项,打开配置文件httpd.conf: 2.找到 Listen 80: ServerName localhost:80; 3.将 8 ...

  4. Asp.net mvc项目分页功能

    1.定义一个分页用的Page<T>类 /* 使用示例: var pager = new Pager<Article>( this.ControllerContext, //上下 ...

  5. webapi------宿主程序

    业务场景: 公司的容器程序需要给前端暴露接口但是代码里面又不想写webapi项目工程就用到了宿主可以达到webapi的效果 1.owin实现 2.其他实现 测试实现如下 1.新建一个控制台程序 2.新 ...

  6. C++ for无限循环~

    无限循环 如果条件永远不为假,则循环将变成无限循环.for 循环在传统意义上可用于实现无限循环.由于构成循环的三个表达式中任何一个都不是必需的,您可以将某些条件表达式留空来构成一个无限循环. #inc ...

  7. 通过request获得全路径

     <% String test = request.getScheme()+"://"+request.getServerName()+":"+reque ...

  8. ACM-Fire Net

    题目描述:Fire Net   Suppose that we have a square city with straight streets. A map of a city is a squar ...

  9. CentOS 7 下oracle 11G R2 ADG 搭建

    本文记录ADG搭建操作步骤,首先在虚拟机CentOS中安装并配置好oracle 11g R2(具体安装步骤在我的另一篇博客中),然后拷贝一份虚拟机,修改新虚拟机的主机名和ip配置,这时候主库和备库是一 ...

  10. String的Split使用方法(以特定字符分隔,提取所需信息)

    此处复制一串以空格分隔的数字,提取数字进行排序 int[] a = new int[10]; string input = Console.ReadLine();//获取用户输入的字符串 char[] ...