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
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define ll __int64
#define maxn 100005
#define ls l,mid,2*i
#define rs mid+1,r,2*i+1
#define lson 2*i
#define rson 2*i+1
struct node
{
int l,r;
ll e,f;//e为该区间的和,f为其近期的斐波那契数
int flag,len;//flag,标记这个区间内是否斐波那契数,len为长度
} a[maxn<<2];
int n,m;
ll f[90] = {1,1}; ll pabs(ll a)
{
return a<0?-a:a;
} void PushDown(int i,int m)
{
if(a[i].flag)
{
a[lson].flag = a[rson].flag = a[i].flag;
a[lson].len = a[i].flag*(m-m>>1);
a[rson].len = a[i].flag*(m>>1);
a[lson].e = a[lson].f;
a[rson].e = a[rson].f;
a[i].flag = 0;
}
} void PushUp(int i)
{
a[i].e = a[lson].e+a[rson].e;
a[i].f = a[lson].f+a[rson].f;
} void init(int l,int r,int i)
{
a[i].flag = a[i].len = 0;
a[i].l = l;
a[i].r = r;
a[i].e = 0;
if(l == r)
{
a[i].f = 1;
return;
}
int mid = (l+r)>>1;
init(ls);
init(rs);
PushUp(i);
} void add(int pos,int m,int l,int r,int i)
{
if(pos<l || pos>r) return ;
if(l == r)
{
if(a[i].flag)
{
a[i].e = m+a[i].f;
a[i].flag = 0;
a[i].len = 0;
}
else a[i].e+=m;
int p = lower_bound(f,f+80,a[i].e)-f;
if(!p)
a[i].f = 1;
else if(pabs(a[i].e-f[p])<pabs(a[i].e-f[p-1]))
a[i].f = f[p];
else
a[i].f = f[p-1];
return ;
}
PushDown(i,r-l+1);
int mid = (l+r)>>1;
if(pos<=mid) add(pos,m,ls);
else add(pos,m,rs);
PushUp(i);
} ll query(int L,int R,int l,int r,int i)
{
if(R<l || L>r) return 0;
else if(L<=l && R>=r) return a[i].e;
PushDown(i,r-l+1);
ll ans = 0;
int mid = (l+r)>>1;
if(L<=mid)
ans += query(L,R,ls);
if(R>mid)
ans += query(L,R,rs);
return ans;
} void change(int L,int R,int l,int r,int i)
{
if(R<l || L>r) return ;
if(L<=l && R>=r)
{
a[i].e = a[i].f;
a[i].flag = 1;
a[i].len = r-l+1;
return ;
}
PushDown(i,r-l+1);
int mid = (l+r)>>1;
if(L<=mid)
change(L,R,ls);
if(R>mid)
change(L,R,rs);
PushUp(i);
} int main()
{
int i,j,x,k,d,l,r;
for(i = 2; i<80; i++)
f[i] = f[i-1]+f[i-2];
while(~scanf("%d%d",&n,&m))
{
init(1,n,1);
while(m--)
{
scanf("%d",&x);
if(x == 1)
{
scanf("%d%d",&k,&d);
add(k,d,1,n,1);
}
else
{
scanf("%d%d",&l,&r);
if(x == 2)
printf("%I64d\n",query(l,r,1,n,1));
else
change(l,r,1,n,1);
}
}
} return 0;
}


HDU4893:Wow! Such Sequence!(段树lazy)的更多相关文章

  1. 2014多校3 Wow! Such Sequence!段树

    主题链接:http://acm.hdu.edu.cn/showproblem.php? pid=4893 这个问题还真是纠结啊--好久不写线段树的题了.由于这几天学伸展树.然后认为线段树小case了. ...

  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. hdu4893 Wow! Such Sequence!

    线段树结点上保存一个一般的sum值,再同一时候保存一个fbsum,表示这个结点表示的一段数字若为斐波那契数时的和 当进行3操作时,仅仅用将sum = fbsum就可以 其它操作照常进行,仅仅是单点更新 ...

  5. 线段树 + 区间更新: HDU 4893 Wow! Such Sequence!

    Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Oth ...

  6. HDU 4893 Wow! Such Sequence! (线段树)

    Wow! Such Sequence! 题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4893 Description Recently, Doge ...

  7. HDU 1394 Minimum Inversion Number (数据结构-段树)

    Minimum Inversion Number Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  8. PKU A Simple Problem with Integers (段树更新间隔总和)

    意甲冠军:一个典型的段树C,Q问题,有n的数量a[i] (1~n),C, a, b,c在[a,b]加c Q a b 求[a,b]的和. #include<cstdio> #include& ...

  9. HDU 6356.Glad You Came-线段树(区间更新+剪枝) (2018 Multi-University Training Contest 5 1007)

    6356.Glad You Came 题意就是给你一个随机生成函数,然后从随机函数里确定查询的左右区间以及要更新的val值.然后最后求一下异或和就可以了. 线段树,区间最大值和最小值维护一下,因为数据 ...

随机推荐

  1. Red Gate系列之八 SQL Connect 1.1.1.19 Edition 数据库连接及操作工具 完全破解+使用教程

    原文:Red Gate系列之八 SQL Connect 1.1.1.19 Edition 数据库连接及操作工具 完全破解+使用教程 Red Gate系列之八 SQL Connect 1.1.1.19 ...

  2. OCP-1Z0-051-名称解析-文章7称号

    7. Which two  statements are true regarding the USING and ON clauses in table joins? (Choose two.) A ...

  3. 使用C#和.NET 4编写的并行应用程序“多核并发编程的规则”

    “多核并发编程的规则” 规则的描述如下 1.      并发编程的思想—这条规则就是要谨记并发编程思想进行设计,就像前边章节所提交的. 2.      面向抽象编程-你可以利用.NET4中的TPL提供 ...

  4. C# Windows Phone 8 WP8 高级开发,制作不循环 Pivot ,图片(Gallery)导览不求人! 内附图文教学!!

    原文:C# Windows Phone 8 WP8 高级开发,制作不循环 Pivot ,图片(Gallery)导览不求人! 内附图文教学!! 一般我们在开发Winodws Phone APP 的时候往 ...

  5. doc-remote-debugging.html

    https://studio.zerobrane.com/doc-remote-debugging.html

  6. awk与sed:关于多行的样本

    几天前CSDN看到一个帖子http://bbs.csdn.net/topics/390848841,楼主贴了以下的问题: 例: 12345 67890 1234567890 123 4567890 怎 ...

  7. eclipse-jee 配置tomcat7,解决404错误

    在eclipse的Servers窗口新建一个tomcat7,配置tomcat的安装路径,然后启动tomcat,访问http://localhost:8080/,但是报404错误,恼火!没有找到要访问的 ...

  8. SQL 编码标准

    1. 你必须从别名表,易于使用的表,该表列 实例 select owner,object_id,name from a,b where a.id=b.id; 假设是不正确的别名表.我知道你是怎么访问表 ...

  9. 2014在百度之星资格赛的第二个问题Disk Schedule

    事实上,我认为它可以用来费用流问题.但光建地图上加班. ..不科学啊.. . 因副作用太大,否则,必然在.最后,想啊想,或者使用dp对.... 别想了一维dp... .我不知道我是怎么想.无论如何,这 ...

  10. hdu 5074 相邻的和最大dp

    http://acm.hdu.edu.cn/showproblem.php?pid=5074 给定一个序列 有些位数未知,给你全部两个数连续所得到的能量.问你怎么安排数字使得总能量最大 二维dp,dp ...