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.
 

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.
 

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
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 黄油嘴打不进去怎么办 螺杆冷水机氟系统有空气怎么办 脚踏式加油枪皮碗不下去怎么办? 自攻螺丝滑丝怎么办? 大工打小工老板不管怎么办 虾缸的过滤吸虾怎么办 加热棒坏了鱼怎么办 钢材软打孔断钻头怎么办 空调余额下水管检查口按不上怎么办 风机盘管噪音大怎么办 混凝土水泥放少了怎么办 门式钢梁端板连接下料短啦怎么办? 灌桩导管堵了怎么办 公路车尾钩歪了怎么办 铃木羚羊车大灯不亮怎么办 玻璃瓶打碎了里面食物怎么办 玻璃门上轴坏了怎么办 配筋面积小了怎么办 ps大文件存不了怎么办 挑架钢丝绳拉环未预埋怎么办 出现偏拉的梁怎么办 尾插不好上锡怎么办 汽车玻璃上的焊点很难去除怎么办 拆苹果硬盘焊点掉了怎么办 玻璃被电焊滴到怎么办 gta5特质卡宾装了消音器怎么办 联想V形底座不好用怎么办 华为手环不计步怎么办 小米手环不计步怎么办 小米手环骑自行车不计步数怎么办 放逐之城着火了怎么办 城市天际线地价过低怎么办 放逐之城铁采完怎么办 车钥匙反锁车内怎么办 眼镜用热水洗了怎么办 眼镜放平后眼镜腿不平怎么办 瞄准镜十字歪了怎么办 瞄准镜调到底了怎么办 墨镜镜片刮花了怎么办 usb小风扇不转怎么办 金属眼镜压歪了怎么办