HDU 4893 Wow! Such Sequence! 线段树
来源:互联网 发布:阿里云客服怎么选班 编辑:程序博客网 时间:2024/05/22 17:44
Wow! Such Sequence!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1455 Accepted Submission(s): 458
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.
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.
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 12 1 15 41 1 71 3 173 2 42 1 5
Sample Output
022
Author
Fudan University
Source
2014 Multi-University Training Contest 3
坑了一下午树状数组。。
#include <cstdlib>#include <cctype>#include <cstring>#include <cstdio>#include <cmath>#include <algorithm>#include <vector>#include <string>#include <iostream>#include <sstream>#include <map>#include <set>#include <queue>#include <stack>#include <fstream>#include <numeric>#include <iomanip>#include <bitset>#include <list>#include <stdexcept>#include <functional>#include <utility>#include <ctime>using namespace std;typedef long long LL;const int maxn=100000+100;struct node{ int visit; LL s,temps;}tree[maxn<<2];LL f[100];void init(){ f[0]=1,f[1]=1; for(int i=2;i<=90;i++) f[i]=f[i-1]+f[i-2];}LL find_Fibonacci(LL k){ int l=0,r=77,mid; LL x1,x2; while(l<=r) { mid=(l+r)>>1; if(f[mid]==k) return f[mid]; if(f[mid]<k) l=mid+1; else r=mid-1; } x1=f[l]-k; if(x1<0)x1=-x1; if(l>0) { x2=f[l-1]-k; if(x2<0)x2=-x2; if(x2<=x1)return f[l-1]; } return f[l];}void PushUp1(int rt){ tree[rt].temps=tree[rt<<1].temps+tree[rt<<1|1].temps;}void PushUp2(int rt){ tree[rt].s=tree[rt<<1].s+tree[rt<<1|1].s;}void build(int L,int R,int rt){ tree[rt].visit=0; tree[rt].s=0; tree[rt].temps=1; if(L==R) return; int M=(L+R)>>1; build(L,M,rt<<1); build(M+1,R,rt<<1|1); PushUp1(rt);}void update(int k,LL d,int L,int R,int rt){ if(L==R) { if(tree[rt].visit) { tree[rt].s=tree[rt].temps; tree[rt].visit=0; } tree[rt].s+=d; tree[rt].temps=find_Fibonacci(tree[rt].s); return; } if(tree[rt].visit) { tree[rt<<1].visit=1,tree[rt<<1|1].visit=1,tree[rt].visit=0; tree[rt<<1].s=tree[rt<<1].temps; tree[rt<<1|1].s=tree[rt<<1|1].temps; } int M=(L+R)>>1; if(k<=M) update(k,d,L,M,rt<<1); else update(k,d,M+1,R,rt<<1|1); PushUp1(rt); PushUp2(rt);}LL query(int l,int r,int L,int R,int rt){ LL res=0; if(l<=L&&r>=R) { return tree[rt].s; } if(tree[rt].visit) { tree[rt<<1].visit=1,tree[rt<<1|1].visit=1,tree[rt].visit=0; tree[rt<<1].s=tree[rt<<1].temps; tree[rt<<1|1].s=tree[rt<<1|1].temps; } int M=(L+R)>>1; if(l<=M) res+=query(l,r,L,M,rt<<1); if(r>M) res+=query(l,r,M+1,R,rt<<1|1); PushUp2(rt); return res;}void change(int l,int r,int L,int R,int rt){ if(l<=L&&r>=R) { tree[rt].s=tree[rt].temps; tree[rt].visit=1; return; } if(tree[rt].visit) { tree[rt<<1].visit=1,tree[rt<<1|1].visit=1,tree[rt].visit=0; tree[rt<<1].s=tree[rt<<1].temps; tree[rt<<1|1].s=tree[rt<<1|1].temps; } int M=(L+R)>>1; if(l<=M) change(l,r,L,M,rt<<1); if(r>M) change(l,r,M+1,R,rt<<1|1); PushUp2(rt);}int main(){ init(); int n,m; while(scanf("%d%d",&n,&m)!=EOF) { build(1,n,1); int c,k,l,r; LL d; while(m--) { scanf("%d",&c); if(c==1) { scanf("%d%I64d",&k,&d); update(k,d,1,n,1); } if(c==2) { scanf("%d%d",&l,&r); printf("%I64d\n",query(l,r,1,n,1)); } if(c==3) { scanf("%d%d",&l,&r); change(l,r,1,n,1); } } } return 0;}
0 0
- hdu 4893 Wow! Such Sequence! 线段树
- hdu 4893 Wow! Such Sequence!(线段树)
- HDU Wow! 4893 Such Sequence!(线段树)
- 【HDU】4893 Wow! Such Sequence! 线段树
- HDU 4893 Wow! Such Sequence!(线段树)
- HDU 4893 Wow! Such Sequence! 线段树
- 【HDU 4893 多校联合】 Wow! Such Sequence!【线段树】
- HDU 4893 Wow! Such Sequence! (线段树)
- HDU 4893 Wow! Such Sequence! 解题报告(线段树)
- hdu 4893 Wow! Such Sequence! (线段树+ 心得)
- ACM 线段树模板 hdu 4893 Wow! Such Sequence!
- 【线段树】 HDOJ 4893 Wow! Such Sequence!
- [HDOJ 4893] Wow! Such Sequence! [线段树]
- Wow! Such Sequence!(线段树4893)
- hdu 4893Wow! Such Sequence!
- HDU 4893 Wow! Such Sequence!
- hdu 4893 Wow! Such Sequence!
- HDU 4893(Wow! Such Sequence!)
- js中join函数的使用方法
- 最简单的MakeFile
- 杭电 1166敌兵布阵(线段树)
- 单片机程序设计中的“分层思想”
- poj 1539 Evaluating Simple C Expressions
- HDU 4893 Wow! Such Sequence! 线段树
- inno setup学习需要掌握的知识点
- spring security 3.1.6 jar包说明
- 黑马程序员-----内部类的访问规则(黑马视频)
- TortoiseGit github 免输用户名密码 Push
- Referenced file contains errors (http://getahead.ltd.uk/dwr/dwr20.dtd).
- 什么是3g(1)
- HDU 1853 Cyclic Tour【最小费用最大流 OR 带权二分图匹配】
- storm源码分析之topology提交过程