Discription

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 allx (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 最近好多 数学 + 数据结构 的题啊qwq,既要动脑子又要调代码qwq
斐波那契数列有一个非常好的性质: f[i] = f[i-1] + f[i-2]
这不是废话吗。。。。但是这个能推出来另一个东西 -> f[i] = f[k]*f[i-k] + f[k-1]*f[i-k-1] (这个式子是对于f[0]=f[1]=1的斐波那契数列而言的)。
有了这个,我们就可以随心所欲的通过 ∑f[i]*a[i+l] 和 ∑f[i+1]*a[i+l] 来构造任意 ∑f[i+d]*a[i+l] 啦(也就是程序中的Get函数)。 然后随便打打标机(别忘了下传维护之类的),随便合并合并子树,就可以A啦(但是要写一坨子qwq)
(最近压行成瘾qwq)
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=2*1e5,ha=1e9;
int f[maxn+5],F[maxn+5],a[maxn+5],le,ri,n,m,w,O;
int sum[maxn*4+5][2],tag[maxn*4+5],L[maxn*4+5],A;
inline void ADD(int &x,int y){ x+=y; if(x>=ha) x-=ha;}
inline int add(int x,int y){ x+=y; return x>=ha?x-ha:x;}
inline void init(){ f[0]=F[0]=1; for(int i=1;i<=maxn;i++) f[i]=add(f[i-1],f[i-2]),F[i]=add(f[i],F[i-1]);}
inline int Get(int o,int Derta){ return Derta<2?(Derta?sum[o][1]:sum[o][0]):add(sum[o][0]*(ll)f[Derta-2]%ha,sum[o][1]*(ll)f[Derta-1]%ha);}
inline void QADD(int o,int Derta){ ADD(tag[o],Derta),ADD(sum[o][0],Derta*(ll)F[L[o]-1]%ha),ADD(sum[o][1],Derta*(ll)add(F[L[o]],ha-1)%ha);}
inline void pushdown(int o,int lc,int rc){ if(tag[o]){ QADD(lc,tag[o]),QADD(rc,tag[o]),tag[o]=0;}}
inline void Merge(int o,int lc,int rc,int Derta){
sum[o][0]=add(sum[lc][0],Get(rc,Derta));
sum[o][1]=add(sum[lc][1],Get(rc,Derta+1));
} void build(int o,int l,int r){
L[o]=r-l+1;
if(l==r){ sum[o][0]=sum[o][1]=a[l]; return;}
int mid=l+r>>1,lc=o<<1,rc=(o<<1)|1;
build(lc,l,mid);
build(rc,mid+1,r);
Merge(o,lc,rc,mid+1-l);
} void update1(int o,int l,int r){
if(l==r){ sum[o][0]=sum[o][1]=w; return;}
int mid=l+r>>1,lc=o<<1,rc=(o<<1)|1;
pushdown(o,lc,rc);
if(le<=mid) update1(lc,l,mid);
else update1(rc,mid+1,r);
Merge(o,lc,rc,mid+1-l);
} void update2(int o,int l,int r){
if(l>=le&&r<=ri){ QADD(o,w); return;}
int mid=l+r>>1,lc=o<<1,rc=(o<<1)|1;
pushdown(o,lc,rc);
if(le<=mid) update2(lc,l,mid);
if(ri>mid) update2(rc,mid+1,r);
Merge(o,lc,rc,mid+1-l);
} void query(int o,int l,int r){
if(l>=le&&r<=ri){ ADD(A,Get(o,l-le)); return;}
int mid=l+r>>1,lc=o<<1,rc=(o<<1)|1;
pushdown(o,lc,rc);
if(le<=mid) query(lc,l,mid);
if(ri>mid) query(rc,mid+1,r);
} inline void solve(){
while(m--){
scanf("%d",&O);
if(O==1){ scanf("%d%d",&le,&w),update1(1,1,n);}
else if(O==2){ scanf("%d%d",&le,&ri),A=0,query(1,1,n),printf("%d\n",A);}
else{ scanf("%d%d%d",&le,&ri,&w),update2(1,1,n);}
}
} int main(){
init();
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",a+i);
build(1,1,n);
solve();
return 0;
}

  

 

CodeForces - 316E3 Summer Homework的更多相关文章

  1. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

  2. CF 316E3 Summer Homework(斐波那契矩阵+线段树)

    题目链接:http://codeforces.com/problemset/problem/316/E3 题意:一个数列A三种操作:(1)1 x y将x位置的数字修改为y:(2)2 x y求[x,y] ...

  3. Codeforces 316E3 线段树 + 斐波那切数列 (看题解)

    最关键的一点就是 f[ 0 ] * a[ 0 ] + f[ 1 ] * a[ 1 ] + ... + f[ n - 1] * a[ n  - 1] f[ 1 ] * a[ 0 ] + f[ 2 ] * ...

  4. Magolor的数据结构作业

    \(CodeForces 706E ~Working routine\) 给出一个矩阵,每次操作交换两个子矩阵,求最后状态. 使用链表存储,每次交换后,影响到的之后矩阵边缘的指针,暴力修改. \(~~ ...

  5. codeforces316E3

    Summer Homework CodeForces - 316E3 By the age of three Smart Beaver mastered all arithmetic operatio ...

  6. Codeforces 437A The Child and Homework

    题目链接:Codeforces 437A The Child and Homework 少看了一个条件,最后被HACK掉到203名,要不然就冲到100多一点了==.. 做这个题收获最大的是英语,A t ...

  7. Codeforces Round #371 (Div. 2) B. Filya and Homework 水题

    B. Filya and Homework 题目连接: http://codeforces.com/contest/714/problem/B Description Today, hedgehog ...

  8. Codeforces Round #371 (Div. 2)B. Filya and Homework

    题目链接:http://codeforces.com/problemset/problem/714/B 题目大意: 第一行输入一个n,第二行输入n个数,求是否能找出一个数x,使得n个数中的部分数加上x ...

  9. Codeforces Round #250 (Div. 2)—A. The Child and Homework

         好题啊,被HACK了.曾经做题都是人数越来越多.这次比赛 PASS人数 从2000直掉 1000人  被HACK  1000多人! ! ! ! 没见过的科技啊 1 2 4 8 这组数 被黑的 ...

随机推荐

  1. volley框架使用

    volley网络请求一个json数据很简单,一句话就搞定了. StringRequest stringRequest=new StringRequest(url, new Listener<St ...

  2. Redis实现之字符串

    简单动态字符串 Redis中的字符串并不是传统的C语言字符串(即字符数组,以下简称C字符串),而是自己构建了一种简单动态字符串(simple dynamic string,SDS),并将SDS作为Re ...

  3. 洛谷P1079 Vigenère 密码

    题目链接:https://www.luogu.org/problemnew/show/P1079

  4. loj2537 「PKUWC 2018」Minimax

    pkusc 快到了--做点题涨涨 rp. 初见时 yy 了一个类似于归并的东西,\(O(n^2)\),50 分. 50 分 yy 做法 对于一个点,枚举他能到达的权值(假设这个权值在左子树,在右子树是 ...

  5. Pipenv 学习笔记

    个人笔记,胡言乱语.并不是什么教学向文章.. 前言 在学习了 Python.Java 后,会发现 Java 有很成熟的项目构建工具,以前是使用 xml 的 Maven,现在又出现了使用 groovy ...

  6. (转载)CentOS 6.5使用aliyun镜像来源

    (原地址:http://www.linuxidc.com/Linux/2014-09/106675.htm) 当我们把CentOS 6.5安装好以后,可以使用这个脚本来使用国内的阿里云镜像源 #!/b ...

  7. java实现图的深度优先遍历和广度优先遍

    首先需要知道的是,图的深度优先遍历是一种类似于树的前序遍历方式,即选择一个入口节点,沿着这个节点一直遍历下去,直至所有节点都被访问完毕:如果说,图的深度优先遍历类似于树的前序遍历的话,那么图的广度优先 ...

  8. html5中checkbox的选中状态的设置与获取

    获取checkbox是否选中: $("#checkbox").is(":checked"); 获得的值为true或false. 设置checkbox是否选中: ...

  9. 【bzoj4386】[POI2015]Wycieczki 矩阵乘法

    题目描述 给定一张n个点m条边的带权有向图,每条边的边权只可能是1,2,3中的一种.将所有可能的路径按路径长度排序,请输出第k小的路径的长度,注意路径不一定是简单路径,即可以重复走同一个点. 输入 第 ...

  10. Markdown语法图解

    Markdown语法图解 文章目录 快捷键 基本语法 对字体设置斜体.粗体.删除线 分级标题 链接 分割线 代码块 引用 列表 表格 常用技巧 换行 缩进字符 如何打出一些特殊符号 字体.字号与颜色 ...