ACM 线段树模板 hdu 4893 Wow! Such Sequence!

来源:互联网 发布:硕鼠mac 下载打不开 编辑:程序博客网 时间:2024/06/05 21:18

hdu 4893 Wow! Such Sequence!

ACM 线段树模板

//Accepted3296MS6600K#include <iostream>#include <cstdio>#include <cstring>#include <cmath>using namespace std;long long fi[1000],f[400010],g[400010];int n,m,ch,x,y;void add(int x,int l,int r,int k,int d){    g[x] = false;    if (l==r){        f[x] += d;        return ;    }    int mid = (l+r)/2;    if (k<=mid) add(x+x,l,mid,k,d);     else add(x+x+1,mid+1,r,k,d);    f[x] = f[x+x]+f[x+x+1];}long long query(int x,int l,int r,int a,int b){    if (l==a&&r==b){        return f[x];    }    int mid = (l+r)/2;    if (b<=mid) return query(x+x,l,mid,a,b);else    if (a>mid) return query(x+x+1,mid+1,r,a,b); else    return query(x+x,l,mid,a,mid)+query(x+x+1,mid+1,r,mid+1,b);}long long find(long long x){    long long ans=1;     for (int i = 1; i <=78; i ++){        if (abs(fi[i]-x)<abs(ans-x))          ans = fi[i];    }     return  ans;}void Fi(int x,int l,int r,int a,int b){    if (g[x]) return;    if (l==r){        f[x] = find(f[x]);        g[x] = true;        return;    }    int mid = (l+r)/2;    if (b<=mid) Fi(x+x,l,mid,a,b);else    if (a>mid) Fi(x+x+1,mid+1,r,a,b); else{       Fi(x+x,l,mid,a,mid);       Fi(x+x+1,mid+1,r,mid+1,b);    }    g[x] = g[x+x]&g[x+x+1];    f[x] = f[x+x]+f[x+x+1];}int main(){    fi[0] = 1;    fi[1] = 1;    for (int i =2 ; i <=100;i ++){        fi[i] = fi[i-1]+fi[i-2];        if (fi[i]>(long long)(10000000000000000ll))  break;    }        while (~scanf("%d%d",&n,&m)){        memset(g,0,sizeof(g));        memset(f,0,sizeof(f));        for (int i = 1; i <= m; i ++){            scanf("%d%d%d",&ch,&x,&y);            if (ch==1) add(1,1,n,x,y);            if (ch==2) cout<<query(1,1,n,x,y)<<endl;            if (ch==3) Fi(1,1,n,x,y);        }    }}


点击打开题目hdu 4893


Wow! Such Sequence!

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


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
 

Author
Fudan University
 

Source
2014 Multi-University Training Contest 3


0 0
原创粉丝点击