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
Initially, array a is equal to 1, 1, 2, 1, 1.
The answer for the first query of the second type is f(1) + f(1) + f(2) + f(1) + f(1) = 1 + 1 + 1 + 1 + 1 = 5.
After the query 1 2 4 2 array a is equal to 1, 3, 4, 3, 1.
The answer for the second query of the second type is f(3) + f(4) + f(3) = 2 + 3 + 2 = 7.
The answer for the third query of the second type is f(1) + f(3) + f(4) + f(3) + f(1) = 1 + 2 + 3 + 2 + 1 = 9.
分析:线段树维护矩阵;
代码:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <algorithm>
#include <climits>
#include <cstring>
#include <string>
#include <set>
#include <map>
#include <queue>
#include <stack>
#include <vector>
#include <list>
#define rep(i,m,n) for(i=m;i<=n;i++)
#define rsp(it,s) for(set<int>::iterator it=s.begin();it!=s.end();it++)
#define mod 1000000007
#define inf 0x3f3f3f3f
#define vi vector<int>
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define ll long long
#define pi acos(-1.0)
#define pii pair<int,int>
#define Lson L, mid, rt<<1
#define Rson mid+1, R, rt<<1|1
const int maxn=1e5+;
using namespace std;
ll gcd(ll p,ll q){return q==?p:gcd(q,p%q);}
ll qpow(ll p,ll q){ll f=;while(q){if(q&)f=f*p%mod;p=p*p%mod;q>>=;}return f;}
int n,m,k,t,a[maxn];
struct Matrix
{
int a[][];
Matrix()
{
memset(a,,sizeof(a));
}
void init()
{
for(int i=;i<;i++)
for(int j=;j<;j++)
a[i][j]=(i==j);
}
Matrix operator + (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int j=;j<;j++)
C.a[i][j]=(a[i][j]+B.a[i][j])%mod;
return C;
}
Matrix operator * (const Matrix &B)const
{
Matrix C;
for(int i=;i<;i++)
for(int k=;k<;k++)
for(int j=;j<;j++)
C.a[i][j]=(C.a[i][j]+1LL*a[i][k]*B.a[k][j])%mod;
return C;
}
Matrix operator ^ (const int &t)const
{
Matrix A=(*this),res;
res.init();
int p=t;
while(p)
{
if(p&)res=res*A;
A=A*A;
p>>=;
}
return res;
}
};
Matrix f;
struct Node
{
Matrix sum,lazy;
} T[maxn<<]; void PushUp(int rt)
{
T[rt].sum = T[rt<<].sum + T[rt<<|].sum;
} void PushDown(int L, int R, int rt)
{
int mid = (L + R) >> ;
Matrix t = T[rt].lazy;
T[rt<<].sum = t * T[rt<<].sum;
T[rt<<|].sum = t * T[rt<<|].sum;
T[rt<<].lazy = t * T[rt<<].lazy;
T[rt<<|].lazy = t *T[rt<<|].lazy;
T[rt].lazy.init();
} void Build(int L, int R, int rt)
{
T[rt].lazy.init();
if(L == R)
{
T[rt].sum=f^(a[L]-);
return ;
}
int mid = (L + R) >> ;
Build(Lson);
Build(Rson);
PushUp(rt);
} void Update(int l, int r, Matrix v, int L, int R, int rt)
{
if(l==L && r==R)
{
T[rt].lazy = v * T[rt].lazy;
T[rt].sum = v * T[rt].sum;
return ;
}
int mid = (L + R) >> ;
if(T[rt].lazy.a[][]||T[rt].lazy.a[][])PushDown(L, R, rt);
if(r <= mid) Update(l, r, v, Lson);
else if(l > mid) Update(l, r, v, Rson);
else
{
Update(l, mid, v, Lson);
Update(mid+, r, v, Rson);
}
PushUp(rt);
} ll Query(int l, int r, int L, int R, int rt)
{
if(l==L && r== R)
{
return T[rt].sum.a[][];
}
int mid = (L + R) >> ;
if(T[rt].lazy.a[][]||T[rt].lazy.a[][]) PushDown(L, R, rt);
if(r <= mid) return Query(l, r, Lson);
else if(l > mid) return Query(l, r, Rson);
return (Query(l, mid, Lson) + Query(mid + , r, Rson))%mod;
}
struct node
{
int a,x,y,z;
}op[maxn];
void init()
{
f.a[][]=,f.a[][]=;
f.a[][]=,f.a[][]=;
}
int main()
{
int i,j;
init();
scanf("%d%d",&n,&m);
rep(i,,n)scanf("%d",&a[i]);
Build(,n,);
rep(i,,m)
{
scanf("%d",&op[i].a);
if(op[i].a==)
{
scanf("%d%d%d",&op[i].x,&op[i].y,&op[i].z);
Update(op[i].x,op[i].y,f^op[i].z,,n,);
}
else
{
scanf("%d%d",&op[i].x,&op[i].y);
printf("%lld\n",Query(op[i].x,op[i].y,,n,));
}
}
//system("Pause");
return ;
}
Sasha and 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 E. Sasha and Array(线段树)
题目链接: E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input sta ...
- 【codeforces 718 C&D】C. Sasha and Array&D. Andrew and Chemistry
C. Sasha and Array 题目大意&题目链接: http://codeforces.com/problemset/problem/718/C 长度为n的正整数数列,有m次操作,$o ...
- CF719E. Sasha and Array [线段树维护矩阵]
CF719E. Sasha and Array 题意: 对长度为 n 的数列进行 m 次操作, 操作为: a[l..r] 每一项都加一个常数 C, 其中 0 ≤ C ≤ 10^9 求 F[a[l]]+ ...
- 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 ...
- Codeforces Round #373 (Div. 2) E. Sasha and Array 矩阵快速幂+线段树
E. Sasha and Array time limit per test 5 seconds memory limit per test 256 megabytes input standard ...
- 【题解】[CF718C Sasha and Array]
[题解]CF718C Sasha and Array 对于我这种喜欢写结构体封装起来的选手这道题真是太对胃了\(hhh\) 一句话题解:直接开一颗线段树的矩阵然后暴力维护还要卡卡常数 我们来把\(2 ...
- E. Sasha and Array 矩阵快速幂 + 线段树
E. Sasha and Array 这个题目没有特别难,需要自己仔细想想,一开始我想了一个方法,不对,而且还很复杂,然后lj提示了我一下说矩阵乘,然后再仔细想想就知道怎么写了. 这个就是直接把矩阵放 ...
- 718C Sasha and Array
传送门 题目 Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be ...
随机推荐
- Ubuntu上用mod_wsgi部署Django出现的一些问题
1 编码问题 直接运行Django没问题,但通过Apache+mod_wsgi上传中文字符的文件时出错: UnicodeEncodeError: 'ascii' codec can't encode ...
- ZUFE OJ 2289 God Wang II
Description 这个世界太无聊了,于是God Wang想出了新的运算符号$,对于两个数x,y来说x$y的值等于x和y各个位置上的数字乘积之和,没有的位按0来算 比如说123$321=1*3+2 ...
- Redis常用API-使用文档
一.Redis Client介绍 1.1.简介 Jedis Client是Redis官网推荐的一个面向java客户端,库文件实现了对各类API进行封装调用. Jedis源码工程地址:https://g ...
- Linux raid信息 查看
Linux下查看软.硬raid信息的方法. 软件raid:只能通过Linux系统本身来查看 cat /proc/mdstat 可以看到raid级别,状态等信息. 硬件raid: 最佳的办法是通过已安装 ...
- 数据库NULL和 ‘’ 区别
NULL判断时 : IS NOT NULL ''判断时: !=''
- Asp.Net调用Office组件操作时的DCOM配置 (转)
Asp.Net调用Office组件操作时的DCOM配置 http://blog.csdn.net/gz775/article/details/6447758 在项目中将数据导出为Excel格式时出现“ ...
- IOS tableViewCell单元格重用中的label重叠的问题
参考:http://zhidao.baidu.com/link?url=_oMUTo5SxUY6SBaxYLsIpN3i2sZ6SKG35MVlPJd2cNmUf9TGQFkKXX9EXwSwti0n ...
- Java学习笔记之static
1.static可以用于修饰成员变量.方法以及块,并不会改变类中成员的权限修饰,如:private修饰的成员变量,类外只能类名或非私有方法调用,而不能使用对象名调用. 2.static方法和成员变量, ...
- HDU 5652 India and China Origins 二分优化+BFS剪枝
题目大意:给你一个地图0代表可以通过1代表不可以通过.只要能从第一行走到最后一行,那么中国与印度是可以联通的.现在给你q个点,每年风沙会按顺序侵蚀这个点,使改点不可通过.问几年后中国与印度不连通.若一 ...
- sql高级篇(一)
1.select top mysql中: select * from persons limit 5; 相当于oracle中的: select * from persons <=5; 在翻页中经 ...