线段树结点上保存一个一般的sum值,再同一时候保存一个fbsum,表示这个结点表示的一段数字若为斐波那契数时的和

当进行3操作时,仅仅用将sum = fbsum就可以

其它操作照常进行,仅仅是单点更新的时候也要先向下更新

#include <cstdio>
#include <ctime>
#include <cstdlib>
#include <cstring>
#include <queue>
#include <string>
#include <set>
#include <stack>
#include <map>
#include <cmath>
#include <vector>
#include <iostream>
#include <algorithm>
#include <bitset>
#include <fstream>
using namespace std;
//LOOP
#define FF(i, a, b) for(int i = (a); i < (b); ++i)
#define FE(i, a, b) for(int i = (a); i <= (b); ++i)
#define FED(i, b, a) for(int i = (b); i>= (a); --i)
#define REP(i, N) for(int i = 0; i < (N); ++i)
#define CLR(A,value) memset(A,value,sizeof(A))
#define FC(it, c) for(__typeof((c).begin()) it = (c).begin(); it != (c).end(); it++)
//OTHER
#define SZ(V) (int)V.size()
#define PB push_back
#define MP make_pair
#define all(x) (x).begin(),(x).end()
//INPUT
#define RI(n) scanf("%d", &n)
#define RII(n, m) scanf("%d%d", &n, &m)
#define RIII(n, m, k) scanf("%d%d%d", &n, &m, &k)
#define RIV(n, m, k, p) scanf("%d%d%d%d", &n, &m, &k, &p)
#define RV(n, m, k, p, q) scanf("%d%d%d%d%d", &n, &m, &k, &p, &q)
#define RS(s) scanf("%s", s)
//OUTPUT
#define WI(n) printf("%d\n", n)
#define WS(n) printf("%s\n", n) #define sqr(x) (x) * (x)
typedef long long LL;
typedef unsigned long long ULL;
typedef vector <int> VI;
const double eps = 1e-9;
const int MOD = 1000000007;
const double PI = acos(-1.0);
//const int INF = 0x3f3f3f3f;
const int maxn = 100010;
const LL INF = 0x3f3f3f3f3f3f3f3fLL; #define ll rt << 1
#define rr rt << 1 | 1 struct Node{
int l, r, m;
LL sum, fbsum;
bool f;
}t[maxn * 4];
LL fb[10000];
int fbcnt; void init()
{
fb[1] = 1, fb[0] = 1;
for (int i = 2; ; i++)
{
fb[i] = fb[i - 1] + fb[i - 2];
if (fb[i] > INF)
{
fbcnt = i - 1;
return;
}
}
return;
} void push_up(int rt)
{
t[rt].sum = t[ll].sum + t[rr].sum;
t[rt].fbsum = t[ll].fbsum + t[rr].fbsum;
t[rt].f = t[ll].f & t[rr].f;
} LL get(LL x)
{
int pos = lower_bound(fb, fb + fbcnt, x) - fb;
if (pos && abs(fb[pos - 1] - x) <= abs(fb[pos] - x))
return fb[pos - 1];
return fb[pos];
} void push_down(int rt)
{
if (t[rt].f)
{
t[ll].sum = t[ll].fbsum;
t[rr].sum = t[rr].fbsum;
t[rr].f = t[ll].f = 1;
t[rt].f = 0;
}
} void build(int l, int r, int rt)
{
t[rt].l = l, t[rt].r = r, t[rt].m = (l + r) >> 1;
if (l == r)
{
t[rt].sum = 0;
t[rt].fbsum = 1;
t[rt].f = 0;
return;
}
build(l, t[rt].m, ll);
build(t[rt].m + 1, r, rr);
push_up(rt);
} void update(int x, int add, int rt)
{
if (x == t[rt].l && t[rt].r == x)
{
t[rt].sum += add;
t[rt].fbsum = get(t[rt].sum);
t[rt].f = 0;
return;
}
push_down(rt);
if (x <= t[rt].m)
update(x, add, ll);
else
update(x, add, rr);
push_up(rt);
} void updatefb(int l, int r, int rt)
{
if (l <= t[rt].l && r >= t[rt].r)
{
t[rt].sum = t[rt].fbsum;
t[rt].f = 1;
return;
}
push_down(rt);
if (l <= t[rt].m)
updatefb(l, r, ll);
if (r > t[rt].m)
updatefb(l, r, rr);
push_up(rt);
} LL query(int l, int r, int rt)
{
if (l <= t[rt].l && r >= t[rt].r)
return t[rt].sum;
push_down(rt);
LL ans = 0;
if (l <= t[rt].m)
ans += query(l, r, ll);
if (r > t[rt].m)
ans += query(l, r, rr);
return ans;
} int main()
{
init();
int n, m;
while (~RII(n, m))
{
build(1, n, 1);
int x, y, op;
while (m--)
{
RIII(op, x, y);
if (op == 1)
update(x, y, 1);
else if (op == 2)
printf("%I64d\n", query(x, y, 1));
else
updatefb(x, y, 1);
}
}
return 0;
}
/*
5 4
3 1 3
2 1 3
1 3 3
2 1 3 5 2
3 1 3
2 1 2
*/

hdu4893 Wow! Such Sequence!的更多相关文章

  1. HDU4893:Wow! Such Sequence!(段树lazy)

    Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger f ...

  2. Wow! Such Sequence!(线段树4893)

    Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...

  3. HDU 4893 Wow! Such Sequence! (线段树)

    Wow! Such Sequence! 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4893 Description Recently, Doge ...

  4. 线段树 + 区间更新: HDU 4893 Wow! Such Sequence!

    Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  5. hdu 4893 Wow! Such Sequence!(线段树)

    题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 改动k的为值添加d 2 l r, 查询l到r的区间和 3 l r. 间l到r区间上的所以数变成 ...

  6. Wow! Such Sequence! (线段树) hdu4893

    http://acm.hdu.edu.cn/showproblem.php?pid=4893 先贴上一份还没过的代码,不知道拿出错了  1 // by caonima ; ; ],col[MAX< ...

  7. HDU 4893 Wow! Such Sequence!(2014 Multi-University Training Contest 3)

    题意: 有三种操作: 1 x y: 表示给x位置加上y 2 x y:查询[x,y]的区间和 3 x y:将 [x,y] 区间上的数变为最接近的 Fibonacci. 思路: 1 操作按正常单调更新,区 ...

  8. HDOJ 4893 Wow! Such Sequence!

    题意是这样的,给定一个n个元素的数组,初始值为0,3种操作: 1 k d将第k个数增加d: 2 l r 询问区间l...r范围内数之和: 3 l r 表示将区间l...r内的数变成离他最近的斐波那契数 ...

  9. 2014多校3 Wow! Such Sequence!段树

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=4893 这个问题还真是纠结啊--好久不写线段树的题了.由于这几天学伸展树.然后认为线段树小case了. ...

随机推荐

  1. http 500错误怎么解决方法

    出现500错误的原因是很多的,一般来说,如果程序出错,那么在浏览器内会返回给用户一个友好的错误提示,统一称之为服务器500错误. 解决的方法就是您必须在http中能够正确的获得错误信息,方法为:请打开 ...

  2. instanceof 原理

    运行流程 function instance_of(L, R) {                               //L 表示左表达式,R 表示右表达式   var O = R.prot ...

  3. 《Linux命令行与shell脚本编程大全》 第八章管理文件系统

    8.1 探索linux文件系统 8.1.1 基本的Linux文件系统 ext:最早的文件系统,叫扩展文件系统.使用虚拟目录操作硬件设备,在物理设备上按定长的块来存储数据. 用索引节点的系统来存放虚拟目 ...

  4. [转载] Redis-benchmark使用总结

    转载自http://blog.csdn.net/jiangguilong2000/article/details/24143721 Redis-benchmark为Redis性能测试工具. 指令说明: ...

  5. ReentrantLock可重入锁的使用场景

    摘要 从使用场景的角度出发来介绍对ReentrantLock的使用,相对来说容易理解一些. 场景1:如果发现该操作已经在执行中则不再执行(有状态执行) a.用在定时任务时,如果任务执行时间可能超过下次 ...

  6. Scratch——教小孩子学编码

    教小孩子学编码 http://scratch.mit.edu/ http://v.163.com/movie/2013/3/H/I/M92389L06_M9238GTHI.html

  7. 从编辑距离、BK树到文本纠错

    搜索引擎里有一个很重要的话题,就是文本纠错,主要有两种做法,一是从词典纠错,一是分析用户搜索日志,今天我们探讨使用基于词典的方式纠错,核心思想就是基于编辑距离,使用BK树.下面我们来逐一探讨: 编辑距 ...

  8. laravel webpack填坑(陆续更)

    ie Promise支持需引入babel-polyfill, 在官方文档中js函数介绍有点少导致按babel-polyfill官方引入时找不到北 //webpack.mix.jsmix.js(['no ...

  9. 浅谈快速开发框架的分层(WinForm)

    对于B/S都是MVC好不好 不多说了,反正大家都这么用 这里简单说下C/S 首先常用的几种: 模仿B/S的MVC  也有人称之为 MVP 还有MVVM这种真心觉得够够的了,当然也有其优势所在,这里不讨 ...

  10. ThinkPHP中ajax绑定select下拉框无法显示

    html代码: 控制器代码: 其中的<option value="{$vo.gradeId}">{$one.gradeName}</option> 在操作过 ...