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 ...
随机推荐
- 8K - 圆桌会议
HDU ACM集训队的队员在暑假集训时经常要讨论自己在做题中遇到的问题.每当面临自己解决不了的问题时,他们就会围坐在一张圆形的桌子旁进行交流,经过大家的讨论后一般没有解决不了的问题,这也只有HDU A ...
- hdu 1558 (线段相交+并查集) Segment set
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1558 题意是在坐标系中,当输入P(注意是大写,我当开始就wa成了小写)的时候输入一条线段的起点坐标和终点坐 ...
- (O)js核心:作用域链
作用域 在一个函数被调用的时候,函数的作用域才会存在.此时,在函数还没有开始执行的时候,开始创建函数的作用域: 函数作用域的创建步骤: 1.函数形参的声明. 2.函数变量的声明. 3.普通变量的声 ...
- 说说为什么会有ssl.CertificateError报错
有一些网站没有获取浏览器的颁发的安全证书 当你在请求这个网站时浏览器会当做不安全网站处理 因此会报ssl.CertificateError 解决办法是 :将默认的证书验证模式修改为不需要验证 代码如下 ...
- 利用sql的OVER()PARTITION 找到最相近的数值
前几天同事问我一个问题,能不能用sql搞定这个问题: 我这里有一个张表table1中有time1,value1,有表table2有字段time2,value2. 现在要把table2中的value2更 ...
- 设计师别浪费时间啦,快来试试这款Sketch标注插件吧
随着移动互联网的快速发展,用户的需求也在不断地增大,这对产品经理还有设计师的考验是越来越大.市场环境的变化让我们深信为快不破,但是一个产品的产出需要各个环节的紧密配合,但往往在产品输出过程中,由于分工 ...
- contenteditable设置元素可编辑
需求背景 实现一个输入框,高度可以随着输入文字的增加而自动增高 有placeholder,输入为空时,显示placeholder 我们知道可以将div的contenteditable设置伪true,将 ...
- Python 使用for...in...和 while 循环 实现8种格式的 九九乘法表
#九九乘法表 for...in .. #左下角 for i in range(1,10): for j in range(1,i+1): print(' %d×%d=%2d'%(j,i,i*j), e ...
- 【Linux】Tree命令安装和使用
Tree命令简介 tree是一种递归目录列表命令,产生一个深度缩进列表文件,这是彩色的ALA dircolors如果ls_colors设置环境变量和输出是TTY.树已经被移植和报道以下操作系统下工作: ...
- spring学习七 spring和dynamic project进行整合
spring和web项目进行整合,其实就是在项目启动时,就创建spring容器,然后在servlet中使用spring容器进行开. 注意:为了页面可以访问到servlet,因此servlet必须放进t ...