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
    评论
Codeforces Round 894 (Div. 3) 是一个Codeforces举办的比赛,是第894轮的Div. 3级别比赛。它包含了一系列题目,其中包括题目E. Kolya and Movie Theatre。 根据题目描述,E. Kolya and Movie Theatre问题要求我们给定两个字符串,通过三种操作来让字符串a等于字符串b。这三种操作分别为:交换a中相同位置的字符、交换a中对称位置的字符、交换b中对称位置的字符。我们需要先进行一次预处理,替换a中的字符,然后进行上述三种操作,最终得到a等于b的结果。我们需要计算预处理操作的次数。 根据引用的讨论,当且仅当b[i]==b[n-i-1]时,如果a[i]!=a[n-i-1],需要进行一次操作;否则不需要操作。所以我们可以遍历字符串b的前半部分,判断对应位置的字符是否与后半部分对称,并统计需要进行操作的次数。 以上就是Codeforces Round 894 (Div. 3)的简要说明和题目E. Kolya and Movie Theatre的要求。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *2* [Codeforces Round #498 (Div. 3) (A+B+C+D+E+F)](https://blog.csdn.net/qq_46030630/article/details/108804114)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *3* [Codeforces Round 894 (Div. 3)A~E题解](https://blog.csdn.net/gyeolhada/article/details/132491891)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值