博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
hdu4893Wow! Such Sequence! (线段树)
阅读量:5900 次
发布时间:2019-06-19

本文共 4051 字,大约阅读时间需要 13 分钟。

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 F
0 = 1,F
1 = 1,Fibonacci number Fn is defined as F
n = F
n - 1 + F
n - 2 for n ≥ 2.
Nearest Fibonacci number of number x means the smallest Fn where |F
n - 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| < 2
31, 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
Source
Recommend
hujie | We have carefully selected several similar problems for you:
解题:线段树结点含两个和,一个真正和,还有一个是3号操作的备用和。

#include
#include
using namespace std;struct tree{ int l,r,t; __int64 s,ts;}ss[100010*3];__int64 f[100]={1,1};void setit(int l,int r,int d){ ss[d].l=l; ss[d].r=r; ss[d].t=0;//推断子结点是否须要进行3号操作 ss[d].s=0;//当前区间内的总和 ss[d].ts=1;//当前区间进行3号操作的备用和 if(l==r) return; setit(l,(l+r)/2,d*2); setit((l+r)/2+1,r,d*2+1); ss[d].ts=ss[d*2].ts+ss[d*2+1].ts;}__int64 doublekill(__int64 k)//查找与k最相近的Fibonacci{ int l=0,r=77,mid; __int64 x1,x2; while(l<=r) { mid=(l+r)/2; if(f[mid]==k)return f[mid]; if(f[mid]
0) { x2=f[l-1]-k; if(x2<0)x2=-x2; if(x2<=x1)return f[l-1]; } return f[l];}void insert1(__int64 e,int k,int d)//点更新{ int l,r,mid; l=ss[d].l; r=ss[d].r; mid=(l+r)/2; if(l==r) { if(ss[d].t) { ss[d].s=ss[d].ts; ss[d].t=0; } ss[d].s+=e; ss[d].ts=doublekill(ss[d].s);//同一时候更新备用 return; } if(ss[d].t)//假设当前段须要进行3号操作 { ss[d*2].t=1; ss[d*2+1].t=1; ss[d].t=0; ss[d*2].s=ss[d*2].ts; ss[d*2+1].s=ss[d*2+1].ts; } if(k>mid)insert1(e,k,d*2+1); else insert1(e,k,d*2); ss[d].s=ss[d*2].s+ss[d*2+1].s; ss[d].ts=ss[d*2].ts+ss[d*2+1].ts;}void insert2(int l,int r,int d)//区间进行3号操作{ int ll,rr,mid; ll=ss[d].l; rr=ss[d].r; mid=(ll+rr)/2; if(ll>=l&&rr<=r) { ss[d].s=ss[d].ts; ss[d].t=1;//子结点须要3号操作更新 return; } if(ss[d].t) { ss[d*2].t=1; ss[d*2+1].t=1; ss[d].t=0; ss[d*2].s=ss[d*2].ts; ss[d*2+1].s=ss[d*2+1].ts; } if(l<=mid)insert2(l,r,d*2); if(r>mid)insert2(l,r,d*2+1); ss[d].s=ss[d*2].s+ss[d*2+1].s;}__int64 find(int l,int r,int d)//求和{ __int64 ll,rr,mid,s=0; ll=ss[d].l; rr=ss[d].r; mid=(ll+rr)/2; if(ll>=l&&rr<=r) { return ss[d].s; } if(ss[d].t) { ss[d*2].t=1; ss[d*2+1].t=1; ss[d].t=0; ss[d*2].s=ss[d*2].ts; ss[d*2+1].s=ss[d*2+1].ts; } if(l<=mid)s+=find(l,r,d*2); if(r>mid)s+=find(l,r,d*2+1); ss[d].s=ss[d*2].s+ss[d*2+1].s; return s;}int main (void){ int n,m,i,j,k,l; __int64 d; for(i=2;i<78;i++) f[i]=f[i-1]+f[i-2]; while(scanf("%d%d",&n,&m)>0) { setit(1,n,1); while(m--) { scanf("%d%d",&j,&k); if(j==1) { scanf("%I64d",&d);insert1(d,k,1); } else { scanf("%d",&l); if(j==2)printf("%I64d\n",find(k,l,1)); else insert2(k,l,1); } } } return 0;}

转载地址:http://nuesx.baihongyu.com/

你可能感兴趣的文章
usermod命令
查看>>
Exchange 2013 配置默认电子邮件地址策略
查看>>
有意思的图片组合飞入效果
查看>>
python3.5opencv3图像文字标注
查看>>
安装UChome中遇到的俩问题
查看>>
暑期书单
查看>>
iptables从入门到精通
查看>>
MonkeyRunner+PowerTutor简单耗电测试
查看>>
一、页面做完右侧留白
查看>>
WPF 对控件其类型的继承方式(七)
查看>>
apache安装,镇博
查看>>
test1
查看>>
Python - while循环
查看>>
NFS配置及详解
查看>>
结合Ngx_lua运用Nginx预加载热点数据
查看>>
.net 安装程序工具 (Installutil.exe)
查看>>
Linux下如何批量修改文件名
查看>>
shell编程
查看>>
Memcached简介
查看>>
top命令批量执行
查看>>