hdu 4893 Wow! Such Sequence!

来源:互联网 发布:java 多线程是什么 编辑:程序博客网 时间:2024/05/07 10:37

Wow! Such Sequence!

Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1894    Accepted Submission(s): 585


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
 


题解及代码:

#include <iostream>#include <cstdio>#include <cmath>#include <cstring>#define maxn 100110#define lson l,m,rt<<1#define rson m+1,r,rt<<1|1using namespace std;typedef long long ll;ll fibo[100];struct node{    ll s,st,r;} son[maxn<<2];inline void init(){    fibo[0]=1;    fibo[1]=1;    for(int i=2; i<=90; i++)    {        fibo[i]=fibo[i-1]+fibo[i-2];    }}inline ll find_fibo(ll s){    if(s==0) return 1;    int l=0,r=79,mid;    while(l<=r)    {        mid=(l+r)/2;        if(fibo[mid]==s) return fibo[mid];        if(fibo[mid]<s) l=mid+1;        if(fibo[mid]>s) r=mid-1;    }    ll p=s-fibo[l]<0?fibo[l]-s:s-fibo[l];    ll q=s-fibo[r]<0?fibo[r]-s:s-fibo[r];    return p<q?fibo[l]:fibo[r];}inline void PushUp(int rt,int f){    if(f==1||f==3)        son[rt].s=son[rt<<1].s+son[rt<<1|1].s;    if(f==2||f==3)        son[rt].st=son[rt<<1].st+son[rt<<1|1].st;}inline void PushDown(int rt){    if(son[rt].r)    {        son[rt<<1].r=son[rt<<1|1].r=1;        son[rt<<1].s=son[rt<<1].st;        son[rt<<1|1].s=son[rt<<1|1].st;        son[rt].r=0;    }}inline void Build(int l,int r,int rt){    son[rt].r=0;    if(l==r)    {        son[rt].s=0;        son[rt].st=1;        return;    }    int m=(l+r)>>1;    Build(lson);    Build(rson);    PushUp(rt,3);}inline void Update(int p,ll add,int l,int r,int rt){    if(l==r)    {        son[rt].s+=add;        son[rt].st=find_fibo(son[rt].s);        return;    }    PushDown(rt);    int m=(l+r)/2;    if(p<=m) Update(p,add,lson);    else Update(p,add,rson);    PushUp(rt,3);}inline ll Query(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R)    {        return son[rt].s;    }    PushDown(rt);    int m=(l+r)/2;    ll ret=0;    if(L<=m) ret+=Query(L,R,lson);    if(R>m) ret+=Query(L,R,rson);    PushUp(rt,1);    return ret;}inline void Change(int L,int R,int l,int r,int rt){    if(L<=l&&r<=R)    {        son[rt].r=1;        son[rt].s=son[rt].st;        return;    }    PushDown(rt);    int m=(l+r)/2;    if(L<=m) Change(L,R,lson);    if(R>m) Change(L,R,rson);    PushUp(rt,1);}int main(){    init();    int m,n,s,l,r;    while(scanf("%d%d",&n,&m)!=EOF)    {        Build(1,n,1);        for(int i=0; i<m; i++)        {            scanf("%d%d%d",&s,&l,&r);            if(s==1) Update(l,r*1LL,1,n,1);            if(s==2)            {                ll t=Query(l,r,1,n,1);                printf("%I64d\n",t);            }            if(s==3)            {                Change(l,r,1,n,1);            }        }    }    return 0;}/*题解随后补上*/


0 0
原创粉丝点击