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- ...
随机推荐
- Java 泛型数组
Java 不支持泛型数组.也就是说, List<String>[] ls = new ArrayList<String>[10]; 是不支持的,而 List<String ...
- 现代的新语言--Swift初探
新的语言 WWDC简短的介绍,新的语言Swift就问世了,尽管新语言的名字导致贴吧下歌手粉丝和开发人员们争抢地盘- -,只是雨燕就是这么来了. WWDC keynote里给Swift打上了非常多标签: ...
- Android 中文 API (40) —— RatingBar
Android 中文 API (40) —— RatingBar 前言 本章内容是 android.widget.RatingBar,译为"评分条",版本为Android 2.2 ...
- Java基础知识强化44:StringBuffer类之把数组拼接成指定格式的字符串的案例
1. 先看案例代码如下: package cn.itcast_07; /* * 把数组拼接成一个字符串 */ public class StringBufferTest2 { public stati ...
- vmstat
vmstat(virtual memory statitics)命令是最常见的Linux/Unix监控工具,可以展现给定时间间隔的服务器的状态值,包括服务器的CPU使用率,内存使用,虚拟内存交换情况, ...
- openwrt time sycronize
三行命令搞定这个. opkg update opkg install ntpclient ntpclient -s -c 0 -h ntp.sjtu.edu.cn 最后把这个 放到 rc.local ...
- .net对文件的操作之文件读写
读写文件的步骤一般需要5步: 创建文件流 创建读写器 执行读或写的操作 关闭读写器 关闭文件流 需要引用:System.IO这个命名空间 代码演示: string path = @"F:\a ...
- 转载——SqlServer之like、charindex、patindex
转载自:http://www.2cto.com/database/201305/214967.html SqlServer之like.charindex.patindex 1.环境介绍 测试环境 ...
- (转)使用 .NET 的 RNGCryptoServiceProvider 生成随机数
1. [代码]一个简单的方法,但不够可靠 跳至 [1] [2] [全屏预览] ? 1 2 3 4 5 6 7 8 9 10 11 static void Main(string[] args) ...
- ---添加一条记录返回一条记录的ID
INSERT INTO Web_AD(PID,ADType,ADTitle,ADTitle1,ADTitle2,ADTarget,LinkURL,DispalyWords,ADCode,UploadI ...