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. Spark Deploy 模块

    Spark Scheduler 模块的文章中,介绍到 Spark 将底层的资源管理和上层的任务调度分离开来,一般而言,底层的资源管理会使用第三方的平台,如 YARN 和 Mesos.为了方便用户测试和 ...

  2. c# 外挂操作(内存操作)(内存读写取窗口句柄移动窗口取模块地址)工具类

    来源于网上  参考 https://www.cnblogs.com/fuhua/p/5877781.html 等众多文章 详情取看我第二个例子封装功能较多 https://www.cnblogs.co ...

  3. CPU各个具体的指标含义

    CPU各个具体的指标含义解释如下: ①CPU(监测到的master主机上的CPU使用情况) 从图中看出,一共有五个关于CPU的指标.分别如下: 1. User User表示:CPU一共花了多少比例的时 ...

  4. IDA使用初步

    按空格看结构图,再按空格看汇编代码,按F5反编译 shift+F12 搜索中文字符串,通过字符串所在位置定位关键信息. 双击可能出flag的语句跳转至关键字符串. 想F5生成C伪代码,先crtl+X打 ...

  5. 中兴将用“加减乘除”建立理想 5G 网络

      6 月 28 日,MWC 2019 上海展期间,中兴通讯执行董事.总裁徐子阳发表演讲表示,面对 5G 建网大势,要看破大势,不破不立.为此中兴将用“加减乘除”建立理想 5G 网络. 何为“加减乘除 ...

  6. cf 506 A. Mr. Kitayuta, the Treasure Hunter

    不知道这个sb题怎么做错了.. /*#include <bits/stdc++.h> #define LL long long using namespace std; inline in ...

  7. pycharm 配置 github 版本控制 VCS

    场景:github上没有repository,将pycharm中的工程push到github 1.在pycharm中登录github 2.新建工程后,选择“share project on githu ...

  8. 逆向-PE头解析

    目录 PE头解析 数据结构 IMAGE_DOS_HEADER IMAGE_NT_HEADERS 区块 PE头解析 PE 格式是Windows系统下组织可执行文件的格式.PE文件由文件头和对应的数据组成 ...

  9. python --- excel文件处理

    1.安装第三方库:openpyxl 2.操作示例 from openpyxl import load_workbook #.打开文件 file = load_workbook("test.x ...

  10. 基于Ambari Server部署HDP集群实战案例

    基于Ambari Server部署HDP集群实战案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.部署Ambari Server端 博主推荐阅读: https://www.c ...