718C Sasha and Array

本文介绍了一种利用矩阵快速幂优化斐波那契数列查询的方法,并通过线段树实现区间更新和求和操作,有效解决了区间修改与查询的问题。

传送门

题目

Sasha has an array of integers a1, a2, ..., an. You have to perform m queries. There might be queries of two types:

  1. 1 l r x — increase all integers on the segment from l to r by values x;
  2. 2 l r — find , where f(x) is the x-th Fibonacci number. As this number may be large, you only have to find it modulo 109 + 7.

In this problem we define Fibonacci numbers as follows: f(1) = 1, f(2) = 1, f(x) = f(x - 1) + f(x - 2) for all x > 2.

Sasha is a very talented boy and he managed to perform all queries in five seconds. Will you be able to write the program that performs as well as Sasha?

Input

The first line of the input contains two integers n and m (1 ≤ n ≤ 100 000, 1 ≤ m ≤ 100 000) — the number of elements in the array and the number of queries respectively.

The next line contains n integers a1, a2, ..., an (1 ≤ ai ≤ 109).

Then follow m lines with queries descriptions. Each of them contains integers tpi, li, ri and may be xi (1 ≤ tpi ≤ 2, 1 ≤ li ≤ ri ≤ n, 1 ≤ xi ≤ 109). Here tpi = 1 corresponds to the queries of the first type and tpi corresponds to the queries of the second type.

It's guaranteed that the input will contains at least one query of the second type.

Output

For each query of the second type print the answer modulo 109 + 7.

题目大意

给你一个长度为n的数列an,有两种操作

1、将L到R的ai加上X

2、询问L到R之间,f(aL)+f(aL+1)+……+f(aR)的和

f是斐波那契函数

分析

我们可以将斐波那契数转化成它所对应的矩阵,对于每一次加x就是给原来矩阵乘上斐波那契矩阵的x次方。将为赋值的矩阵全部初始化为单位矩阵,然后进行朴素的线段树为何两节点之和即可。

代码

#pragma G++ optimize (2)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<cctype>
#include<cmath>
#include<cstdlib>
#include<queue>
#include<ctime>
#include<vector>
#include<set>
#include<map>
#include<stack>
using namespace std;
#define rri register int
const int mod=1e9+7;
struct mat {
      int g[5][5];
};
mat d[440000],one,per;
mat add[440000];
inline mat operator * (const mat a,const mat b){
      mat c;
      c.g[1][1]=c.g[1][2]=c.g[2][1]=c.g[2][2]=0;
      for(rri i=1;i<=2;++i)
         for(rri k=1;k<=2;++k)
            for(rri j=1;j<=2;++j)            
               c.g[i][j]=(c.g[i][j]+(long long)a.g[i][k]*b.g[k][j]%mod)%mod;
      return c;
}
inline mat operator + (const mat a,const mat b){
      mat c;
      for(rri i=1;i<=2;++i)
         for(rri j=1;j<=2;++j)
            c.g[i][j]=(a.g[i][j]+b.g[i][j])%mod;
      return c;
}
inline mat pw(mat a,int p){
      mat res=a;
      p-=1;
      while(p){
          if(p&1)res=res*a;
          a=a*a;
          p>>=1;
      }
      return res;
}
inline int read(){
      int x=0,f=1;char s=getchar();
      while(s<'0'||s>'9'){if(s=='-')f=-1;s=getchar();}
      while(s>='0'&&s<='9'){x=(x<<3)+(x<<1)+(s-'0');s=getchar();}
      return f*x;
}
inline void build(int le,int ri,int pl,mat k,int wh){
      add[wh]=per;
      if(le==ri){
        d[wh]=k;
        return;
      }
      int mid=(le+ri)>>1;
      if(mid>=pl)build(le,mid,pl,k,wh<<1);
        else build(mid+1,ri,pl,k,wh<<1|1);
      d[wh]=d[wh<<1]+d[wh<<1|1];
}
inline void pd(int wh){
      if(add[wh].g[1][1]!=1||add[wh].g[2][2]!=1||
         add[wh].g[1][2]!=0||add[wh].g[2][1]!=0){
        add[wh<<1]=add[wh<<1]*add[wh];
        add[wh<<1|1]=add[wh<<1|1]*add[wh];
        d[wh<<1]=d[wh<<1]*add[wh];
        d[wh<<1|1]=d[wh<<1|1]*add[wh];
        add[wh]=per;
      }
}
inline void update(int le,int ri,int x,int y,mat k,int wh){
      if(le>=x&&ri<=y){
          add[wh]=add[wh]*k;
          d[wh]=d[wh]*k;
          return;
      }
      int mid=(le+ri)>>1;
      pd(wh);
      if(mid>=x)update(le,mid,x,y,k,wh<<1);
      if(mid<y)update(mid+1,ri,x,y,k,wh<<1|1);
      d[wh]=d[wh<<1]+d[wh<<1|1];
}
inline int q(int le,int ri,int x,int y,int wh){
      if(le>=x&&ri<=y)return d[wh].g[1][2]%mod;
      int mid=(le+ri)>>1,ans=0;
      pd(wh);
      if(mid>=x)ans=(ans+q(le,mid,x,y,wh<<1))%mod;
      if(mid<y)ans=(ans+q(mid+1,ri,x,y,wh<<1|1))%mod;
      d[wh]=d[wh<<1]+d[wh<<1|1];
      return ans%mod;
}
int main()
{     int n,m,x,l,r,k;
      one.g[1][1]=0,one.g[1][2]=one.g[2][1]=one.g[2][2]=1;
      per.g[1][1]=per.g[2][2]=1,per.g[1][2]=per.g[2][1]=0;
      n=read(),m=read();
      for(rri i=1;i<=n;++i){
          x=read();
          build(1,n,i,pw(one,x),1);
      }
      for(rri i=1;i<=m;++i){
          k=read();
          if(k==1){
              l=read(),r=read(),x=read();
              update(1,n,l,r,pw(one,x),1);
          }else {
              l=read(),r=read();
              printf("%d\n",q(1,n,l,r,1)%mod);
          }
      }
      return 0;
}

转载于:https://www.cnblogs.com/yzxverygood/p/9195980.html

### 强化学习中Sasha算法的原理介绍 强化学习(Reinforcement Learning, RL)是一种通过与环境交互来学习策略的方法,其目标是使智能体在环境中采取一系列动作以最大化累积奖励。Sasha Rush组的研究工作主要集中在自然语言处理(NLP)领域中的序列生成任务上,虽然并非直接属于强化学习范畴,但其提出的编码器-解码器架构为后续强化学习中的序列生成任务提供了重要参考[^2]。 #### 1. 编码器-解码器框架 Sasha Rush组的工作(Rush et al., 2015)提出了一种基于注意力机制的编码器-解码器模型,用于文本简化、标题生成等任务。该模型的核心思想是将输入序列编码为一个固定长度的向量表示,然后通过解码器将其转换为目标序列。这种架构设计与强化学习中的策略网络(Policy Network)有相似之处,因为它们都涉及从一个状态空间到动作空间的映射[^2]。 ```python import torch import torch.nn as nn class EncoderDecoder(nn.Module): def __init__(self, input_dim, hidden_dim, output_dim): super(EncoderDecoder, self).__init__() self.encoder = nn.LSTM(input_dim, hidden_dim) self.decoder = nn.LSTM(hidden_dim, output_dim) def forward(self, input_seq): _, (hidden, _) = self.encoder(input_seq) output, _ = self.decoder(hidden) return output ``` #### 2. 强化学习中的应用 在强化学习中,Sasha算法的原理可以被扩展为以下几点: - **状态表示**:类似于编码器的功能,强化学习中的状态可以通过神经网络进行编码,以便捕捉环境的复杂特征。 - **动作选择**:解码器的作用可以类比为强化学习中的策略网络,负责根据当前状态选择最优动作。 - **奖励信号**:强化学习通过奖励信号指导智能体学习,而Sasha算法中的监督信号则由目标序列提供。两者在训练目标上有一定的相似性[^1]。 #### 3. 注意力机制的作用 Sasha算法引入的注意力机制允许模型在生成每个输出词时聚焦于输入序列的不同部分。这种机制在强化学习中也有广泛应用,例如在多智能体协作或复杂环境建模中,注意力机制可以帮助智能体关注关键信息[^3]。 #### 4. 训练目标的差异 尽管Sasha算法主要依赖监督学习,但其训练目标可以启发强化学习中的奖励设计。例如,强化学习中的下一个令牌预测(NTP)和掩码语言建模(MLM)目标可以被视为对Sasha算法训练目标的扩展[^4]。 ```python # 掩码语言建模示例 def masked_language_modeling(sequence, mask_token_id): masked_sequence = sequence.clone() mask_indices = torch.rand(len(sequence)) < 0.15 masked_sequence[mask_indices] = mask_token_id return masked_sequence ``` ### 结论 Sasha算法的核心思想在于利用编码器-解码器框架结合注意力机制完成序列生成任务。这一思想为强化学习中的策略学习提供了重要借鉴,尤其是在状态表示、动作选择和奖励设计等方面。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值