储物点的距离

储物点的距离

参考:【每日一题】5月15日题目精讲

该题考的算法不难,但是思维要灵活。而且要注意取模。

技巧在于,要找到一些特定的状态。

// Created by CAD on 2020/5/15.
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int maxn=2e5+5;
const int mod=1000000007;
ll a[maxn],b[maxn],c[maxn];

ll solve(int l,int r,int x){
    ll ans=0;
    if(x>r)
        ans=(((b[r]-b[l-1])%mod*a[x]%mod-(c[r]-c[l-1])%mod)%mod+mod)%mod;
    else
        ans=(((c[r]-c[l-1])%mod-(b[r]-b[l-1])*a[x]%mod)%mod+mod)%mod;
    return ans;
}

int main() {
    ios::sync_with_stdio(false);
    cin.tie(0);
    int n,q;cin>>n>>q;
    for(int i=2;i<=n;++i)
        cin>>a[i],a[i]+=a[i-1],a[i]%=mod;
    for(int i=1;i<=n;++i)
        cin>>b[i],c[i]=(c[i-1]+a[i]*b[i])%mod,
        b[i]=(b[i]+b[i-1])%mod;
    while(q--){
        int l,r,x;cin>>x>>l>>r;
        if(x<l||x>r) cout<<solve(l,r,x)<<"\n";
        else cout<<(solve(l,x-1,x)+solve(x+1,r,x)+mod)%mod<<"\n";
    }
    return 0;
}
posted @ 2020-05-15 21:01  caoanda  阅读(141)  评论(0编辑  收藏  举报