HDU4893 Wow! Such Sequence!

Wow! Such Sequence!


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


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


Source
2014 Multi-University Training Contest 3

题意:一段初始为0的数,有三种操作,1代表给k位置加上d,2代表l-r区间内数的和,3代表将每个数变为最接近这个数的斐波拉契数(相同选小的)。

比赛的时候一上来就看了这题。。。。然后花了2个多小时才A掉。。。3次CE。。无语。。。用线段树做的,首先写个二分查找斐波拉契数,数字肯定小于2的64次方的(两个2的63次方的数相加),然后斐波拉契数只要到93就越界了,所以打表只要打到92(用unsigned __int64存的),然后如果只是单纯的把这题看成一个点更新和区间求和的题会超时(比赛T了3次),所以后面对每个区间加了个标记,每次将这段区间里的数变为斐波拉契数时就将这个区间的标记变为1,单点更新过要将这个点的标记变为0,然后pushup传递上去,区间更新的时候如果这个区间标记已经为1了,就不要再往下走了,直接返回,因为下面的数已经更新成斐波拉契数了,而且下面的数也没有单点更新过。

比赛时考虑数会不会超int太麻烦。。。所以就全部__int64了。。。。

#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll __int64
using namespace std;
const int MAXN=100010;
unsigned __int64 f[100];
struct tree
{
    ll l,r;
    ll mark;    //标记
    ll sum;
}tree[MAXN<<2];
ll n,m;
ll num[MAXN];
ll find(ll x)
{
    ll l=1,r=92,mid;
    while(l<=r)
    {
        mid=(l+r)>>1;
        if(x>f[mid])
            l=mid+1;
        else
            r=mid-1;
    }
    return l;
}
ll aabs(ll x,ll y)
{
    if(x>y)
        return x-y;
    else
        return y-x;
}
ll get_f(ll x)
{
    ll temp=x;
    ll res;
    if(temp<=0)
        res=1;
    else
    {
        res=find(temp);
        ll tmp1=aabs(f[res],temp);
        if(res>0)
        {
            ll tmp2=aabs(f[res-1],temp);
            res=tmp1<tmp2?res:res-1;
        }
    }
    return f[res];
}
void build(ll l,ll r,ll k)
{
    tree[k].l=l;
    tree[k].r=r;
    tree[k].sum=0;
    tree[k].mark=0;
    if(l==r)
        return;
    ll mid=(l+r)>>1;
    build(l,mid,k<<1);
    build(mid+1,r,k<<1|1);
}
void pushup(ll k)
{
    tree[k].sum=tree[k<<1].sum+tree[k<<1|1].sum;
    tree[k].mark=tree[k<<1].mark&tree[k<<1|1].mark; //传递标记
}
void update1(ll n,ll d,ll k)    //单点更新
{
    if(tree[k].l==tree[k].r)
    {
        tree[k].mark=0;
        tree[k].sum+=d;
        num[tree[k].l]+=d;
        return ;
    }
    ll mid=(tree[k].l+tree[k].r)>>1;
    if(n<=mid)
        update1(n,d,k<<1);
    else
        update1(n,d,k<<1|1);
    pushup(k);
}
void update2(ll l,ll r,ll k)    //区间更新,将这个区间内的数变为斐波拉契数
{
    if(tree[k].mark==1)
        return ;
    if(tree[k].l==tree[k].r)
    {
        ll tmp=get_f(num[tree[k].l]);
        tree[k].sum=tmp;
        num[tree[k].l]=tmp;
        tree[k].mark=1;
        return ;
    }
    ll mid=(tree[k].l+tree[k].r)>>1;
    if(l<=mid)
        update2(l,r,k<<1);
    if(r>mid)
        update2(l,r,k<<1|1);
    pushup(k);
}
ll query(ll l,ll r,ll k)    //求和
{
    if(l<=tree[k].l&&r>=tree[k].r)
    {
        return tree[k].sum;
    }
    ll mid=(tree[k].l+tree[k].r)>>1;
    ll ret=0;
    if(l<=mid)
        ret+=query(l,r,k<<1);
    if(r>mid)
        ret+=query(l,r,k<<1|1);
    return ret;
}
int main()
{

    ll i;
    f[1]=1,f[2]=1;
    for(i=3;i<=92;i++)
    {
        f[i]=f[i-1]+f[i-2];
    }
    while(scanf("%I64d%I64d",&n,&m)!=EOF)
    {
        ll op;
        build(1,n,1);
        memset(num,0,sizeof(num));
        while(m--)
        {
            scanf("%I64d",&op);
            if(op==1)
            {
                ll x,val;
                scanf("%I64d%I64d",&x,&val);
                update1(x,val,1);
            }
            else if(op==2)
            {
                ll x,y,temp;
                scanf("%I64d%I64d",&x,&y);
                if(x>y)
                temp=x,x=y,y=temp;
                printf("%I64d\n",query(x,y,1));
            }
            else if(op==3)
            {
                ll x,y;
                scanf("%I64d%I64d",&x,&y);
                update2(x,y,1);
            }
        }
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值