codeforces 718 C.Sasha and Array

codeforces 718 C.Sasha and Array


给你一个1~n的数列,每个数代表了是Fibonacci数列的第几项(f[0]=0),两种操作:
1:l~r区间加上一个数
2:求区间代表的Fibonacci数的和


似乎很难的样子。。。看上去肯定是线段树,有一种做法是对于每一个区间维护一个矩阵,常数会比较大,再加上lsh写得太丑只能卡过
介绍另一种写法,考虑Fibonacci 通项

f[n]=(1+52)n(152)n)5

发现两个n次方是互相独立的,则开两个线段树分别维护,而√5不好处理,所以使用复数a+√5b,并定义复数下的运算

struct node
{
    long long a,b;
    node(){}
    node(long long _a,long long _b)
    {
        a=_a%MOD;b=_b%MOD;
    }
    friend node operator +(node x,node y)
    {
        return node(x.a+y.a,x.b+y.b);
    }
    friend node operator -(node x,node y)
    {
        return node(x.a-y.a,x.b-y.b);
    }
    friend node operator *(node x,node y)
    {
        return node(x.a*y.a+5*x.b*y.b,x.a*y.b+x.b*y.a);
    }
};

将取模运算写到构造函数里,不用再其他地方再写取模
而/2的运算换为乘逆就ok了,剩下的都是裸的线段树了

#include<cstdio>
#include<cstring>

using namespace std;

const long long MOD=1e9+7;
const long long inv=5e8+4;

struct node
{
    long long a,b;
    node(){}
    node(long long _a,long long _b)
    {
        a=_a%MOD;b=_b%MOD;
    }
    friend node operator +(node x,node y)
    {
        return node(x.a+y.a,x.b+y.b);
    }
    friend node operator -(node x,node y)
    {
        return node(x.a-y.a,x.b-y.b);
    }
    friend node operator *(node x,node y)
    {
        return node(x.a*y.a+5*x.b*y.b,x.a*y.b+x.b*y.a);
    }
};

struct segment
{
    node data;
    node lazy;
    int l,r;
};

segment t1[500050],t2[500050];
int s[100050];
int n,m;

node Pow(node x,int y)
{
    node now;
    now.a=1;now.b=0;
    while (y)
    {
        if (y&1)
        {
            now=now*x;
        }
        x=x*x;
        y>>=1;
    }
    return now;
}

void update(segment t[],int p)
{
    t[p].data=t[p<<1].data+t[(p<<1)+1].data;
}

void spread(segment t[],int p)
{
    if (t[p].lazy.a==1 && t[p].lazy.b==0)
    {
        return;
    }
    t[p<<1].data=t[p<<1].data*t[p].lazy;
    t[p<<1].lazy=t[p<<1].lazy*t[p].lazy;
    t[(p<<1)+1].data=t[(p<<1)+1].data*t[p].lazy;
    t[(p<<1)+1].lazy=t[(p<<1)+1].lazy*t[p].lazy;
    t[p].lazy=node(1,0);
}

void build(segment t[],int p,int l,int r,node c)
{
    t[p].l=l;t[p].r=r;t[p].lazy=node(1,0);
    if (l==r)
    {
        t[p].data=Pow(c,s[l]);
        return;
    }
    int mid=(l+r) >> 1;
    build(t,p<<1,l,mid,c);
    build(t,(p<<1)+1,mid+1,r,c);
    update(t,p);
}

void change(segment t[],int p,int l,int r,node c)
{
    if (t[p].l==l && t[p].r==r)
    {
        t[p].lazy=t[p].lazy*c;
        t[p].data=t[p].data*c;
        return;
    }
    spread(t,p);
    int mid=(t[p].l+t[p].r) >> 1;
    if (r<=mid)
    {
        change(t,p<<1,l,r,c);
        update(t,p);
        return;
    }
    if (l>mid)
    {
        change(t,(p<<1)+1,l,r,c);
        update(t,p);
        return;
    }
    change(t,p<<1,l,mid,c);
    change(t,(p<<1)+1,mid+1,r,c);
    update(t,p);
}

node get(segment t[],int p,int l,int r)
{
    if (t[p].l==l && t[p].r==r)
    {
        return t[p].data;
    }
    spread(t,p);
    int mid=(t[p].l+t[p].r) >> 1;
    if (r<=mid)
    {
        return get(t,p<<1,l,r);
    }
    if (l>mid)
    {
        return get(t,(p<<1)+1,l,r);
    }
    return get(t,p<<1,l,mid)+get(t,(p<<1)+1,mid+1,r);
}

int main()
{
    //freopen("input.txt","r",stdin);
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&s[i]);
    }
    build(t1,1,1,n,node(inv,inv));
    build(t2,1,1,n,node(inv,-inv));
    int op,l,r,x;
    node now;
    node now1,now2;
    for (int i=1;i<=m;i++)
    {
        scanf("%d",&op);
        if (op==1)
        {
            scanf("%d%d%d",&l,&r,&x);
            now=Pow(node(inv,inv),x);
            change(t1,1,l,r,now);
            now=Pow(node(inv,-inv),x);
            change(t2,1,l,r,now);
        }
        else
        {
            scanf("%d%d",&l,&r);
            now1=get(t1,1,l,r);
            now2=get(t2,1,l,r);
            now=now1-now2;
            printf("%I64d\n",(now.b%MOD+MOD)%MOD);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值