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. zookeeper[2] zookeeper原理(转)

    转自:http://cailin.iteye.com/blog/2014486 ZooKeeper是一个分布式的,开放源码的分布式应用程序协调服务,它包含一个简单的原语集,分布式应用程序可以基于它实现 ...

  2. CSS常用操作-导航栏

    1.垂直导航栏 index.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...

  3. MyBatis的简单操作

    这里将的是简单的增.删.改.查等基本操作 首先创建java项目,导入相应的依赖包,这里可以参考上一篇博客 1.添加数据 在jike.book.pojo包中,新建java类 JiKeUser.java: ...

  4. centos下部署redis服务环境的操作记录

    Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.从2010年3月15日起,Redis的开发工作由VMware主 ...

  5. Jmeter接口测试-badboy录制脚本(二)

    1.脚本录制,采用badboy进行录制,操作步骤很简单 2.badboy简介: Badboy是一款免费WEB自动化测试工具. 官方下载地址:http://www.badboy.com.au badbo ...

  6. 关于退运美国转基因玉米含有MRI 162转基因成分的质疑

    6月30日,新华社刊出文章"我国退运125.2万吨进口美国转基因玉米",读后有感. 文章说:国家质检总局办公厅副主任陆春明30日介绍,截至今年6月16日,全国出入境检验检疫机构共在 ...

  7. AxisFault另外一个问题

    出现以下情况,能够是proxy.setEndpoint(endpoint);中endpoint不正确导致 因该是:endpoint = http://127.0.0.1/8080/项目名/servic ...

  8. HDU 4099 Revenge of Fibonacci (数学+字典数)

    传送门:http://acm.hdu.edu.cn/showproblem.php?pid=4099 这个题目就是一个坑或. 题意:给你不超过40的一串数字,问你这串数字是Fibonacci多少的开头 ...

  9. C++11 : variadic templates(可变参数模板)

      Introduction: Before the possibilities of the new C++ language standard, C++11, the use of templat ...

  10. HashMap学习笔记

        概述   HashMap是Map接口的一个哈希表的实现,内部是一个数组表示的.数组中的元素叫做一个Node,一个Node可以一个是一个简单的表示键值对的二元组,也可以是一个复杂的TreeNod ...