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. android开发步步为营之68:Facebook原生广告接入总结

    开发应用的目的是干嘛?一方面当然是提供优质服务给用户,还有一方面最重要的还是须要有盈利.不然谁还有动力花钱花时间去开发app? 我们的应用主攻海外市场,所以主要还是接入国外的广告提供商.本文就今天刚完 ...

  2. linux定时任务1-crontab命令

    简单测试例子: 添加定时任务前,注意查看crond服务是否已经启动,如果未启动,则用命令service crond start命令启动. 注意给脚本添加可执行权限. [root@rheltest1 ~ ...

  3. VNC 黑屏

    参考资料: http://blog.sina.com.cn/s/blog_57edaf600100serf.html http://www.programgo.com/article/43581885 ...

  4. HDU 1199 - Color the Ball 离散化

    [题意]现在有几个球排成一排,编号从1开始,开始时所有球为黑色,现在有n(<=2000)次操作,每次操作将l[i]至r[i](均在int范围)的球凃成颜色c[i](黑色'b'或白色'w'),然后 ...

  5. Myeclipse中点(.)不出来方法或者属性?

  6. mysql文件导入到数据库load data infile into table 的使用例子

    load data infile "C:/Users/Administrator/Desktop/1.txt"into table 要一个已经存的表名 字段默认用制表符隔开 文件 ...

  7. C++小技巧之四舍五入与保留小数

    四舍五入:保留整数 int a = b+0.5; 保留一位小数  int a=(b+0.05)*10; double c=a/10; 保留二位小数  int a=(b+0.005)*100; doub ...

  8. 你好,C++(26)如何与函数内部进行数据交换?5.1.3 函数参数的传递

    5.1.3  函数参数的传递 我们知道,函数是用来完成某个功能的相对独立的一段代码.函数在完成这个功能的时候,往往需要外部数据的支持,这时就需要在调用这个函数时向它传递所需要的数据它才能完成这个功能获 ...

  9. GetWindowText

    用于得到窗口中的数据 {// TODO: If this is a RICHEDIT control, the control will not// send this notification un ...

  10. 关于Zend Framework 2中 Zend\Session的使用

    一直迷惑于zend\Session的使用,这个是Zend\Session的官方教程的中文版,http://zend-framework-2.yangfan.co/blog/556. 其中最重要的是关于 ...