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. [置顶] Extjs4 异步刷新书的情况下 保持树的展开状态

    前言:首先我觉得搞IT不管你是菜鸟还是特种兵,最重要的品质就是分享知识,因为可能在你这,这点知识不算什么,可是对于像我这样的菜鸟来说,无疑就可能会从中得到一点启发,甚至更大!此段代码,是我在某个网站上 ...

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

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

  3. (转)VS无法启动调试:“生成下面的模块时,启用了优化或没有调试信息“

    中调试项目遇到错误提示,Visual Studio 2010(或VS2008或VS2005)启动调试的时候,弹出提示信息: 生成下面的模块时,启用了优化或没有调试信息: C:\WINDOWS\Micr ...

  4. 10个最实用的Linux命令

    收集了一些对于Linux新手最基本但最有用的Linux命令.你完全可以键入这些命令来管理你的服务器.这些命令对于学习vps或服务器管理的新手最为简便.1.List命令 ls -a //列出所有文件 l ...

  5. 《第一行代码》学习笔记38-服务Service(5)

    1.希望服务一旦启动就立刻去执行某个动作,可以将逻辑写在onStartCommand()方法里. 2.onCreate()和onStartCommand()的区别:onCreate()方法是在服务第一 ...

  6. UIColor各种颜色转换

    1.Hex值颜色转换 #import <UIKit/UIKit.h> @interface UIColor (Extension) // 根据无符号的32位整数转换为对应的RGB颜色 + ...

  7. java 修改文件名

    // 修改文件名 public static boolean modifyFileName(String serverPath, String oldFileName, String newLogin ...

  8. (六)backbone - API学习 - Backbone路由

    Backbone路由本质 Backbone路由分为两个大块,Router以及History用户在Router中定义相关规则,然后开启history.start进行路由监控,执行默认的回调所以,Rout ...

  9. Extjs 兼容IE10

    在对应的地方将Ext.isIE 修改成: Ext.isIE && !(/msie 9/.test(navigator.userAgent.toLowerCase())  &&a ...

  10. PHP扩展开发(6) - VS2012下strncasecmp和fopen函数warning

    1. fopen   warning C4996: 'fopen': This function or variable may be unsafe. Consider using fopen_s i ...