Z0J 3772 Calculate the Function 线段树+矩阵
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 线段树+矩阵的更多相关文章
- ZOJ 3772 Calculate the Function 线段树+矩阵
Calculate the Function Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %ll ...
- ZOJ3772 - Calculate the Function(线段树+矩阵)
题目大意 给定一个序列A1 A2 .. AN 和M个查询 每个查询含有两个数 Li 和Ri. 查询定义了一个函数 Fi(x) 在区间 [Li, Ri] ∈ Z. Fi(Li) = ALi Fi(Li ...
- Wannafly Winter Camp 2019.Day 8 div1 E.Souls-like Game(线段树 矩阵快速幂)
题目链接 \(998244353\)写成\(99824435\)然后调这个线段树模板1.5h= = 以后要注意常量啊啊啊 \(Description\) 每个位置有一个\(3\times3\)的矩阵, ...
- 线段树 + 矩阵 --- ZOJ 3772 Calculate the Function
Calculate the Function Problem's Link: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCod ...
- 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 ...
- CF718C Sasha and Array 线段树+矩阵加速
正解:线段树 解题报告: 传送门! 首先这种斐波拉契,又到了1e9的范围,又是求和什么的,自然而然要想到矩阵加速昂 然后这里主要是考虑修改操作,ai+=x如果放到矩阵加速中是什么意思呢QAQ? 那不就 ...
随机推荐
- 016.Oracle数据库,取本季度第一天,取本季度最后一天
/*取本季度第一天,取本季度最后一天*/ SELECT trunc(sysdate, 'Q') AS 本季度第一天 , add_months(trunc(sysdate, ) AS 本季度最后一天 F ...
- asp.net+sql数据库学生信息管理系统
一款学生信息管理系统送给大家. 功能部分: 1.教师管理(添加,修改,删除,查看) 2.学生管理(添加,修改,删除,查看) 3.班级管理(添加,修改,删除,查看) 4.学生成绩管理(添加,修改,删除, ...
- RadioButton之互斥选择和Toast显示
前言: RadioButton用来单选并且用Toast来进行提示所选内容 RadioButton标签单独写的时候不能出现互斥现象,代码如下 <RadioButton android:layout ...
- checkbox checked属性值
记住我1<input type='checkbox' /> 记住我2<input type='checkbox' /> <button onclick='hehe();' ...
- HDU - 6201 transaction transaction transaction(spfa求最长路)
题意:有n个点,n-1条边的无向图,已知每个点书的售价,以及在边上行走的路费,问任选两个点作为起点和终点,能获得的最大利益是多少. 分析: 1.从某个结点出发,首先需要在该结点a花费price[a]买 ...
- Maven项目工程目录
maven工程目录规范: src/main/java 存放项目的.java文件 src/main/resources 存放项目的资源文件,如spring.hibernate配置文件 src/t ...
- DRF项目之实现用户密码加密保存
在DRF项目的开发中,我们通过直接使用序列化器保存的用户信息时,用户的密码是被明文保存到数据库中. 代码实现: def create(self, validated_data): '''重写creat ...
- Django——优美的Path()函数
path( )作用:解析URL地址 path( ) 标准语法: (<>为必须的参数,[]为可选参数) path(<route>, <view>, [name=Non ...
- 152-PHP htmlspecialchars函数
<?php //定义一个HTML代码字符串 $str=<<<HTM <a href=#><b><i>到一个网址的链接<>< ...
- JNI操作二维数组
之前的文章讲解了有关JNI使用方法,这篇文章进阶一点,介绍下JNI操作二维数组的方法.有了之前文章的操作JNI的方法,这里直接上代码了. Java代码部分 package com.testjni; p ...