Naive Operations

Naive Operations

Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 502768/502768 K (Java/Others)
Total Submission(s): 1853    Accepted Submission(s): 802


 

Problem Description

In a galaxy far, far away, there are two integer sequence a and b of length n.
b is a static permutation of 1 to n. Initially a is filled with zeroes.
There are two kind of operations:
1. add l r: add one for al,al+1...ar
2. query l r: query ∑ri=lai/bi

 

 

Input

There are multiple test cases, please read till the end of input file.
For each test case, in the first line, two integers n,q, representing the length of a,b and the number of queries.
In the second line, n integers separated by spaces, representing permutation b.
In the following q lines, each line is either in the form 'add l r' or 'query l r', representing an operation.
1≤n,q≤100000, 1≤lrn, there're no more than 5 test cases.

 

 

Output

Output the answer for each 'query', each one line.

 

 

Sample Input

 

5 12 1 5 2 4 3 add 1 4 query 1 4 add 2 5 query 2 5 add 3 5 query 1 5 add 2 4 query 1 4 add 2 5 query 2 5 add 2 2 query 1 5

 

 

Sample Output

 

1 1 2 4 4 6

 题意:给一个长度n值为0的区间数组a,和长度为n的一个排列b,有q次操作,add(l,r)表示a的l,r区间内的数+1。query(l,r)表示查询一个区间内ai/bi的值。

分析:由于起初a都是0,所以当ai加到与bi相等答案才会+1。可以反过来想,把bi放入对应节点,维护区间最小值,当最小值减到0时就再用线段维护一个答案,使得答案+1,并且重新给该节点赋值bi。

#include<stdio.h>
#include<iostream>
#include<string.h>
#include<vector>
#include<stdlib.h>
#include<math.h>
#include<queue>
#include<deque>
#include<ctype.h>
#include<map>
#include<set>
#include<stack>
#include<string>
#include<algorithm>
#define INF 0x3f3f3f3f
#define FAST_IO ios::sync_with_stdio(false)
const double PI = acos(-1.0);
const double eps = 1e-6;
const int MAX=1e5+10;
const int mod=1e9+7;
typedef long long ll;
using namespace std;
#define gcd(a,b) __gcd(a,b)
inline ll lcm(ll a,ll b){return a/gcd(a,b)*b;}
inline ll qpow(ll a,ll b){ll r=1,t=a; while(b){if(b&1)r=(r*t)%mod;b>>=1;t=(t*t)%mod;}return r;}
inline ll inv1(ll b){return qpow(b,mod-2);}
inline ll exgcd(ll a,ll b,ll &x,ll &y){if(!b){x=1;y=0;return a;}ll r=exgcd(b,a%b,y,x);y-=(a/b)*x;return r;}
inline ll read(){ll x=0,f=1;char c=getchar();for(;!isdigit(c);c=getchar()) if(c=='-') f=-1;for(;isdigit(c);c=getchar()) x=x*10+c-'0';return x*f;}
//freopen( "in.txt" , "r" , stdin );
//freopen( "data.txt" , "w" , stdout );
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
int Minx[MAX<<2];//区间最小值
int ans[MAX<<2];//记录最终答案
int lazy[MAX<<2];//懒惰标记
int b[MAX<<2];//记录b数组
int n,q;
void pushup(int rt)
{
    Minx[rt]=min(Minx[rt<<1],Minx[rt<<1|1]);//最小值
    ans[rt]=ans[rt<<1]+ans[rt<<1|1];//答案求区间和
}
void pushdown(int rt)
{
    if(lazy[rt])
    {
        lazy[rt<<1]+=lazy[rt];
        lazy[rt<<1|1]+=lazy[rt];
        Minx[rt<<1|1]-=lazy[rt];//最小值一直减1
        Minx[rt<<1]-=lazy[rt];
        lazy[rt]=0;
    }
}
void build(int l,int r,int rt)
{
    lazy[rt]=0;
    ans[rt]=0;
    if(l==r)
    {
        scanf("%d",&b[rt]);
        Minx[rt]=b[rt];
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    pushup(rt);
}
void updata(int L,int R,int l,int r,int rt)
{
    if(L<=l && r<=R)
    {
        Minx[rt]--;
        if(Minx[rt])
        {
            lazy[rt]++;
            return ;
        }
        else//区间最小值减小到0了
        {
            if(l==r)
            {
                ans[rt]++;
                Minx[rt]=b[rt];//重新赋值区间最小值
                return ;
            }
        }
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m) updata(L,R,lson);
    if(m<R) updata(L,R,rson);
    pushup(rt);
}
int query(int L,int R,int l,int r,int rt)
{
    if(L<=l && R>=r)
        return ans[rt];
    pushdown(rt);
    int m=(l+r)>>1;
    int sum=0;
    if(m>=L) sum+=query(L,R,lson);
    if(m<R) sum+=query(L,R,rson);
    return sum;
}
int main()
{
    int l,r;
    while(scanf("%d%d",&n,&q)!=EOF)
    {
        build(1,n,1);
        for(int i=1;i<=q;i++)
        {
            char op[10];
            scanf("%s%d%d",op,&l,&r);
            if(op[0]=='a')
                updata(l,r,1,n,1);
            else
                printf("%d\n",query(l,r,1,n,1));
        }
    }
    return 0;
}

 

### Naive RAG 的基本概念 Naive RAG(Retrieval-Augmented Generation)是一种简单形式的信息检索增强生成模型,其核心理念是在传统文本生成的基础上引入外部知识库的支持。通过结合检索模块和生成模块的功能,RAG 能够显著提升自然语言处理任务中的上下文理解能力以及生成内容的质量。 在 Naive RAG 中,通常会采用一种较为基础的方式来进行文档检索与生成器的融合。具体而言,在面对输入查询时,系统首先利用简单的检索算法从大规模的知识库中提取若干候选片段作为背景信息[^1]。这些片段随后被传递至预训练的语言模型以辅助完成最终的目标序列生成过程。 #### 实现方法概述 以下是构建一个朴素版 RAG 所需的主要组件及其工作流程: 1. **检索部分**: 使用 TF-IDF 或 BM25 等经典搜索引擎技术来定位最相关的几篇文档摘要或者段落。 ```python from sklearn.feature_extraction.text import TfidfVectorizer vectorizer = TfidfVectorizer() X = vectorizer.fit_transform(corpus) query_vector = vectorizer.transform([query]) scores = cosine_similarity(query_vector, X).flatten()[:top_k] ``` 2. **生成阶段**: 将上述获得的相关材料连同原始提问一起送入到像 GPT-NeoX 这样的大型神经网络架构里执行解码操作从而得到答案表述。 这种设计使得即使是没有经过特别优化调整过的简易版本也能展现出超越单纯依赖内部记忆存储的传统 seq2seq 架构的表现水平因为额外获取到了实时更新的真实世界资料支持. ```python def naive_rag_generate(query, corpus, model, tokenizer): # Step 1: Retrieve relevant documents using a basic search algorithm. retrieved_docs = retrieve_documents(query, corpus) # Combine the input with context provided by retrieved docs. full_context = f"{query} [SEP] {' '.join(retrieved_docs)}" # Tokenize and generate output based on combined input & contexts. inputs = tokenizer(full_context, return_tensors="pt", truncation=True) outputs = model.generate(**inputs, max_length=100) return tokenizer.decode(outputs[0], skip_special_tokens=True) # Example usage of function defined above... generated_text = naive_rag_generate(user_query, document_corpus, pretrained_model, tokenization_tool) print(generated_text) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值