Wow! Such Sequence!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 3354    Accepted Submission(s): 966

Problem Description
Recently, Doge got a funny birthday present from his new friend, Protein Tiger from St. Beeze College. No, not cactuses. It's a mysterious blackbox.

After some research, Doge found that the box is maintaining a sequence an of n numbers internally, initially all numbers are zero, and there are THREE "operations":

1.Add d to the k-th number of the sequence.
2.Query the sum of ai where l ≤ i ≤ r.
3.Change ai to the nearest Fibonacci number, where l ≤ i ≤ r.
4.Play sound "Chee-rio!", a bit useless.

Let F0 = 1,F1 = 1,Fibonacci number Fn is defined as Fn = Fn - 1 + Fn - 2 for n ≥ 2.

Nearest Fibonacci number of number x means the smallest Fn where |Fn - x| is also smallest.

Doge doesn't believe the machine could respond each request in less than 10ms. Help Doge figure out the reason.

 
Input
Input contains several test cases, please process till EOF.
For each test case, there will be one line containing two integers n, m.
Next m lines, each line indicates a query:

1 k d - "add"
2 l r - "query sum"
3 l r - "change to nearest Fibonacci"

1 ≤ n ≤ 100000, 1 ≤ m ≤ 100000, |d| < 231, all queries will be valid.

 
Output
For each Type 2 ("query sum") operation, output one line containing an integer represent the answer of this query.
 
Sample Input
1 1
2 1 1
5 4
1 1 7
1 3 17
3 2 4
2 1 5
 
Sample Output
0
22
 
Author
Fudan University
 
Source
 

题意:

1.Add d to the k-th number of the sequence.
2.Query the sum of ai where l ≤ i ≤ r.
3.Change ai to the nearest Fibonacci number, where l ≤ i ≤ r.     就这三句话

 #include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
const int maxn = 1e5 + ;
ll fab[];
int lazy[maxn<<]; //lazy[pos]为1 表示该区间的数为Fibonacci number。
ll sum1[maxn<<],sum2[maxn<<]; // sum1为原数组的和,sum2为变为Fibonacci number之后的和,维护这两个数组
void pre_solve() //预处理数前90个斐波那契数
{
fab[] = fab[] = ;
for (int i = ; i <= ; i++)
fab[i] = fab[i-] + fab[i-];
}
ll find_fab(ll x) //找到距离x最近的斐波那契数
{
ll ans = fab[],delta= abs(fab[] - x);
for (int i = ; i <= ; i++)
{
if (delta > abs(x - fab[i]))
{
delta = abs (x - fab[i]);
ans = fab[i];
}
}
return ans;
}
void push_down(int pos)
{
if (lazy[pos])
{
lazy[pos<<] = lazy[pos<<|] = lazy[pos];
lazy[pos] = ;
sum1[pos<<] = sum2[pos<<];
sum1[pos<<|] = sum2[pos<<|];
}
}
void push_up(int pos)
{
sum1[pos] = sum1[pos<<] + sum1[pos<<|];
sum2[pos] = sum2[pos<<] + sum2[pos<<|];
}
void build(int l,int r,int pos)
{
sum1[pos] = lazy[pos] = ;
if (l == r)
{
sum2[pos] = ; //初始状态 所有数都为0,距离0最近的斐波那契数是1
return;
}
int mid = (l + r) >> ;
build(l,mid,pos<<);
build(mid+,r,pos<<|);
push_up(pos);
}
void update_add(int l,int r,int pos,int x,int val)
{
if (l == r)
{
if (lazy[pos])
sum1[pos] = sum2[pos] + val; //如果该位置的数字为斐波那契数,那么在此基础上加val
else
sum1[pos] += val; //不是斐波那契数 直接加val
sum2[pos] = find_fab(sum1[pos]); //sum1[pos] 改变,相应的sum2也要改变
lazy[pos] = ;
return;
}
push_down(pos);
int mid = (l + r) >> ;
if (x <= mid)
update_add(l,mid,pos<<,x,val);
else
update_add(mid+,r,pos<<|,x,val);
push_up(pos);
}
void update_fab(int l,int r,int pos,int ua,int ub)
{
if (ua <= l && ub >= r)
{
sum1[pos] = sum2[pos];
lazy[pos] = ;
return;
}
push_down(pos);
int mid = (l + r) >> ;
if (ua <= mid)
update_fab(l,mid,pos<<,ua,ub);
if (ub > mid)
update_fab(mid+,r,pos<<|,ua,ub);
push_up(pos);
}
ll query(int l,int r,int pos,int ua,int ub)
{
if (ua <= l && ub >= r )
return sum1[pos];
push_down(pos);
int mid = (l + r) >> ;
ll ans = ;
if (ua <= mid)
ans += query(l,mid,pos<<,ua,ub);
if (ub > mid)
ans += query(mid+,r,pos<<|,ua,ub);
return ans;
}
int main(void)
{
#ifndef ONLINE_JUDGE
freopen("in.txt","r",stdin);
freopen("out.txt","w",stdout);
#endif
pre_solve();
int n,m;
while (~scanf ("%d%d",&n,&m))
{
build(,n,);
for (int i = ; i < m; i++)
{
int op,x,y;
scanf ("%d%d%d",&op,&x,&y);
if (op == )
update_add(,n,,x,y);
if (op == )
update_fab(,n,,x,y);
if (op == )
printf("%I64d\n",query(,n,,x,y));
}
}
return ;
}

HDU4893--Wow! Such Sequence! (线段树 延迟标记)的更多相关文章

  1. HDU 3468:A Simple Problem with Integers(线段树+延迟标记)

    A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...

  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!(线段树)

    题目链接:hdu 4983 Wow! Such Sequence! 题目大意:就是三种操作 1 k d, 改动k的为值添加d 2 l r, 查询l到r的区间和 3 l r. 间l到r区间上的所以数变成 ...

  4. Tree(树链剖分+线段树延迟标记)

    Tree http://poj.org/problem?id=3237 Time Limit: 5000MS   Memory Limit: 131072K Total Submissions: 12 ...

  5. codevs 1690 开关灯 线段树+延迟标记

    1690 开关灯  时间限制: 1 s  空间限制: 128000 KB   题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这 ...

  6. HDU4893:Wow! Such Sequence!(段树lazy)

    Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger f ...

  7. Jiu Yuan Wants to Eat(树链剖分+线段树延迟标记)

    Jiu Yuan Wants to Eat https://nanti.jisuanke.com/t/31714 You ye Jiu yuan is the daughter of the Grea ...

  8. codevs 2216 行星序列 线段树+延迟标记(BZOJ 1798)

    2216 行星序列  时间限制: 2 s  空间限制: 256000 KB     题目描述 Description “神州“载人飞船的发射成功让小可可非常激动,他立志长大后要成为一名宇航员假期一始, ...

  9. hdu-3397 Sequence operation 线段树多种标记

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=3397 题目大意: 0 a b表示a-b区间置为0 1 a b表示a-b区间置为1 2 a b表示a- ...

随机推荐

  1. RequireJS入门(三)

    这篇来写一个具有依赖的事件模块event.event提供三个方法bind.unbind.trigger来管理DOM元素事件. event依赖于cache模块,cache模块类似于jQuery的$.da ...

  2. MongoDB的数据类型

    最近在写一个lua的MongoDB模块.MongoDB版本3.2,lua则是5.3.1.底层以C++来写,再把函数暴露给lua调用.但是在lua中打印结果时,发现了些奇怪的现象.首先,数据库中的内容: ...

  3. 你需要知道的九大排序算法【Python实现】源码

    #coding: utf-8 #!/usr/bin/python import randomimport math #随机生成0~100之间的数值def get_andomNumber(num): l ...

  4. html中显示xml

    在工作中经常会遇到一些特殊的要求,比如在html中显示xml,xml的格式跟html很相似,同样包含了标签.属性.值,所以xml的标签等内容会在html中被转义 如果要在html中让xml的内容(包括 ...

  5. hdu 4848 搜索+剪枝 2014西安邀请赛

    http://acm.hdu.edu.cn/showproblem.php?pid=4848 比赛的时候我甚至没看这道题,事实上不难.... 可是说实话,如今对题意还是理解不太好...... 犯的错误 ...

  6. android音乐播放器开发 SweetMusicPlayer 实现思路

    一,实现效果 眼下还不是特别完好,主要有下面几个功能, 1,载入歌曲列表(实现a-z字母检索) 2,播放本地音乐 3.智能匹配本地歌词 4.智能载入在线歌词(事实上不算智能.发现歌词迷api提供的歌词 ...

  7. CentOS LiveCD LiveDVD DVD 等版本的区别

    1.CentOS系统镜像DVD有两个,安装系统只用到第一个镜像即CentOS-6.7-x86_64-bin-DVD1.iso,第二个镜像CentOS-6.7-x86_64-bin-DVD2.iso是系 ...

  8. vim中systemverilog的高亮显示

    vim中systemverilog的高亮显示 Linux中的vim显示systemverilog语法高亮 windows中的gvim显示systemverilog语法高亮 Linux系统 查看打开vi ...

  9. “=”号和“:”的区别,Html.Raw()的使用

    “=”号,将原封不动输出字符串到页面 “:”号:将字符串进行编码后输出到页面 public ActionResult HtmlEncodeDemo() { ViewData["strScri ...

  10. (转)SQL Server 2008将数据导出为脚本 [SQL Server]

    之前我们要将一个表中的数据导出为脚本,那么只有在网上找一个导出数据的Script,然后运行就可以导出数据脚本了.现在在SQL Server 2008的Management Studio中增加了一个新特 ...