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. Lucene和jackson冲突

    今天在使用lucene的时候,想直接在Controller中返回json对象,于是在Spring中配置了JackSon的converter: <bean id="jacksonMess ...

  2. Android 之 自定义标签 和 自定义组件

    1    自定义标签 这是我的模板项目目录     既然想像 android:text  那样使用自己的标签,那么首先得有标签. 在 res/values/ 下我新建了个 mm_tag.xml (切记 ...

  3. [Angular 2] Child Router

    Benefit to use child router is Angualr 2 then can lazy load the component on demand. Define a child ...

  4. linux boost 安装

    sudo apt-get install libboost-dev 但是,我这样安装以后,编译程序时出现了很多错误,而且都是系统文件的错误.我开始以为是我的boost库版本不对,后来换了好几个版本,都 ...

  5. 在Windows环境中安装并使用kafka

    [TOC] 安装部署 安装部署Java 下载需要安装的软件,下载地址:http://www.oracle.com/technetwork/java/javase/downloads/jre8-down ...

  6. 大数据笔记06:大数据之Hadoop的HDFS(文件的读写操作)

    1. 首先我们看一看文件读取: (1)客户端(java程序.命令行等等)向NameNode发送文件读取请求,请求中包含文件名和文件路径,让NameNode查询元数据. (2)接着,NameNode返回 ...

  7. linux下常用基本命令操作

    #fdisk -l 查看硬盘信息 cat /proc/cpuinfo 查看CPU信息 free -m 查看内存信息 ethtool eth0 查看网卡信息 df -h 查看硬盘各分区可用空间大小 ca ...

  8. 解决从源码编译ncurses6.0编译lib_gen.c报错的问题

    直接从官网的源码编译时,会提示报错: gcc -DHAVE_CONFIG_H -I. -I../include -D_GNU_SOURCE -DNDEBUG -O2 --param max-inlin ...

  9. js和php判断当前是否为微信浏览器?

  10. (转)jquery.validate.js 的 remote 后台验证

    之前已经有一篇关于jquery.validate.js验证的文章,还不太理解的可以先看看:jQuery Validate 表单验证(这篇文章只是介绍了一下如何实现前台验证,并没有涉及后台验证remot ...