hdu 4893 Wow! Such Sequence!

Wow! Such Sequence!

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


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 1 2 1 1 5 4 1 1 7 1 3 17 3 2 4 2 1 5
 

Sample Output
0 22

对于每个点距离它最近的Fibonacci数需要加多少,那么就知道一个区间需要加多少了。。

const int M=90;
const int N=100005;
ll f[100],sum[N<<2],need[N<<2];
bool lazy[N<<2];

inline ll Find(ll x){
    if(x<=1) return 1;
    int id=lower_bound(f,f+M,x)-f;
    if(x-f[id-1]<=f[id]-x) return f[id-1];
    return f[id];
}

inline void push_up(int t){
    sum[t]=sum[L]+sum[R];
    need[t]=need[L]+need[R];
}

inline void push_down(int t){
    if(lazy[t]){
        lazy[L]=lazy[R]=1;
        sum[L]+=need[L];
        sum[R]+=need[R];
        need[L]=need[R]=0;
        lazy[t]=0;
    }
}

inline void build(int t,int l,int r){
    sum[t]=0;lazy[t]=0;
    need[t]=(r-l+1);
    if(l!=r){
        MID(l,r);
        build(L,l,mid);
        build(R,mid+1,r);
    }
}

inline void add(int t,int l,int r,int x,ll v){
    if(l==r){
        sum[t]+=v;
        need[t]=Find(sum[t])-sum[t];
        return;
    }
    push_down(t);
    MID(l,r);
    if(x<=mid) add(L,l,mid,x,v);
    else add(R,mid+1,r,x,v);
    push_up(t);
}

inline void change(int t,int l,int r,int x,int y){
    if(l>=x && r<=y){
        lazy[t]=1;
        sum[t]+=need[t];
        need[t]=0;
        return;
    }
    push_down(t);
    MID(l,r);
    if(y<=mid) change(L,l,mid,x,y);
    else if(x>mid) change(R,mid+1,r,x,y);
    else{
        change(L,l,mid,x,mid);change(R,mid+1,r,mid+1,y);
    }
    push_up(t);
}

inline ll query(int t,int l,int r,int x,int y){
    if(l>=x && r<=y) return sum[t];
    push_down(t);
    MID(l,r);
    if(y<=mid) return query(L,l,mid,x,y);
    else if(x>mid) return query(R,mid+1,r,x,y);
    else return query(L,l,mid,x,mid)+query(R,mid+1,r,mid+1,y);
}

int n,m;
int op,x,y;
int main(){
    f[0]=f[1]=1;
    rep(i,2,M) f[i]=f[i-1]+f[i-2];
    while(~scanf("%d%d",&n,&m)){
        build(1,1,n);
        while(m--){
            op=input();
            if(op==1){
                x=input(),y=input();
                add(1,1,n,x,y*1LL);
            }
            else if(op==3){
                x=input(),y=input();
                change(1,1,n,x,y);
            }
            else{
                x=input(),y=input();
                output(query(1,1,n,x,y));
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值