Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers
參考:http://www.cnblogs.com/chanme/p/3843859.html
然后我看到在别人的AC的方法里还有这么一种神方法,他预先设定了一个阈值K,当当前的更新操作数j<K的时候,它就用一个类似于树状数组段更的方法,用一个 d数组去存内容,譬如它要在区间 [3,6]上加一段fibonacci
原来:
id 0 1 2 3 4 5 6 7 8 9 10
d 0 0 0 0 0 0 0 0 0 0 0
更新:
id 0 1 2 3 4 5 6 7 8 9 10
d 0 0 0 1 0 0 0 -5 -3 0 0
我们能够发现,当利用 d[i]=d[i]+d[i-1]+d[i-2] i由小到大更新后就会得到
id 0 1 2 3 4 5 6 7 8 9 10
d 0 0 0 1 1 2 3 0 0 0 0
所以对于[L,R]上加一段操作,我们能够d[L]+=1; d[R+1]-=f[R-L+2],d[R+2]=f[R-L+1];
所以假如我更新了m次,我每次更新的复杂度是O(1),我把全部数求出来一次的复杂度是O(n)
然后他的算法是这种,j<K的时候更新的时候依照上述方法更新,询问的话则是将询问区间和更新的区间求交,把交的和加上去。
当j==K的时候,利用d还原出新的a,把当前的区间清0.
4 seconds
256 megabytes
standard input
standard output
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 4
1 2 3 4
1 1 4
2 1 4
1 2 4
2 1 3
17
12
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.
#include <iostream>
#include <cstring>
#include <cstdio>
#include <algorithm> using namespace std; const int maxn=330000;
const long long int mod=1000000009LL;
typedef long long int LL; int n,m,k;
LL a[maxn],sum[maxn],fib[maxn],gib[maxn];
LL d[maxn]; int L[1100],R[1100]; void init()
{
fib[1]=1LL,fib[2]=1LL;
gib[1]=1LL,gib[2]=2LL;
for(int i=3;i<=n+50;i++)
{
fib[i]=(fib[i-1]+fib[i-2])%mod;
gib[i]=(gib[i-1]+fib[i])%mod;
}
} int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
{
scanf("%I64d",a+i);
sum[i]=sum[i-1]+a[i];
}
init();
while(m--)
{
int c,l,r;
scanf("%d%d%d",&c,&l,&r);
if(c==1)
{
L[k]=l,R[k]=r; k++;
d[l]=(d[l]+1LL)%mod;
d[r+1]=(d[r+1]-fib[r-l+2]+mod)%mod;
d[r+2]=(d[r+2]-fib[r-l+1]+mod)%mod;
if(k>=1000)
{
for(int i=1;i<=n;i++)
{
if(i>=2)
d[i]=((d[i]+d[i-1])%mod+d[i-2])%mod;
a[i]=(a[i]+d[i])%mod;
sum[i]=sum[i-1]+a[i];
}
memset(d,0,sizeof(d)); k=0;
}
}
else if(c==2)
{
LL ret=0;
for(int i=0;i<k;i++)
{
int _left=max(l,L[i]);
int _right=min(r,R[i]);
if(_left>_right) continue;
int s=_left-L[i]+1,e=_right-L[i]+1;
ret=((ret+gib[e])%mod-gib[s-1]+mod)%mod;
}
ret=((ret+sum[r])%mod-sum[l-1]+mod)%mod;
printf("%I64d\n",(ret+mod)%mod);
}
}
return 0;
}
Codeforces Round #FF 446 C. DZY Loves Fibonacci Numbers的更多相关文章
- Codeforces Round #FF/#255 D DZY Loves Modification --贪心+优先队列
题意:给你一个矩阵,每次选某一行或者某一列,得到的价值为那一行或列的和,然后该行每个元素减去p.问连续取k次能得到的最大总价值为多少. 解法: 如果p=0,即永远不减数,那么最优肯定是取每行或每列那个 ...
- 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 ...
- [CodeForces - 447E] E - DZY Loves Fibonacci Numbers
E DZY Loves Fibonacci Numbers In mathematical terms, the sequence Fn of Fibonacci numbers is define ...
- 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 Time Limit:4000MS Memory Limit:262144KB 64bit IO Format:%I64d &a ...
- Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)
题目:DZY Loves Fibonacci Numbers 题意比較简单,不解释了. 尽管官方的题解也是用线段树,但还利用了二次剩余. 可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是 ...
- 『题解』Codeforces446C DZY Loves Fibonacci Numbers
更好的阅读体验 Portal Portal1: Codeforces Portal2: Luogu Description In mathematical terms, the sequence \( ...
- cf446C DZY Loves Fibonacci Numbers
C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...
随机推荐
- Eclipse用法和技巧十:显示代码outline
在一个文件中快速找到某一个方法或者某一个作用域,可以使用 Ctrl+O或者Ctrl+F3,快速显示当前代码的outline,进行快速查找.效果如下: 这里主要是补充一些后续操作,能更加方 ...
- Eclipse用法和技巧八:自动添加try/catch块1
站在编译器的角度来看,java中的异常可以分为两种,已检查异常和未检查异常.对于已检查异常比如IO操作,编译器会要求设置try/catch语句块,在eclipse中也只要使用帮助快捷键ctrl+1,就 ...
- Linkedin工程师是如何优化他们的Java代码的(转)
英文原文:LinkedIn Feed: Faster with Less JVM Garbage 最近在刷各大公司的技术博客的时候,我在Linkedin的技术博客上面发现了一篇很不错博文.这篇博文介绍 ...
- Windows8 Metro快捷键 | Win8迷
Windows8 Metro快捷键 | Win8迷 Win + Q : 打开 搜索面板 Win + C : 打开屏幕右侧的Charms简化菜单 Win + 空格 : 切换输入语言和键盘布局
- zoj1028-Flip and Shift
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=28 题意:有相互交叉的黑白两种颜色的小球,每一个小球每次可以跳两格:问你是否可以 ...
- Kendo UI开发教程(24): 单页面应用(二) Router 类
Route类负责跟踪应用的当前状态和支持在应用的不同状态之间切换.Route通过Url的片段功能(#url)和流量器的浏览历史功能融合在一起.从而可以支持把应用的某个状态作为书签添加到浏览器中.Rou ...
- HDU 4669 Mutiples on a circle (DP , 统计)
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个环,每个点是一个数字,取一个子串,使 ...
- POJ 2115 C Looooops(扩展欧几里得应用)
题目地址:POJ 2115 水题. . 公式非常好推.最直接的公式就是a+n*c==b+m*2^k.然后能够变形为模线性方程的样子,就是 n*c+m*2^k==b-a.即求n*c==(b-a)mod( ...
- Uva - 11383 - Golden Tiger Claw
题意:一个N*N的矩阵,第i行第j列的元素大小为w[i][j],每行求一个数row[i],每列求一个数col[j],使得row[i] + col[j] >= w[i][j],且所有的row[]与 ...
- 随想录(移动app下的生活)
[ 声明:版权全部,欢迎转载,请勿用于商业用途. 联系信箱:feixiaoxing @163.com] 我算不上非常潮的人,使用移动app的时间也非常短.换成android手机也是近期一年的事情,可 ...