牛客 储物点的距离(前缀和)

写出式子后前缀和乱搞

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
typedef long long ll;
const int N=3e5+10;
const int mod=1000000007;
ll a[N],b[N];
ll s[N];
ll pos[N];
ll c[N];
int cal1(int x,int l,int r){
    ll res=0;
    ll tmp=s[r]-s[l-1];
    tmp=tmp%mod*pos[x]%mod;
    return (c[r]-c[l-1]-tmp+mod+mod)%mod;
}
int cal2(int x,int l,int r){
    ll res=0;
    ll tmp=s[r]-s[l-1];
    tmp=tmp%mod*pos[x]%mod;
    return (tmp-(c[r]-c[l-1])+2*mod)%mod;
}
int main(){
    int n,m;
    cin>>n>>m;
    int i;
    for(i=2;i<=n;i++){
        scanf("%lld",&a[i]);
        pos[i]=(pos[i-1]+a[i])%mod;
    }
    for(i=1;i<=n;i++){
        scanf("%lld",&b[i]);
        s[i]=(s[i-1]+b[i])%mod;
        c[i]=(c[i-1]+b[i]*pos[i])%mod;

    }
    while(m--){
        int l,r,x;
        scanf("%d%d%d",&x,&l,&r);
        if(x<=l){
            int ans=0;
            ans=cal1(x,l,r);
            printf("%d\n",ans%mod);
        }
        else if(x>=r){
            int ans=0;
            ans=cal2(x,l,r);
            printf("%d\n",ans%mod);
        }
        else{
            int ans=0;
            ans=cal1(x,x,r)+cal2(x,l,x);
            printf("%d\n",ans%mod);

        }
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/ctyakwf/p/12907428.html