HDU4843Wow! Such Sequence!(樹狀陣列寫法)

bigbigship發表於2014-08-01
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
 

Author
Fudan University
 

Source
操作一和操作二都是基本的樹狀陣列的操作,操作三需要一定的優化,用set記錄已經改點是否被更改成斐波那契數,如果已經更改了,就不需要更新了
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <set>
#include <cstdlib>
using namespace std;

typedef long long LL;

const int MAX=100010;

LL tree[MAX],fib[100];

int del[MAX],n,m;

inline int Lowbit(int t){
    return t&-t;
}

LL Query(int i){
    LL s=0;
    while(i>0){
        s+=tree[i];
        i-=Lowbit(i);
    }
    return s;
}

void Update(int i,LL v){
    while(i<=n){
        tree[i]+=v;
        i+=Lowbit(i);
    }
}

LL myabs(LL a){
    return a > 0 ? a :-a;
}

void init(){
    fib[0]=1;fib[1]=1;
    for(int i=2;i<=91;i++)
        fib[i]=fib[i-1]+fib[i-2];
}
int main(){
    init();
    while(~scanf("%d%d",&n,&m)){
        memset(tree,0,sizeof(tree));
        set<int>st;
        set<int>::iterator it;
        for(int i=1;i<=n;i++) st.insert(i);
        while(m--){
            int ord,a,b;
            scanf("%d%d%d",&ord,&a,&b);
            if(ord==1){
                Update(a,b);
                st.insert(a);
            }
             if(ord==2)
                printf("%I64d\n",Query(b)-Query(a-1));
             if(ord==3){
                it=lower_bound(st.begin(),st.end(),a);
                del[0]=0;
                for(;it!=st.end()&&((*it)<=b);it++){
                    int p=*it;
                    del[++del[0]]=p;
                    LL u=Query(p)-Query(p-1),k;
                    int t=lower_bound(fib+1,fib+92,u)-fib;
                    if(myabs(fib[t]-u)<myabs(fib[t-1]-u)) k=fib[t];
                    else k=fib[t-1];
                    Update(p,k-u);
                }
                for(int i=1;i<=del[0];i++)//剔除已經變成斐波那契數的序號
                    st.erase(del[i]);
            }
        }
    }
    return 0;
}



相關文章