【思维题 线段树】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]\) ...
随机推荐
- Jeasyui的datagrid前端分页要点
Jeasyui的分页有两种方式: 1. 服务器端分页,是真正的分页,datagridview的pager会自动把pageSize和pageNum传到后台,后台根据根据pageSize和pageNum构 ...
- JS 对象的操作方法
第一种: 变量名.style.属性: 第二种: 变量名.style[参数]
- <pre></pre>标签自动换行
原文地址:https://www.cnblogs.com/qq78292959/p/4193142.html pre { white-space: pre-wrap; word-wrap: bre ...
- .net core实现的全程序跟踪
Ocelot中使用Butterfly实践 ocelot Ocelot + Consul实践 Ocelot中使用Butterfly实践 Ocelot监控 Ocelot统一权限验证 ...
- Token认证登录以及权限控制
IdentityServer4实现Token认证登录以及权限控制 相关知识点 不再对IdentityServer4做相关介绍,博客园上已经有人出了相关的系列文章,不了解的可以看一下: 蟋蟀大神的: ...
- SpringBoot---核心---日志配置
- 每天学点Linux命令之grep 和 wc命令 --- !管道命令!
Linux系统中grep命令是一种强大的文本搜索工具,它能使用正则表达式搜索文本,并把匹 配的行打印出来.grep全称是Global Regular Expr ession Print,表示全局正则表 ...
- PT100/PT1000
热敏电阻:互换性差,非线性严重,测量范围窄-50~300℃. 金属电阻:准备稳定可靠.-200~500℃ PT100:测量范围宽比PT1000宽,分辨率比PT1000低(100倍,即PT1000每变化 ...
- (转) Linux 下的dd命令使用详解(摘录)
使用dd命令克隆整个系统------http://www.cnblogs.com/jikexianfeng/p/6103504.html 原文:https://www.cnblogs.com/jike ...
- SSAS 部署之创建部署脚本
1.获取多维数据库的结构脚本: 当你的SSAS项目完成后,在Bin目录下会有一个SSAS.asdatabase文件. 2.打开“开始” ->Microsoft SQL Server 2008 R ...