CF446C DZY Loves Fibonacci Numbers 万能的线段树
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation
F1 = 1; F2 = 1; Fn = Fn - 1 + Fn - 2 (n > 2).
DZY loves Fibonacci numbers very much. Today DZY gives you an array consisting of n integers: a1, a2, ..., an. Moreover, there are mqueries, each query has one of the two types:
- Format of the query "1 l r". In reply to the query, you need to add Fi - l + 1 to each element ai, where l ≤ i ≤ r.
- Format of the query "2 l r". In reply to the query you should output the value of modulo 1000000009 (109 + 9).
Help DZY reply to all the queries.
The first line of the input contains two integers n and m (1 ≤ n, m ≤ 300000). The second line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109) — initial array a.
Then, m lines follow. A single line describes a single query in the format given in the statement. It is guaranteed that for each query inequality 1 ≤ l ≤ r ≤ n holds.
For each query of the second type, print the value of the sum on a single line.
4 41 2 3 41 1 42 1 41 2 42 1 3
1712
After the first query, a = [2, 3, 5, 7].
For the second query, sum = 2 + 3 + 5 + 7 = 17.
After the third query, a = [2, 4, 6, 9].
For the fourth query, sum = 2 + 4 + 6 = 12.
由于两个fib数列相加还是fib数列,可以通过维护这个区间的fib前两项来做区间加法。
1 #include <cmath> 2 #include <queue> 3 #include <cstdio> 4 #include <cstring> 5 #include <iostream> 6 #include <algorithm> 7 #define ll long long 8 using namespace std; 9 const int inf = 0x3f3f3f3f; 10 const int maxn = 300005; 11 const int mod = 1e9+9; 12 ll t[maxn<<2], f[maxn], num[maxn]; 13 int lazyl[maxn<<2], lazyr[maxn<<2]; 14 void update(int x, int l, int r, int a, int b, int c, int d); 15 int fib(int a, int b, int n)//求f(0)=a,f(1)=b的斐波那契数列第n项 16 { 17 if (n==0) return a; 18 if (n==1) return b; 19 return (a*f[n-1]%mod+b*f[n]%mod)%mod; 20 } 21 void build(int x, int l, int r) 22 { 23 lazyl[x]=lazyr[x]=0; 24 if (l==r) { 25 t[x]=num[l]; 26 return ; 27 } 28 int mid=(l+r)/2; 29 build(x<<1, l, mid); 30 build(x<<1|1, mid+1, r); 31 t[x]=(t[x<<1]+t[x<<1|1])%mod; 32 } 33 void push_down(int x, int l, int r) 34 { 35 int mid=(l+r)/2; 36 if (lazyl[x]||lazyr[x]) { 37 update(x<<1, l, mid, l, r, lazyl[x], lazyr[x]); 38 update(x<<1|1, mid+1, r, l, r, lazyl[x], lazyr[x]); 39 lazyl[x]=0; 40 lazyr[x]=0; 41 } 42 } 43 void update(int x, int l, int r, int a, int b, int c, int d) 44 { 45 if (a<=l&&b>=r) { 46 int nc, nd; 47 nc=fib(c, d, l-a); 48 nd=fib(c, d, l-a+1); 49 c=nc; d=nd; 50 lazyl[x]=(lazyl[x]+c)%mod; lazyr[x]=(d+lazyr[x])%mod; 51 t[x]=(t[x]+fib(c, d, r-l+2)-d)%mod;//f(0)=a,f(1)=b的前n项和∑f(i),i<n,等于f(n+1)-b 52 return ; 53 } 54 push_down(x, l, r); 55 int mid=(l+r)/2; 56 if (a<=mid) update(x<<1, l, mid, a, b, c, d); 57 if (b>mid) update(x<<1|1, mid+1, r, a, b, c, d); 58 t[x]=(t[x<<1]+t[x<<1|1])%mod; 59 } 60 ll query(int x, int l, int r, int a, int b) 61 { 62 if (a<=l&&b>=r) return t[x]; 63 push_down(x, l, r); 64 int mid=(l+r)/2; 65 ll ret=0; 66 if (a<=mid) ret+=query(x<<1, l, mid, a, b); 67 if (b>mid) ret+=query(x<<1|1, mid+1, r, a, b); 68 return ret; 69 } 70 int main() { 71 int n, m, op, l, r; 72 f[1]=f[2]=1; 73 scanf("%d%d", &n, &m); 74 for (int i=1; i<=n; i++) { 75 scanf("%I64d", &num[i]); 76 } 77 for (int i=3; i<n+5; i++) 78 f[i]=(f[i-1]+f[i-2])%mod; 79 build(1, 1, n); 80 while (m--) { 81 scanf("%d%d%d", &op, &l, &r); 82 if (op==1) { 83 update(1, 1, n, l, r, 1, 1); 84 } else { 85 printf("%I64d\n", (query(1, 1, n, l, r)%mod+mod)%mod); 86 } 87 } 88 return 0; 89 }
View Code
转载于:https://www.cnblogs.com/mandora/p/3842201.html
CF446C DZY Loves Fibonacci Numbers 万能的线段树相关推荐
- [CF446C]DZY Loves Fibonacci Numbers
Description: 给出一个数列,每次可以选取一个区间,按顺序加上第i个Fibonacci Numbers(斐波那契数)进行更新,也可以查询某一个区间的总和. Hint: \(n \le 3*1 ...
- Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)
题目:DZY Loves Fibonacci Numbers 题意比較简单,不解释了. 尽管官方的题解也是用线段树,但还利用了二次剩余. 可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是 ...
- Codeforces 446C. DZY Loves Fibonacci Numbers【斐波那契+线段树】
C. DZY Loves Fibonacci Numbers [题目描述] 传送门 [题解] 我们可以知道斐波那契数列有两个性质: ∑i=1nFi=Fn+2−F2\sum_{i=1}^{n} F_i= ...
- C. DZY Loves Fibonacci Numbers(线段树fibonacci)
C. DZY Loves Fibonacci Numbers(线段树&fibonacci) 考虑fibonaccifibonaccifibonacci的几个性质: 两个的广义斐波那契数列和仍是 ...
- Codeforces 446C. DZY Loves Fibonacci Numbers (Fibonacci + 线段树)
Description In mathematical terms, the sequence F n of Fibonacci numbers is defined by the recurrenc ...
- Codeforces446C - DZY Loves Fibonacci Numbers
Portal Description 给出一个\(n(n\leq3\times10^5)\)个数的序列,进行\(m(m\leq3\times10^5)\)次操作,操作有两种: 给区间\([L,R]\) ...
- Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers
參考:http://www.cnblogs.com/chanme/p/3843859.html 然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K ...
- 【hdu5266】pog loves szh III (LCA+线段树)
题意:给一颗树,Q次询问L,L+1,L+2...R的LCA 题目传送门 以LCA为权建线段树,直接查询即可 (我用树剖找LCA) 代码: #include<iostream> #inclu ...
- 线段树之单点更新,区域求和
线段树之单点更新,区域求和 今天在coreforces上做的一题 E. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory ...
- CodeForces - 1311F Moving Points(线段树+离散化)
题目链接:点击查看 题目大意:给出 x 轴上的 n 个点,每个点都有一个位置和一个速度,每个点会根据速度在 x 轴上移动,现在规定dis( x , y )为点 x 和点 y 在移动过程中的最小距离,我 ...
最新文章
- eclipse中查看java源代码设置方法
- LINQ to SQL 在 Visual Studio 2008 中的简单应用
- 【Linux指标】内存篇
- 页面中color颜色值_HTML+CSS 基础知识-入门概括-颜色与单位
- SQL基础【十七、uuid()、sys_guid()、newid()】
- 迭代加深搜索与埃及分数求解
- 使用url参数传递SAP Analytics Cloud filter的一个例子
- ffmpeg builds by zeranoe_FFmpeg
- 【APICloud系列|14】xcode下载地址
- 统计学习方法(—)——统计学习方法概念
- 把文化全交给HR,是管理者最大的过失
- Linux 30年专访:Linus Torvalds谈Linux内核开发与Git
- 强制应用 AMP 工具,开发者欲“封杀” Google!
- C++内存机制中内存溢出、内存泄露、内存越界和栈溢出的区别和联系
- 关闭戴尔增霸卡!!!
- matlab求3db函数,数字信号处理第二章习题26、30、32解答(包括matlab运行程序)
- 怎么制作你的第一个机器人
- H5如何获取内网IP和公网IP
- 如何设置Ubuntu键盘输入法框架为fcitx
- Qt ApplicationAttribute/WidgetAttribute 程序级别属性
热门文章
- 微信连wifi3.1总结
- 单片机复位电路,隐藏着这么多门道
- 数据分析师之路-数据埋点
- SQL Server高级编程
- win7资源管理器总是崩溃
- win2008R2 不能访问局域网共享\局域网共享中无本机,解决办法
- win 2008R2启用TLS 1.2 Windows 2008/2008R2手动启用TLS1_2协议教程
- 使用批处理解决U盘内出现的同名文件夹EXE病毒问题
- [W806捣鼓手记]FPU性能简单测试——2022.05.23
- Elasticsearch:在华为大数据集群 从c80升级至651版本后,执行es命令修改配置失败