HDU4893--Wow! Such Sequence! (线段树 延迟标记)
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
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.
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.
题意:
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! (线段树 延迟标记)的更多相关文章
- HDU 3468:A Simple Problem with Integers(线段树+延迟标记)
A Simple Problem with Integers Case Time Limit: 2000MS Description You have N integers, A1, A2, ... ...
- Wow! Such Sequence!(线段树4893)
Wow! Such Sequence! Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others ...
- 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区间上的所以数变成 ...
- Tree(树链剖分+线段树延迟标记)
Tree http://poj.org/problem?id=3237 Time Limit: 5000MS Memory Limit: 131072K Total Submissions: 12 ...
- codevs 1690 开关灯 线段树+延迟标记
1690 开关灯 时间限制: 1 s 空间限制: 128000 KB 题目描述 Description YYX家门前的街上有N(2<=N<=100000)盏路灯,在晚上六点之前,这 ...
- HDU4893:Wow! Such Sequence!(段树lazy)
Problem Description Recently, Doge got a funny birthday present from his new friend, Protein Tiger f ...
- 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 ...
- codevs 2216 行星序列 线段树+延迟标记(BZOJ 1798)
2216 行星序列 时间限制: 2 s 空间限制: 256000 KB 题目描述 Description “神州“载人飞船的发射成功让小可可非常激动,他立志长大后要成为一名宇航员假期一始, ...
- 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- ...
随机推荐
- 将vs2012建的项目转换为vs2010项目
vs2012建的项目vs2010打不开,但vs2010的项目vs2012能打开,所以我觉得vs2012没有对解决方案进行重大的调整,尝试修改了一下.sln文件,使用vs2010就能打开了,方法如下: ...
- [ES6] Objects create-shorthand && Destructuring
Creating Object: Example 1: let name = "Brook"; let totalReplies = 249; let avatar = " ...
- Java基础知识强化50:运行javac 报告javac不是内部或外部命令(已解决)
1. 问题:运行javac 报告javac不是内部或外部命令,但是运行java.java-version正常 ? 看看下面三个环境变量是否设置正确: (1)环境变量 JAVA_HOME 设置JAVA ...
- SQL数值函数
/*abs(n)返回参数n所指定数值的绝对值(如果参数值为NULL,则返回结果为NULL,下同).*/--SELECT ABS(-3.14) FROM DUAL; --3.14 /*round(n[, ...
- 基本SQL语句练习(order by,group by,having)
一.GROUP BY 和ORDER BY 1.使用Order by 进行排序,默认升序ASC,降序则使用DESC;(还可以这样:order by 1表示按第一列排序:order by 2 desc表示 ...
- rhel Linux 网络配置
--网络配置 vi /etc/sysconfig/network-scripts/ifcfg-eth0 1)DHCPDEVICE=eth0BOOTPROTO=dhcpONBOOT=yes2)静态IP ...
- sqlserver中的序列
序列是由用户定义的绑定到架构的对象.序列依据定义的间隔按升序或降序生成,并可配置为用尽时重新启动(循环).序列不与特定表关联.序列与表之间的关系由应用程序进行控制. 创建序列的语法: CREATE S ...
- JobDeer 的《程序员必读的职业规划书》
JobDeer 的<程序员必读的职业规划书> 关键字 持续性,人生规划,职业规划 概念 职业规划三部分: 职业定位 目标设定 通道设计 职业价值论: 能为公司做什么 同样的能力再不同公司价 ...
- .NET Linq获取一个集合中的一个或多个属性,赋值到新的类对象
//得到自定义的list var list = schoolGradeClassModelList.Select(x => new DropDownListData() { DataTextFi ...
- Sql Server导出表结构Excel
SELECT 表名 Then D.name Else '' End, 表说明 Then isnull(F.value,'') Else '' End, 字段序号 = A.colorder, 字段名 = ...