http://acm.hdu.edu.cn/showproblem.php?pid=4893

三种操作:

1 k d, 修改k的为值增加d

2 l r, 查询l到r的区间和

3 l r, 从l到r区间上的所以数变成最近的斐波那契数,相等的话取向下取。

就是线段树搞,每个节点lazy表示该节点以下的位置是否都是斐波那契数,找比x小的斐波那契数使用lower_bound+加特判最近即可

#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define RD(x) scanf("%d",&x)
#define RD2(x,y) scanf("%d%d",&x,&y)
#define clr0(x) memset(x,0,sizeof(x))
typedef long long LL;
const int N = 100005;
const LL inf = 1LL<<60; struct node
{
int lazy;
LL sum,r;
}s[N<<3];
LL c[N];
LL f[1500];
void build(int l,int r,int root)
{
s[root].sum = 0;
s[root].r = r - l + 1;
s[root].lazy = 0;
if(l == r){
return;
}
int mid = (l+r)>>1;
build(l,mid,root<<1);
build(mid+1,r,(root<<1)+1);
return;
}
LL check(LL x){
int t = lower_bound(f,f+1426,x) - f;
long long delta = abs(f[t]-x);
if (t > 0 && abs(f[t-1] - x) <= delta) --t;
return f[t];
}
void update(int root)
{
s[root].sum = s[root<<1].sum + s[root<<1|1].sum;
s[root].r = s[root<<1].r + s[root<<1|1].r;
}
void insert(int l,int r,int root,int index,int v)
{
if(l == r){
s[root].sum += v;
s[root].r = check(s[root].sum);
return;
}
if(s[root].lazy){
s[root<<1].lazy = 1;
s[root<<1].sum = s[root<<1].r;
s[root<<1|1].lazy = 1;
s[root<<1|1].sum = s[root<<1|1].r;
s[root].lazy = 0;
}
int mid = (l+r)>>1;
if(mid >= index)
insert(l,mid,root<<1,index,v);
else
insert(mid+1,r,(root<<1)+1,index,v);
update(root);
}
LL query(int l , int r , int root , int ll , int rr){
if (l > rr || r < ll) return 0;
if(s[root].lazy){
s[root<<1].lazy = 1;
s[root<<1].sum = s[root<<1].r;
s[root<<1|1].lazy = 1;
s[root<<1|1].sum = s[root<<1|1].r;
s[root].lazy = 0;
}
if (ll <= l && rr >= r) return s[root].sum;
int mid = (l+r)>>1;
return query(l,mid,root<<1,ll,rr) + query(mid+1,r,(root<<1)+1,ll,rr);
}
void change(int l , int r , int root, int ll , int rr){
if (l > rr || r < ll) return;
if (ll <= l && rr >= r){
s[root].lazy = 1;
s[root].sum = s[root].r;
return;
}
if(s[root].lazy){
s[root<<1].lazy = 1;
s[root<<1].sum = s[root<<1].r;
s[root<<1|1].lazy = 1;
s[root<<1|1].sum = s[root<<1|1].r;
s[root].lazy = 0;
}
int mid = (l+r)>>1;
change(l,mid,root<<1,ll,rr);
change(mid+1,r,(root<<1)+1,ll,rr);
update(root);
}
int main()
{
f[0] = f[1] = 1LL;
int i;
for(i = 2;i < 1426;++i)
f[i] = f[i-1]+f[i-2];
int n,m;
while(~RD2(n,m)){
clr0(c);
build(1,n,1);
int l,r,q;
while(m--){
scanf("%d%d%d",&q,&l,&r);
if(q == 1)
insert(1,n,1,l,r);
else if(q == 2)
printf("%I64d\n",query(1,n,1,l,r));
else
change(1,n,1,l,r);
}
}
return 0;
}

hdu 4983 线段树+斐波那契数的更多相关文章

  1. [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)

    [Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...

  2. hdu 4099 字典树 + 斐波那契

    题意:       给你一个串(最长40位)问你这个串是斐波那契F(n)  n <= 99999中的那个数的前缀,如果存在多个输出最小的n否则输出-1. 思路:       给的串最长40位,那 ...

  3. Codeforces 446-C DZY Loves Fibonacci Numbers 同余 线段树 斐波那契数列

    C. DZY Loves Fibonacci Numbers time limit per test 4 seconds memory limit per test 256 megabytes inp ...

  4. 【CF446C】DZY Loves Fibonacci Numbers (线段树 + 斐波那契数列)

    Description ​ 看题戳我 给你一个序列,要求支持区间加斐波那契数列和区间求和.\(~n \leq 3 \times 10 ^ 5, ~fib_1 = fib_2 = 1~\). Solut ...

  5. [莫队算法 线段树 斐波那契 暴力] Codeforces 633H Fibonacci-ish II

    题目大意:给出一个长度为n的数列a. 对于一个询问lj和rj.将a[lj]到a[rj]从小到大排序后并去重.设得到的新数列为b,长度为k,求F1*b1+F2*b2+F3*b3+...+Fk*bk.当中 ...

  6. HDU 5914 Triangle(打表——斐波那契数的应用)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Problem Description Mr. Frog has n sticks, whos ...

  7. HDU 1021(斐波那契数与因子3 **)

    题意是说在给定的一种满足每一项等于前两项之和的数列中,判断第 n 项的数字是否为 3 的倍数. 斐波那契数在到第四十多位的时候就会超出 int 存储范围,但是题目问的是是否为 3 的倍数,也就是模 3 ...

  8. noip模拟9[斐波那契·数颜色·分组](洛谷模拟测试)

    这次考试还是挺好的 毕竟第一题被我给A了,也怪这题太简单,规律一眼就看出来了,但是除了第一题,剩下的我只有30pts,还是菜 第二题不知道为啥我就直接干到树套树了,线段树套上一个权值线段树,然后我发现 ...

  9. hdu1568&&hdu3117 求斐波那契数前四位和后四位

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568 题意:如标题所示,求斐波那契数前四位,不足四位直接输出答案 斐波那契数列通式: 当n<=2 ...

随机推荐

  1. andorid 全部对话框

    .xml <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android ...

  2. UI和View 三种控制方式

    AndroidManifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xm ...

  3. Luogu2149 [SDOI2009]Elaxia的路线-最短路+拓扑排序

    Solution 另外$ m <=5e5$. 两条最短路的 最长公共路径 一定是若干条连续的边, 并且满足拓扑序. 于是我们分别 正向 和反向走第二条路径,若该条边同时是两条最短路径上的边, 则 ...

  4. Ubuntu下实现gedit支持nesC语法高亮

    在TinyOS下主要采用nesC语言(C语言的一个变种)编程,ubuntu系统默认打开文本的工具是gedit,为实现gedit支持nesC语法高亮,将最下面的代码保存为nesC.lang文件,然后将n ...

  5. 论坛:Html代码生成器>>FCKeditor的使用

    >>文件准备: >>例1: >>例2: >>例3:指定工具栏 添加 JS代码:

  6. Linux搭建SVN

    Linux搭建SVN 服务器 1 安装SVN 官网下载:http://subversion.apache.org/packages.html SVN客户端:TortoiseSVN,官网下载:http: ...

  7. js循环遍历数组

    一维数组的遍历 <script> //循环遍历数组 var animals = ["cat",'dog','human','whale','seal']; var an ...

  8. Python 语法糖装饰器的应用

    Python中的装饰器是你进入Python大门的一道坎,不管你跨不跨过去它都在那里. 为什么需要装饰器 我们假设你的程序实现了say_hello()和say_goodbye()两个函数. def sa ...

  9. ServiceDesk Plus解析内容,简化工单管理

  10. kbmmw 5.0 中的REST 服务

    目前关于REST 服务的话题越来越热,kbmmw 在5.0 里面开始支持rest.今天我就试一下kbmmw 的 rest 服务.闲话少说,开始. 老规矩,放上两个kbmMWServer1和 kbmMW ...