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 ...
随机推荐
- Mac下安装社区版MongoDB
MongoDB下载地址:https://www.mongodb.com/download-center?_ga=2.98072543.1777419256.1515472368-391344272.1 ...
- iOS.Notification.Bar.Color
Reference: http://apple.stackexchange.com/questions/44246/what-determines-the-special-color-of-the-s ...
- [Centos] ERROR: Could not find useradd in chroot, maybe the install failed?
[mockbuild at localhost ~]$ mock -r centos-5-x86_64-testdev.cfg initinitcleanprepThis may take a whi ...
- ROC曲线 Receiver Operating Characteristic
ROC曲线与AUC值 本文根据以下文章整理而成,链接: (1)http://blog.csdn.net/ice110956/article/details/20288239 (2)http://b ...
- 如何使用AE来制作烟雾粒子特效
1. 首先,我们先建立一个合成,大小1280×720,帧速率25,持续时间为10秒,建好后,再新建一个固态层点击确定. 2.点击效果添加Particular粒子插件 3. 点开粒子插件的发射器,方 ...
- openssl_error_string()
其实已经成功了,openssl_error_string()一样会输出错误信息,忽略就好
- Spring 系列教程之容器的功能
Spring 系列教程之容器的功能 经过前面几章的分析,相信大家已经对 Spring 中的容器功能有了简单的了解,在前面的章节中我们一直以 BeanFacotry 接口以及它的默认实现类 XmlBea ...
- AWVS基本用法
https://www.bugbank.cn/q/article/5983de41cbb936102d397781.html
- swift NSdata 转换 nsstring
result = NSString(data: data, encoding: NSUTF8StringEncoding) 做HTTP 请求时 遇到 打印结果看 所以~~~
- Https如何确保传输安全的
1.对称加密算法 加密和解密算法是公开的,那个密钥是保密的, 只有双方才知道, 这样生成的加密消息(密文) 别人就无法得知了. 2.非对称加密算法 RSA算法非常有意思,是有一对儿钥匙, 一个是保密的 ...