【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers
我这种maintain写法好zz。考试时获得了40pts的RE好成绩
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.
Input
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.
Output
For each query of the second type, print the value of the sum on a single line.
题目分析
注意到形如Fib的数列$a_{n+2}=a_{n+1}+a_n$有相当好的性质。
- $a_n=F_{n−2}a_1+F_{n−1}a_2$
- $\sum_{i=1}^na_i=a_{n+2}−a_2$
第一条可以用数学归纳法证明;第二条就是将$2\sum_{i=1}^na_i$展开,得到$\sum_{i=1}^na_i+a_{n+2}-a_2$.
回到这一道题上,利用了这两条性质,那么对于每一个修改的区间只需要保留区间前两项增加的Fib值就可以记录下这个操作。所以现在就可以用线段树来维护这一系列询问了。
#include<bits/stdc++.h>
#define MO 1000000009
typedef long long ll;
const int maxn = ; struct node
{
ll tag1,tag2,sum;
}f[maxn<<];
int n,m;
ll sum[maxn],fib[maxn]; int read()
{
char ch = getchar();
int num = , fl = ;
for (; !isdigit(ch); ch=getchar())
if (ch=='-') fl = -;
for (; isdigit(ch); ch=getchar())
num = (num<<)+(num<<)+ch-;
return num*fl;
}
void maintain(int rt, int lens)
{
f[rt].tag1 %= MO, f[rt].tag2 %= MO;
f[rt].sum = f[rt<<].sum+f[rt<<|].sum;
f[rt].sum = (f[rt].sum+f[rt].tag1*fib[lens]+f[rt].tag2*fib[lens+]-f[rt].tag2)%MO;
}
void pushdown(int rt, int l, int r)
{
if (!f[rt].tag1&&!f[rt].tag2) return;
int mid = (l+r)>>, ls = rt<<, rs = rt<<|;
f[ls].tag1 += f[rt].tag1, f[ls].tag2 += f[rt].tag2;
f[rs].tag1 += f[rt].tag1*fib[mid-l]+f[rt].tag2*fib[mid-l+];
f[rs].tag2 += f[rt].tag1*fib[mid-l+]+f[rt].tag2*fib[mid-l+];
f[rt].tag1 = f[rt].tag2 = ;
maintain(ls, mid-l+);
maintain(rs, r-mid);
}
void modify(int rt, int L, int R, int l, int r)
{
if (L <= l&&r <= R){
f[rt].tag1 += fib[l-L+];
f[rt].tag2 += fib[l-L+];
maintain(rt, r-l+);
return;
}
int mid = (l+r)>>;
pushdown(rt, l, r);
if (L <= mid) modify(rt<<, L, R, l, mid);
if (R > mid) modify(rt<<|, L, R, mid+, r);
maintain(rt, r-l+);
}
ll query(int rt, int L, int R, int l, int r)
{
if (L <= l&&r <= R) return f[rt].sum;
pushdown(rt, l, r);
int mid = (l+r)>>;
ll ret = ;
if (L <= mid) ret += query(rt<<, L, R, l, mid);
if (R > mid) ret += query(rt<<|, L, R, mid+, r);
return ret%MO;
}
int main()
{
n = read(), m = read(), fib[] = fib[] = ;
for (int i=; i<=n; i++) sum[i] = (sum[i-]+read())%MO;
for (int i=; i<=; i++) fib[i] = (fib[i-]+fib[i-])%MO;
for (int i=; i<=m; i++)
{
int opt = read(), l = read(), r = read();
if (opt==) modify(, l, r, , n);
else printf("%lld\n",(query(, l, r, , n)+(sum[r]-sum[l-])%MO+MO)%MO);
}
return ;
}
END
【思维题 线段树】cf446C. DZY Loves Fibonacci Numbers的更多相关文章
- cf446C DZY Loves Fibonacci Numbers
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- CF446C DZY Loves Fibonacci Numbers 线段树 + 数学
有两个性质需要知道: $1.$ 对于任意的 $f[i]=f[i-1]+f[i-2]$ 的数列,都有 $f[i]=fib[i-2]\times f[1]+fib[i-1]\times f[2]$ 其中 ...
- codeforces 446C DZY Loves Fibonacci Numbers(数学 or 数论+线段树)(两种方法)
In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence relation F1 ...
- Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
- ACM学习历程—Codeforces 446C DZY Loves Fibonacci Numbers(线段树 && 数论)
Description In mathematical terms, the sequence Fn of Fibonacci numbers is defined by the recurrence ...
- Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)
题目:DZY Loves Fibonacci Numbers 题意比較简单,不解释了. 尽管官方的题解也是用线段树,但还利用了二次剩余. 可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是 ...
- codeforces 446C DZY Loves Fibonacci Numbers 数论+线段树成段更新
DZY Loves Fibonacci Numbers Time Limit:4000MS Memory Limit:262144KB 64bit IO Format:%I64d &a ...
- 「CF446C」 DZY Loves Fibonacci Numbers
「CF446C」 DZY Loves Fibonacci Numbers 这里提供一种优美的根号分治做法. 首先,我们考虑一种不太一样的暴力.对于一个区间加斐波那契数的操作 \([a,b]\),以及一 ...
- Codeforces446C - DZY Loves Fibonacci Numbers
Portal Description 给出一个\(n(n\leq3\times10^5)\)个数的序列,进行\(m(m\leq3\times10^5)\)次操作,操作有两种: 给区间\([L,R]\) ...
随机推荐
- Prefix.pch文件的用法
我们知道,每新建立一个工程,比如说HelloWord,在分类SupportingFiles里都会有一个以工程名开头-Prefix.pch结尾的文件,如HelloWord-Prefix.pch.对于这个 ...
- jquery jtemplates.js模板渲染引擎的详细用法第三篇
jquery jtemplates.js模板渲染引擎的详细用法第三篇 <span style="font-family:Microsoft YaHei;font-size:14px;& ...
- 如何删除.DS_Store文件?
.DS_Store出现在Desktop和其它地方,看它碍眼,它是什么,详见百度百科 http://baike.baidu.com/link?url=yLTDHR6OS66-981wpCY-mWPF7a ...
- 译—— a tale of viewport2
这一页我们将讨论移动浏览器.如果您对移动设备完全陌生,我建议您首先阅读第一部分关于桌面浏览器的内容,以便在熟悉的环境中做好准备. 移动浏览器的问题 移动浏览器和桌面浏览器比较,最明显的差异是屏幕大小. ...
- PAT甲级——1103 Integer Factorization (DFS)
本文同步发布在CSDN:https://blog.csdn.net/weixin_44385565/article/details/90574720 1103 Integer Factorizatio ...
- CodeForces - 581B-Luxurious Houses
The capital of Berland has n multifloor buildings. The architect who built up the capital was very c ...
- 牛客网练习赛34-D-little w and Exchange(思维题)
链接:https://ac.nowcoder.com/acm/contest/297/D 来源:牛客网 时间限制:C/C++ 1秒,其他语言2秒 空间限制:C/C++ 262144K,其他语言5242 ...
- replcation set (复制集)配置过程 --mongodb
一,配置规划 复制集原理(基本构成是1主2从的结构,自带互相监控投票机制(Raft(MongoDB) Paxos(mysql MGR 用的是变种))如果发生主库宕机,复制集内部会进行投票选举,选择一 ...
- 获取spring里的bean
ClassPathXmlApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring. ...
- 为什么HDFS的副本数通常选择3?
HDFS采用一种称为机架感知的策略来改进数据的可靠性.可用性和网络带宽的利用率. 在大多数情况下,HDFS的副本系数是3,HDFS的存放策略是一个副本存放在本地机架节点上,另一个副本存放在同一机架的另 ...