线段树结点上保存一个一般的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. javascript 关于 this 作用域链

    使用 function f() {}  或者 var f = function() {}  来定义的函数,this 是指向 全局对象   var  a = {    b: 1,    c: funct ...

  2. 【Kafka源码】SocketServer启动过程

    SocketServer主要用于接收外部的网络请求,并把请求添加到请求队列中. 一.入口 在KafkaServer.scala中的start方法中,有这样的入口: socketServer = new ...

  3. plsql远程连接虚拟机上的oracle

    下载oracle instantclient_11_2和plsql 将oracle instantclient_11_2解压到任意目录 在此目录下新建network,在network目录下新建admi ...

  4. 小白必看Python视频基础教程

    Python的排名从去年开始就借助人工智能持续上升,现在它已经成为了第一名.Python的火热,也带动了工程师们的就业热.可能你也想通过学习加入这个炙手可热的行业,可以看看Python视频基础教程,小 ...

  5. 使用MS Test做单元测试

    声明:本篇博客翻译自:http://www.c-sharpcorner.com/article/unit-testing-with-ms-tests-in-c-sharp/ 写在翻译之前: 依然清晰的 ...

  6. 一致性hash算法以及其在分布式系统中的应用(转)

    初始架构

  7. 利用可变参数模拟Printf()函数实现一个my_print()函数和调用可变参数注意的陷阱!

    可变参数函数的实现与函数调用的栈结构密切相关,正常情况下C的函数参数入栈规则为__stdcall, 它是从右到左的,即函数中的最右边的参数最先入栈. 例如,对于函数: void test(char a ...

  8. Python网络编程篇之socketserver

    1.socketserver模块和类 socketserver是标准库中的一个高级模块,目标是简化很多样板代码(创建网络客户端和服务器所必须的代码) 这个模块封装了socket编程所需要的各种各样的类 ...

  9. tomcat集群日志切割和远程备份脚本分享

    笔者一共有3台tomcat服务器,一共4个tomcat服务,未来还会增加4个作为负载,笔者想通过在存储服务器对tomcat服务的日志进行远程切割和备份到存储上. 文中采用清空日志的方式,优点是不用重启 ...

  10. Vim常用操作-合并行。

    刚接触 Vim 会觉得它的学习曲线非常陡峭,要记住很多命令.所以这个系列的分享,不会教你怎么配置它,而是教你怎么快速的使用它. 在开发时为了代码美观,经常会把属性用换行的方式显示. <el-di ...