hdu 4983 线段树+斐波那契数
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 线段树+斐波那契数的更多相关文章
- [Codeforces 316E3]Summer Homework(线段树+斐波那契数列)
[Codeforces 316E3]Summer Homework(线段树+斐波那契数列) 顺便安利一下这个博客,给了我很大启发(https://gaisaiyuno.github.io/) 题面 有 ...
- hdu 4099 字典树 + 斐波那契
题意: 给你一个串(最长40位)问你这个串是斐波那契F(n) n <= 99999中的那个数的前缀,如果存在多个输出最小的n否则输出-1. 思路: 给的串最长40位,那 ...
- 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 ...
- 【CF446C】DZY Loves Fibonacci Numbers (线段树 + 斐波那契数列)
Description 看题戳我 给你一个序列,要求支持区间加斐波那契数列和区间求和.\(~n \leq 3 \times 10 ^ 5, ~fib_1 = fib_2 = 1~\). Solut ...
- [莫队算法 线段树 斐波那契 暴力] Codeforces 633H Fibonacci-ish II
题目大意:给出一个长度为n的数列a. 对于一个询问lj和rj.将a[lj]到a[rj]从小到大排序后并去重.设得到的新数列为b,长度为k,求F1*b1+F2*b2+F3*b3+...+Fk*bk.当中 ...
- HDU 5914 Triangle(打表——斐波那契数的应用)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=5914 Problem Description Mr. Frog has n sticks, whos ...
- HDU 1021(斐波那契数与因子3 **)
题意是说在给定的一种满足每一项等于前两项之和的数列中,判断第 n 项的数字是否为 3 的倍数. 斐波那契数在到第四十多位的时候就会超出 int 存储范围,但是题目问的是是否为 3 的倍数,也就是模 3 ...
- noip模拟9[斐波那契·数颜色·分组](洛谷模拟测试)
这次考试还是挺好的 毕竟第一题被我给A了,也怪这题太简单,规律一眼就看出来了,但是除了第一题,剩下的我只有30pts,还是菜 第二题不知道为啥我就直接干到树套树了,线段树套上一个权值线段树,然后我发现 ...
- hdu1568&&hdu3117 求斐波那契数前四位和后四位
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1568 题意:如标题所示,求斐波那契数前四位,不足四位直接输出答案 斐波那契数列通式: 当n<=2 ...
随机推荐
- 5A - 超级楼梯
有一楼梯共M级,刚开始时你在第一级,若每次只能跨上一级或二级,要走上第M级,共有多少种走法? Input 输入数据首先包含一个整数N,表示测试实例的个数,然后是N行数据,每行包含一个整数M(1< ...
- 关于sublime Text 3安装sublimecodeIntel插件配置方法
打开preferences-package settings-sublimecodeIntel-settings users 添加 { "JavaScript": { " ...
- opencv中imread第二个参数的意义
文档中是这么写的: Flags specifying the color type of a loaded image: CV_LOAD_IMAGE_ANYDEPTH - If set, return ...
- 758B Blown Garland
B. Blown Garland time limit per test 1 second memory limit per test 256 megabytes input standard inp ...
- nodejs buffer 内存泄漏问题
摘自<Node.js 高级编程> var buffer = new Buffer("this is the content of my buffer"); var sm ...
- Luogu 2812 校园网络 - Tarjan
Description 给出一个有向图, 要求出至少从哪几个点出发, 能不漏地经过所有节点. 再求出至少加几条边, 才能使图变成一个强联通分量 Solution 求出所有强联通分量, 形成一个有向无环 ...
- LibreOJ 2003. 「SDOI2017」新生舞会 基础01分数规划 最大权匹配
#2003. 「SDOI2017」新生舞会 内存限制:256 MiB时间限制:1500 ms标准输入输出 题目类型:传统评测方式:文本比较 上传者: 匿名 提交提交记录统计讨论测试数据 题目描述 ...
- Sort Array By Parity LT905
Given an array A of non-negative integers, return an array consisting of all the even elements of A, ...
- IOS初级:UIScrollView & UIPageControl
UIScrollView其实构建的就像一列很长的火车,每滑动一个屏幕,展示一节车厢. //主屏幕高度 #define kScreenHeight [UIScreen mainScreen].bound ...
- 获取JavaScript异步函数的返回值
今天研究一个小问题: 怎么拿到JavaScript异步函数的返回值? 1.错误尝试 当年未入行时,我的最初尝试: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 <s ...