hdu 4893 线段树
来源:互联网 发布:兄弟连java百度云盘 编辑:程序博客网 时间:2024/06/05 09:54
Wow! Such Sequence!
Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 620 Accepted Submission(s): 191
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
Source
2014 Multi-University Training Contest 3
#include<stdio.h>#define N 100100#include<math.h>typedef __int64 ll;#define inf ((ll)1<<63)-1struct node{ ll x,y,p,sum;}a[N*3];ll f[100];void fun(){ ll i; f[0]=1; f[1]=1; for(i=2;i<=91;i++) { f[i]=f[i-1]+f[i-2]; //printf("%I64d\n",f[i]); }}ll binary(ll l,ll r,ll k){ if(k<0) return 1; ll i,aa,ans=inf,u; //printf("%I64d\n",ans); for(i=0;i<=91;i++) { aa=f[i]-k; if(aa<0) aa=-aa; if(aa<ans) { ans=aa; u=f[i]; } } return u;}void build(ll t,ll x,ll y){ a[t].x=x; a[t].y=y; a[t].sum=0; a[t].p=0; if(x==y) return; ll mid=(a[t].x+a[t].y)>>1,temp=t<<1; build(temp,x,mid); build(temp+1,mid+1,y);}void update(ll t,ll k,ll d){ if(a[t].x==a[t].y) { a[t].sum+=d; a[t].p=0; return; } ll temp=t<<1,mid=(a[t].x+a[t].y)>>1; if(k<=mid) update(temp,k,d); else update(temp+1,k,d); a[t].sum=a[temp].sum+a[temp+1].sum; a[t].p=(a[temp].p)&(a[temp+1].p);}ll query(ll t ,ll x,ll y){ if(a[t].x==x&&a[t].y==y) return a[t].sum; ll mid=(a[t].x+a[t].y)>>1,temp=t<<1; if(y<=mid) return query(temp,x,y); else if(x>mid) return query(temp+1,x,y); else return query(temp,x,mid)+query(temp+1,mid+1,y);}void reupdate(ll t,ll x,ll y){ ll ans; if(a[t].p) return; if(a[t].x==a[t].y) { a[t].p=1; ans=binary(0,91,a[t].sum); //printf("%I64d %I64d\n",a[t].sum,ans); a[t].sum=ans; return ; } ll mid=(a[t].x+a[t].y)>>1,temp=t<<1; if(y<=mid) reupdate(temp,x,y); else if(x>mid) reupdate(temp+1,x,y); else { reupdate(temp,x,mid); reupdate(temp+1,mid+1,y); } a[t].p=(a[temp].p)&(a[temp+1].p); a[t].sum=a[temp].sum+a[temp+1].sum;}int main(){ fun(); ll n,m,i,op,k,d,l,r; while(scanf("%I64d%I64d",&n,&m)!=EOF) { build(1,1,n); for(i=1;i<=m;i++) { scanf("%I64d",&op); if(op==1) { scanf("%I64d%I64d",&k,&d); update(1,k,d); } else if(op==2) { scanf("%I64d%I64d",&l,&r); printf("%I64d\n",query(1,l,r)); } else { scanf("%I64d%I64d",&l,&r); reupdate(1,l,r); } } } return 0;}
0 0
- HDU 4893 线段树
- hdu 4893 线段树
- hdu 4893 线段树
- hdu 4893 线段树
- 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 线段树+二分fib
- HDU 4893(线段树区间更新)
- HDU 4893 线段树裸题
- hdu 1754 线段树
- hdu 1754 线段树
- HDU 1698 线段树
- hdu 1754 线段树
- 【线段树】hdu 1754
- ubuntu下mysql远程连接
- hdoj.2032 杨辉三角 20140729
- 一入python深似海--小爬虫
- 设计计时器
- java蓝桥杯 B组(二)
- hdu 4893 线段树
- Effective C++(八)定制new和delete
- ubuntu 由于已经达到 MaxReports 限制,没有写入 apport 报告
- 能不能上头版
- hdoj.2033 人见人爱A+B 20140729
- getline()函数的用法
- error LNK2001;error LNK 2019;error MSB8020
- Paid Roads(DFS)
- HTML知识汇总