2022 Nioke Winter Holiday Algorithm Basic Training Camp 2, Xiaosha's Counting (Blocks, Inverse Elements)

Portal
insert image description here
idea:

First of all, we must have a deep understanding of the meaning of the question. Since the operation has priority, and we found that each +interval will be distinguished from each other, the values ​​of each interval do not interfere with each other , so we can integrate the information of each interval into one in advance. In the value, and then calculate, that is , the idea of ​​​​blocking (intra -block product, addition and subtraction between blocks), first preprocess the first block: the first number is in the first block, the value of the first block is a[1], and the last modification During the process, locate the position pos of the block to be modified, then change the value in the block, and then recalculate the new value.

Code:

#include<iostream>
#define int long long

using namespace std;
const int N=1000010,mod=1e9+7;
int a[N],b[N];
int n,q;
int ans[N];
char c[N];

int qmi(int a,int b){
    
    
    int res=1;
    while(b){
    
    
        if(b&1)res=res*a%mod;
        a=a*a%mod;
        b>>=1;
    }
    return res;
}

int inv(int a){
    
    
    return qmi(a,mod-2);
}

signed  main(){
    
    
    cin>>n>>q;
    cin>>(c+1);
    for(int i=1;i<=n;i++)
        cin>>a[i],ans[i]=1;
        
    //预处理第一块:第1个数字在第1块里,第一块的值是a[1]
    b[1]=1;
    ans[1]=a[1];
    
    int now=1;
    for(int i=1;i<=n-1;i++){
    
    
        //遇到'+',进入下一块
        if(c[i]=='+')now++;
        //第now块的值是ans[now]
        ans[now]=ans[now]*a[i+1]%mod;
        //第(i+1)个数字在第now块里
        b[i+1]=now;
    }
    int res=0;
    for(int i=1;i<=now;i++)
        res=(res+ans[i])%mod;
        
    while(q--){
    
    
        int x,y;
        cin>>x>>y;
        
        //修改的是第pos块的数据,要把old修改为y
        int pos=b[x],old=a[x];
        int en=ans[pos];
        ans[pos]=ans[pos]*inv(old)%mod*y%mod;
        int change=((ans[pos]-en)%mod+mod)%mod;
        res=(res+change)%mod;
        
        //别忘记
        a[x]=y;
        
        cout<<res<<endl;
    }
}

Guess you like

Origin blog.csdn.net/qq_45550375/article/details/122738777