http://codeforces.com/contest/447/problem/E

题意: 给定一个数组, m次操作,

1 l r 表示区间修改, 每次 a[i] +  Fibonacci[i-l+]

2 l r 区间求和

每次修改操作,只需要记录 每个点前两个值就可以了, 后面的和以及孩子需要加的值都可以通过这两个求出来。 推推公式就出来了。

注意 溢出。

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL;
const int MAXN = 3e5+;
const int MOD = 1e9+;
LL sum[MAXN << ], addv1[MAXN << ], addv2[MAXN << ];
LL fib[MAXN], hhh[MAXN], fff[MAXN];
void pre_solve() {
fff[] = ;
fff[] = ;
fib[] = fib[] = ;
hhh[] = ;
for (int i = ; i < MAXN; i++) {
fib[i] = (fib[i-] + fib[i-]) % MOD;
fff[i] = (fff[i-] + fff[i-]) % MOD;
}
for (int i = ; i < MAXN; i++) {
hhh[i] = (fib[i-] + hhh[i-]) % MOD;
}
}
void push_up(int pos) {
sum[pos] = (sum[pos<<] + sum[pos<<|]) % MOD;
}
void update_add(int l, int r, int pos, LL val1, LL val2) {
addv1[pos] = (addv1[pos] + val1) % MOD;
addv2[pos] = (addv2[pos] + val2) % MOD;
int idx = (r - l + );
sum[pos] = (sum[pos] + fib[idx] * val1 % MOD + hhh[idx] * val2 % MOD) % MOD;
}
void push_down(int l, int r, int pos) {
int mid = (l + r) >> ;
update_add(l, mid, pos<<, addv1[pos], addv2[pos]);
update_add(mid+, r, pos<<|, (fff[mid+-l+]*addv1[pos]+fib[mid+-l]*addv2[pos])%MOD,
(fff[mid+-l+]*addv1[pos]+fib[mid+-l+]*addv2[pos])%MOD);
addv1[pos] = addv2[pos] = ;
}
void build (int l, int r, int pos) {
addv1[pos] = addv2[pos] = ;
if (l == r) {
scanf ("%I64d", sum+pos);
return ;
}
int mid = (l + r) >> ;
build(l, mid, pos<<);
build(mid+, r, pos<<|);
push_up(pos);
}
void update (int l, int r, int pos, int ua, int ub, LL x1, LL x2) {
if (ua <= l && ub >= r) {
update_add(l, r, pos, x1, x2);
return ;
}
push_down(l, r, pos);
int mid = (l + r) >> ;
if (ua <= mid) {
update(l, mid, pos<<, ua, ub, x1, x2);
}
if (ub > mid) {
if (ua <= mid) {
int tmp = max(l, ua);
LL t1 = (fff[mid+-tmp+]*x1+fib[mid+-tmp]*x2) % MOD;
LL t2 = (fff[mid+-tmp+]*x1+fib[mid+-tmp+]*x2) % MOD;
update(mid+, r, pos<<|, ua, ub, t1, t2);
} else {
update(mid+, r, pos<<|, ua, ub, x1, x2);
}
}
push_up(pos);
}
LL query (int l, int r, int pos, int ua, int ub) {
if (ua <= l && ub >= r) {
return sum[pos];
}
push_down(l, r, pos);
int mid = (l + r) >> ;
LL tmp = ;
if (ua <= mid)
{
tmp = (tmp + query(l, mid, pos<<, ua, ub)) % MOD;
}
if (ub > mid)
{
tmp = (tmp + query(mid+, r, pos<<|, ua, ub)) % MOD;
} return tmp;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
#endif
int n, m;
pre_solve();
while (~ scanf ("%d%d", &n, &m)) {
build(, n, );
for (int i = ; i < m; i++) {
int op, u, v;
scanf ("%d%d%d", &op, &u, &v);
if (op == ) {
update(, n, , u, v, fib[], fib[]);
} else {
LL ans = query(, n, , u, v);
printf("%I64d\n", ans);
}
}
}
return ;
}

Codeforces Round #FF (Div. 2)__E. DZY Loves Fibonacci Numbers (CF447) 线段树的更多相关文章

  1. DP Codeforces Round #FF (Div. 1) A. DZY Loves Sequences

    题目传送门 /* DP:先用l,r数组记录前缀后缀上升长度,最大值会在三种情况中产生: 1. a[i-1] + 1 < a[i+1],可以改a[i],那么值为l[i-1] + r[i+1] + ...

  2. codeforces 446C DZY Loves Fibonacci Numbers 数论+线段树成段更新

    DZY Loves Fibonacci Numbers Time Limit:4000MS     Memory Limit:262144KB     64bit IO Format:%I64d &a ...

  3. Codeforces 446C —— DZY Loves Fibonacci Numbers(线段树)

    题目:DZY Loves Fibonacci Numbers 题意比較简单,不解释了. 尽管官方的题解也是用线段树,但还利用了二次剩余. 可是我没有想到二次剩余,然后写了个感觉非常复杂度的线段树,还是 ...

  4. Codeforces Round #FF (Div. 1) B. DZY Loves Modification 优先队列

    B. DZY Loves Modification 题目连接: http://www.codeforces.com/contest/446/problem/B Description As we kn ...

  5. Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划

    A. DZY Loves Sequences 题目连接: http://www.codeforces.com/contest/446/problem/A Description DZY has a s ...

  6. Codeforces Round #FF (Div. 2) D. DZY Loves Modification 优先队列

    D. DZY Loves Modification time limit per test 2 seconds memory limit per test 256 megabytes input st ...

  7. Codeforces Round #FF (Div. 1) B. DZY Loves Modification

    枚举行取了多少次,如行取了i次,列就取了k-i次,假设行列单独贪心考虑然后相加,那么有i*(k-i)个交点是多出来的:dpr[i]+dpc[k-i]-i*(k-i)*p 枚举i取最大值.... B. ...

  8. Codeforces Round #FF (Div. 2):B. DZY Loves Strings

    B. DZY Loves Strings time limit per test 1 second memory limit per test 256 megabytes input standard ...

  9. Codeforces Round #FF (Div. 1) A. DZY Loves Sequences

    题目链接: http://www.codeforces.com/contest/446/problem/A 题解: dp1[x]表示以x结尾的最大严格升序连续串,dp2[x]表示以x开头的最大严格升序 ...

随机推荐

  1. Understand Rails Authenticity Token

    翻译整理自: http://stackoverflow.com/questions/941594/understand-rails-authenticity-token 主要翻译的是第一个回答,另外结 ...

  2. Neutron中的Service类

    Service是OpenStack中非常重要的一个概念,各个服务的组件都以Service类的方式来进行交互. Neutron中的Service类继承自rpc中的Service,总体的继承关系为 neu ...

  3. 《C专家编程》第一天

    1.2 C语言的早期体验 1)C语言的基本数据类型直接与底层硬件相对应.C语言不存在内置的复数类型.C语言一开始不支持浮点类型,直到硬件系统能够直接支持浮点数之后才增加了对它的支持. 2)auto关键 ...

  4. Linux 机器之间建立互信

    原理: 就是两台机器(web-1和web-2)经过预先设置好经过认证的key文件,双方互相访问时,进行自动认证,从而实现互信.   互信的原理了解了,我们可以把配置ssh互信的步骤进行有效的分割. 1 ...

  5. UVA 12378 Ball Blasting Game 【Manacher回文串】

    Ball Blasting Game Morteza is playing a ball blasting game. In this game there is a chain of differe ...

  6. HDU3757

    题意:一些团队因为任务要去避难所,并且每个避难所必须要有团队在,避难所的数量小于等于团队的数量, 团队去避难所的消耗油量与路程成正比,求解最小耗油量.题目来源:2010 Northeastern Eu ...

  7. 阿里云linux的nginx下面配置多站点

    假设有服务器ip为 114.214.85.35 域名1为  www.jieshendada.cn 域名2为 www.jieshenxiaoxiao.cn 1.首先打开nginx域名配置文件存放目录:/ ...

  8. RTSP协议资料

    维基百科: RTSP:http://en.wikipedia.org/wiki/Real_Time_Streaming_Protocol RTP:http://en.wikipedia.org/wik ...

  9. 告诉你GetDC()没有释放造成的后果

    最近做的项目中需要显示视频监控窗口,从采集卡中读到图像的数据,需要实时显示出来,而且速度比较快. 由于比较简单,就直接使用了GDI画图,以前复杂的都用openGL啥的工具了,这次这个简单,就直接用GD ...

  10. C++拾遗(十二)C++代码重用

    “has-a”关系 通常有两种方法实现: 1.被包含,本身是另一个类的对象. 2.私有或者保护继承. 主要讨论第二种方法,在继承时使用private关键字(或者不用任何关键字,默认就是私有的). 使用 ...