codeforces 719E E. Sasha and Array(线段树)
题目链接:
5 seconds
256 megabytes
standard input
standard output
Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types:
- 1 l r x — increase all integers on the segment from l to r by values x;
- 2 l r — find
, where f(x) is the x-th Fibonacci number. As this number may be large, you only have to find it modulo109 + 7.
In this problem we define Fibonacci numbers as follows: f(1) = 1, f(2) = 1, f(x) = f(x - 1) + f(x - 2) for all x > 2.
Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha?
The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of elements in the array and the number of queries respectively.
The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).
Then follow m lines with queries descriptions. Each of them contains integers tpi, li, ri and may be xi (1 ≤ tpi ≤ 2, 1 ≤ li ≤ ri ≤ n,1 ≤ xi ≤ 109). Here tpi = 1 corresponds to the queries of the first type and tpi corresponds to the queries of the second type.
It's guaranteed that the input will contains at least one query of the second type.
For each query of the second type print the answer modulo 109 + 7.
5 4
1 1 2 1 1
2 1 5
1 2 4 2
2 2 4
2 1 5
5
7
9 题意: 两个操作,1是把这个区间里的数都加x,2是求这个区间的和函数和,函数是斐波那契数列; 思路: 显然是一个线段树的题,不过维护的是矩阵,具体的可以看题解,写的太挫,跑了2000+ms; AC代码:
#include <bits/stdc++.h>
#define lson o<<1
#define rson o<<1|1
using namespace std;
typedef long long LL;
const int maxn=1e5+10;
const LL mod=1e9+7;
LL a[maxn];
struct matrix
{
LL a[2][2];
};
matrix add(matrix A,matrix B)
{
matrix C;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
C.a[i][j]=A.a[i][j]+B.a[i][j];
if(C.a[i][j]>=mod)C.a[i][j]-=mod;
}
}
return C;
}
matrix mul(matrix A,matrix B)
{
matrix C;
for(int i=0;i<2;i++)
{
for(int j=0;j<2;j++)
{
C.a[i][j]=0;
for(int k=0;k<2;k++)
{
C.a[i][j]+=A.a[i][k]*B.a[k][j];
C.a[i][j]%=mod;
}
}
}
return C;
}
matrix pow_mod(LL x)
{
matrix s,base;
s.a[0][0]=s.a[1][1]=1;s.a[0][1]=s.a[1][0]=0;
base.a[0][0]=base.a[0][1]=base.a[1][0]=1;base.a[1][1]=0;
while(x)
{
if(x&1)s=mul(s,base);
base=mul(base,base);
x>>=1;
}
return s;
} struct Tree
{
int l,r,mark;
matrix sum,fs;
}tr[4*maxn]; inline void pushup(int o)
{
tr[o].sum=add(tr[lson].sum,tr[rson].sum);
}
inline void pushdown(int o)
{
if(tr[o].mark)
{
tr[o].mark=0;tr[lson].mark=1;tr[rson].mark=1;
tr[lson].sum=mul(tr[lson].sum,tr[o].fs);tr[rson].sum=mul(tr[rson].sum,tr[o].fs);
tr[lson].fs=mul(tr[lson].fs,tr[o].fs);tr[rson].fs=mul(tr[rson].fs,tr[o].fs);
tr[o].fs.a[0][0]=tr[o].fs.a[1][1]=1;tr[o].fs.a[1][0]=tr[o].fs.a[0][1]=0;
}
}
void build(int o,int L ,int R)
{
tr[o].l=L;tr[o].r=R;tr[o].mark=0;
tr[o].fs.a[0][0]=tr[o].fs.a[1][1]=1;tr[o].fs.a[0][1]=tr[o].fs.a[1][0]=0;
if(L>=R)
{
tr[o].sum=pow_mod(a[L]);
return ;
}
int mid=(L+R)>>1;
build(lson,L,mid);
build(rson,mid+1,R);
pushup(o);
} LL query(int o,int L,int R)
{
//cout<<o<<" "<<L<<" "<<R<<endl;
if(L<=tr[o].l&&R>=tr[o].r)return tr[o].sum.a[0][0];
int mid=(tr[o].l+tr[o].r)>>1;
pushdown(o);
LL ans=0;
if(L<=mid)ans+=query(lson,L,R);
if(R>mid)ans+=query(rson,L,R);
pushup(o);
return ans%mod;
} void update(int o,int L,int R,matrix num)
{
if(L<=tr[o].l&&R>=tr[o].r)
{
tr[o].fs=mul(tr[o].fs,num);
tr[o].mark=1;
tr[o].sum=mul(tr[o].sum,num);
return ;
}
pushdown(o);
int mid=(tr[o].l+tr[o].r)>>1;
if(L<=mid)update(lson,L,R,num);
if(R>mid)update(rson,L,R,num);
pushup(o);
}
int n,m;
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)scanf("%I64d",&a[i]),a[i]--;
build(1,1,n);
int op,u,v;
LL temp;
while(m--)
{
scanf("%d",&op);
if(op==1)
{
scanf("%d%d%I64d",&u,&v,&temp);
matrix num=pow_mod(temp);
update(1,u,v,num);
}
else
{
scanf("%d%d",&u,&v);
printf("%I64d\n",query(1,u,v));
}
}
return 0;
}
codeforces 719E E. Sasha and Array(线段树)的更多相关文章
- Codeforces 719 E. Sasha and Array (线段树+矩阵运算)
题目链接:http://codeforces.com/contest/719/problem/E 题意:操作1将[l, r] + x; 操作2求f[l] + ... + f[r]; 题解:注意矩阵可以 ...
- [Codeforces 266E]More Queries to Array...(线段树+二项式定理)
[Codeforces 266E]More Queries to Array...(线段树+二项式定理) 题面 维护一个长度为\(n\)的序列\(a\),\(m\)个操作 区间赋值为\(x\) 查询\ ...
- Codeforces Round #373 (Div. 2) E. Sasha and Array 线段树维护矩阵
E. Sasha and Array 题目连接: http://codeforces.com/contest/719/problem/E Description Sasha has an array ...
- 【Codeforces718C】Sasha and Array 线段树 + 矩阵乘法
C. Sasha and Array time limit per test:5 seconds memory limit per test:256 megabytes input:standard ...
- 【codeforces 719E】Sasha and Array
[题目链接]:http://codeforces.com/contest/719/problem/E [题意] 给你一个数列,有两种操作1 l r x 给[l,r]区间上的数加上x, 2 l r 询问 ...
- CF719E. Sasha and Array [线段树维护矩阵]
CF719E. Sasha and Array 题意: 对长度为 n 的数列进行 m 次操作, 操作为: a[l..r] 每一项都加一个常数 C, 其中 0 ≤ C ≤ 10^9 求 F[a[l]]+ ...
- CF718C Sasha and Array 线段树+矩阵加速
正解:线段树 解题报告: 传送门! 首先这种斐波拉契,又到了1e9的范围,又是求和什么的,自然而然要想到矩阵加速昂 然后这里主要是考虑修改操作,ai+=x如果放到矩阵加速中是什么意思呢QAQ? 那不就 ...
- CF718C Sasha and Array 线段树 + 矩阵乘法
有两个操作: 将 $[l,r]$所有数 + $x$ 求 $\sum_{i=l}^{r}fib(i)$ $n=m=10^5$ 直接求不好求,改成矩阵乘法的形式: $a_{i}=M^x\times ...
- CF718C Sasha and Array [线段树+矩阵]
我们考虑线性代数上面的矩阵知识 啊呸,是基础数学 斐波那契的矩阵就不讲了 定义矩阵 \(f_x\) 是第 \(x\) 项的斐波那契矩阵 因为 \(f_i * f_j = f_{i+j}\) 然后又因为 ...
随机推荐
- eclipse debug 快捷键
简单的说下调试的快捷键: 1 F5:下一步,可以进入下一个函数栈 2 F6:当前函数的下一步,不会进入其他的函数. 3 F8:下一个断点. 4 也可以通过选中一个变量或者表达式,按ctrl+shift ...
- ASP.NET WebAPI 10 Action的选择(二)
在本系列的第二篇简要的讲述了Action的选择条件本篇深入讲述一下Action选择的过程在上一篇中我们已经讲到了Controller的激活过程中已经说到了设置Controller的Controller ...
- Ansible用于网络设备管理 part 3 使用NAPALM成品库
闲话 经过了这俩月的闲暇时间的瞎逛和瞎琢磨,我发现NAPALM是一条路,NAPALM是由帅哥David Barroso和美女Elisa Jasinska创建的一个项目,都是颜值高的技术牛人啊,真是不给 ...
- xCode删除storyboard,新建window并启动
application:didFinishLaunchingWithOptions该函数是应用程序启动之后首次加载页面的函数,删除storyboard之后,需要在这里new出新的window,初始化, ...
- swift GCD使用指南
swift GCD使用指南 Grand Central Dispatch(GCD)是异步执行任务的技术之一.一般将应用程序中记述的线程管理用的代码在系统级中实现.开发者只需要定义想执行的任务并追加到适 ...
- iOS-代理反向传值<转>
在上篇博客 iOS代理协议 中,侧重解析了委托代理协议的概念等,本文将侧重于它们在开发中的应用. 假如我们有一个需求如下:界面A上面有一个button.一个label.从界面A跳转到界面B,在界面B的 ...
- 插入排序(java版)
public class InsertSortTest{ public static void InsertSort(int[] source) { //默认第一个元素已排序 for (int i = ...
- Linux 下Firefox无法打开在'.domain'之前带有中划线的域名
问题 Linux系统下的Firefox无法打开在".domain"之前带有中划线的域名 eg:"http://su---.diandian.com/" 问题原因 ...
- Java 网络编程----基本概念
网络现在是一个非常普遍的概念. 以下是维基百科上的解释: 网络一词有多种意义,可解作: 网络流也简称为网络(network).一般用于管道系统.交通系统.通讯系统建模. 有时特指计算机网络. 或特指其 ...
- (传输层)UDP协议
目录 数据单位特点具体实现要求UDP首部格式发送UDP请求的客户端图释 数据单位 UDP 传送的数据单位协议是 UDP 报文或用户数据报 特点 UDP 是无连接的,即发送数据之前不需要建立连接 UDP ...