Summer Homework

CodeForces - 316E3

By the age of three Smart Beaver mastered all arithmetic operations and got this summer homework from the amazed teacher:

You are given a sequence of integers a1, a2, ..., an. Your task is to perform on it mconsecutive operations of the following type:

  1. For given numbers xi and vi assign value vi to element axi.
  2. For given numbers li and ri you've got to calculate sum , where f0 = f1 = 1 and at i ≥ 2: fi = fi - 1 + fi - 2.
  3. For a group of three numbers li ri di you should increase value ax by di for all x (li ≤ x ≤ ri).

Smart Beaver planned a tour around great Canadian lakes, so he asked you to help him solve the given problem.

Input

The first line contains two integers n and m (1 ≤ n, m ≤ 2·105) — the number of integers in the sequence and the number of operations, correspondingly. The second line contains n integers a1, a2, ..., an (0 ≤ ai ≤ 105). Then follow m lines, each describes an operation. Each line starts with an integer ti (1 ≤ ti ≤ 3) — the operation type:

  • if ti = 1, then next follow two integers xi vi (1 ≤ xi ≤ n, 0 ≤ vi ≤ 105);
  • if ti = 2, then next follow two integers li ri (1 ≤ li ≤ ri ≤ n);
  • if ti = 3, then next follow three integers li ri di (1 ≤ li ≤ ri ≤ n, 0 ≤ di ≤ 105).

The input limits for scoring 30 points are (subproblem E1):

  • It is guaranteed that n does not exceed 100, m does not exceed 10000 and there will be no queries of the 3-rd type.

The input limits for scoring 70 points are (subproblems E1+E2):

  • It is guaranteed that there will be queries of the 1-st and 2-nd type only.

The input limits for scoring 100 points are (subproblems E1+E2+E3):

  • No extra limitations.

Output

For each query print the calculated sum modulo 1000000000 (109).

Examples

Input
5 5
1 3 1 2 4
2 1 4
2 1 5
2 2 4
1 3 10
2 1 5
Output
12
32
8
50
Input
5 4
1 3 1 2 4
3 1 4 1
2 2 4
1 2 10
2 1 5
Output
12
45
sol:对于斐波那契数列,是有矩阵的递推公式的,搬一个讲的很好的blog
自己手撸一下,发现法2转移其实很好理解
如这样一个数列
1,2,3,4
斐波那契数列是1 1 2 3 5
S[0](1~2)是1*f[0]+2*f[1] S[1](1~2)是1*f[1]+2*f[2]
S[0](3~4)是3*f[0]+4*f[1] S[1](1~2)是3*f[1]+4*f[2]
转移S[0](1~4)是1*f[0]+2*f[1]+3*(f[0]*f[0]+f[1]*f[1])+4*(f[1]*f[0]+f[2]*f[1])
假如把这些看成矩阵乘法
这个例子太low了看个大一点的
数列1,2,3,4,5,6,7,8
S[0](1,8)直接看后面的
5*(f[0]*f[2]+f[1]*f[3])+6*(f[1]*f[2]+f[2]*f[3])+7*(f[2]*f[2]+f[3]*f[3])+8*(f[3]*f[2]+f[4]*f[3]) 然后机智的发现f[0]=f[1]=1,所以f[0]*f[2]+f[1]*f[3]=f[4] 容易知道f[0]*矩阵k=f[1] f[1]*矩阵k=f[2] 所以f[1]*f[2]+f[2]*f[3]就是f[4]*矩阵k=f[5] 容易发现f[0]*f[2]+f[1]*f[3]=f[4] f[1]*f[2]+f[2]*f[3]=f[5] 这样就做完了qaq
k=1 1
1 0
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
inline ll read()
{
ll s=; bool f=; char ch=' ';
while(!isdigit(ch)) {f|=(ch=='-'); ch=getchar();}
while(isdigit(ch)) {s=(s<<)+(s<<)+(ch^); ch=getchar();}
return (f)?(-s):(s);
}
#define R(x) x=read()
inline void write(ll x)
{
if(x<) {putchar('-'); x=-x;}
if(x<) {putchar(x+''); return;}
write(x/); putchar((x%)+'');
}
#define W(x) write(x),putchar(' ')
#define Wl(x) write(x),putchar('\n')
const int N=;
const ll Mod=;
int n,m;
ll a[N],f[N],fs[N];
inline int fei(int x){if(x<)return ;else return f[x];}
struct Node
{
ll le,S[],lazy;
}T[N<<];
#define c1 (x<<1)
#define c2 (x<<1|1)
inline ll Ad(ll x,ll y) {x+=y; x-=(x>=Mod)?Mod:; x+=(x<)?Mod:; return x;}
inline ll Ad(ll x,ll y,ll z){return Ad(Ad(x,y),z);}
inline ll Mul(ll x,ll y) {return 1ll*x*y%Mod;}
inline Node Merg(Node a,Node b)
{
Node ans;
ans.le=a.le+b.le; ans.lazy=;
ans.S[]=Ad(a.S[],Mul(b.S[],fei(a.le-)),Mul(b.S[],fei(a.le-)));
ans.S[]=Ad(a.S[],Mul(b.S[],fei(a.le-)),Mul(b.S[],fei(a.le)));
return ans;
}
inline void F5(Node &a,ll oo)
{
a.S[]=Ad(a.S[],Mul(oo,fs[a.le-]));
a.S[]=Ad(a.S[],Ad(Mul(oo,fs[a.le]),-oo));
}
inline void PushDown(int x)
{
if(!T[x].lazy) return;
T[c1].lazy=Ad(T[c1].lazy,T[x].lazy); F5(T[c1],T[x].lazy);
T[c2].lazy=Ad(T[c2].lazy,T[x].lazy); F5(T[c2],T[x].lazy);
T[x].lazy=;
}
inline void Build(int x,int l,int r)
{
T[x].le=r-l+; T[x].lazy=;
if(l==r)
{
T[x].S[]=T[x].S[]=a[l]; return;
}
int mid=(l+r)>>;
Build(c1,l,mid); Build(c2,mid+,r);
T[x]=Merg(T[c1],T[c2]);
}
inline void Chag(int x,int l,int r,int Pos,ll Val)
{
if(l==r)
{
T[x].S[]=T[x].S[]=Val; return;
}
PushDown(x);
int mid=(l+r)>>;
if(Pos<=mid) Chag(c1,l,mid,Pos,Val);
else Chag(c2,mid+,r,Pos,Val);
T[x]=Merg(T[c1],T[c2]);
}
inline Node Que(int x,int l,int r,int ql,int qr)
{
// cout<<l<<' '<<r<<' '<<ql<<' '<<qr<<" "<<T[x].S[0]<<endl;
if(ql==l&&qr==r) return T[x];
PushDown(x);
int mid=(l+r)>>;
if(qr<=mid) return Que(c1,l,mid,ql,qr);
else if(ql>mid) return Que(c2,mid+,r,ql,qr);
else return Merg(Que(c1,l,mid,ql,mid),Que(c2,mid+,r,mid+,qr));
T[x]=Merg(T[c1],T[c2]);
}
inline void Updata(int x,int l,int r,int ql,int qr,ll Val)
{
if(ql==l&&qr==r)
{
T[x].lazy=Ad(T[x].lazy,Val); F5(T[x],Val); return;
}
PushDown(x);
int mid=(l+r)>>;
if(qr<=mid) Updata(c1,l,mid,ql,qr,Val);
else if(ql>mid) Updata(c2,mid+,r,ql,qr,Val);
else Updata(c1,l,mid,ql,mid,Val),Updata(c2,mid+,r,mid+,qr,Val);
T[x]=Merg(T[c1],T[c2]);
}
int main()
{
int i;
R(n); R(m);
for(i=;i<=n;i++) R(a[i]);
f[]=f[]=; for(i=;i<=n;i++) f[i]=Ad(f[i-],f[i-]);
fs[]=; for(i=;i<=n;i++) fs[i]=Ad(fs[i-],f[i]);
Build(,,n);
// cout<<"!!!!"<<Que(1,1,n,4,4).S[0]<<endl;
// return 0;
while(m--)
{
int opt; ll x,y,z; R(opt); R(x); R(y);
if(opt==)
{
Chag(,,n,x,y);
}
else if(opt==)
{
if(x>y) swap(x,y);
Node ans=Que(,,n,x,y); Wl(ans.S[]);
}
else if(opt==)
{
R(z); Updata(,,n,x,y,z);
}
}
return ;
}
/*
Input
5 5
1 3 1 2 4
2 1 4
2 1 5
2 2 4
1 3 10
2 1 5
Output
12
32
8
50 Input
5 4
1 3 1 2 4
3 1 4 1
2 2 4
1 2 10
2 1 5
Output
12
45
*/

 

codeforces316E3的更多相关文章

  1. codeforces316E3 Summer Homework(线段树,斐波那契数列)

    题目大意 给定一个n个数的数列,m个操作,有三种操作: \(1\ x\ v\) 将\(a_x\)的值修改成v $2\ l\ r\ $ 求 \(\sum_{i=l}^r x_i*f_{i-l}\) 其中 ...

随机推荐

  1. 把.exe的格式的运行程序加到电脑本地服务的办法(本文来源于百度)

    Instsrv.exe(可以给系统安装和删除服务) Srvany.exe(可以让程序以服务的方式运行) 方法/步骤     要实现这个功能要用到微软提供的两个小工具“instsrv.exe”和“srv ...

  2. MyEclipse10.0 采用插件方式安装 SVN

    一.到官方上下载svn1.8.3,下载后的文件名叫site-1.8.3.zip 地址:http://subclipse.tigris.org/servlets/ProjectDocumentList? ...

  3. C#异步编程中的最佳实践(做法)

    原文地址Stephen Cleary 写得很详细,尤其讲到了 GUI 上下文调用,在APS.NET中它会阻塞 GUI 线程,从而导致死锁.而控制台中却不存在这个问题. 比如开发过程中本地写控制台程序测 ...

  4. 电脑无法上网,DNS出现fec0:0:0:ffff::1%1问题

    具体描述:qq,微信可用网,但其他不能用. 一.win+r 输入cmd 打开命令行:ipconfig /all 查看DNS 二.打开文本编辑器,输入如下文本: @Echo onpushd\window ...

  5. SpringBoot 中aop整合方法执行日志

    今天事情不多, 处理完手中的事边想着捣鼓一下AOP, 着手开始写才发现, 多久不用, 自己已经忘得差不多了, 捣鼓半天之后, 慢慢整出这个小demo,以便于以后查阅回顾 1 .先创建一个注解, 用来作 ...

  6. kali linux 安装 qq (deepin-wine)

    添加deeepin-wine 依赖 /etc/apt/sources.list: # Generated by deepin-installer deb http://mirrors.aliyun.c ...

  7. mysql 利用 case 批量更新

  8. phpstorm 习惯设置

    phpstorm 习惯设置 1. 字体:Source Code Pro 大小:14 链接: https://pan.baidu.com/s/1HLpbduBHFvbq1a10QV4uCg 提取码: y ...

  9. 前端基础(四):BOM和DOM

    前戏 到目前为止,我们已经学过了JavaScript的一些简单的语法.但是这些简单的语法,并没有和浏览器有任何交互. 也就是我们还不能制作一些我们经常看到的网页的一些交互,我们需要继续学习BOM和DO ...

  10. tigervnc-server安装

    #vncserver安装方法 #su - root rpm -ivh tigervnc-server-1.8.0-13.el7.x86_64.rpm cp /lib/systemd/system/vn ...