HDU 3074 (线段树+模P乘法)

题目链接http://acm.hdu.edu.cn/showproblem.php?pid=3074

题目大意:单点更新。维护序列乘法。mod 1000000007。

解题思路

1000000007*1000000007~10^18<9*10^18(int64)

所以单步模P乘法可以直接计算。

(a*b)%p=[(a%p)*(b%p)]%p,PushUp维护即可。

Query的rson的时候,要先判下lson是否存在,不存在ret=Query(rson),否则ret=(Query(lson)*Query(rson))%p

#include "iostream"
#include "string"
#include "vector"
#include "cstring"
#include "fstream"
#include "cstdio"
using namespace std;
#define M 100005
#define lson l,mid,root<<1
#define rson mid+1,r,root<<1|1
#define LL long long
#define mod 1000000007
LL ans[M<<2];
void PushUp(int root)
{
    LL a=ans[root<<1]%mod,b=ans[root<<1|1]%mod;
    ans[root]=(a*b)%mod;
}
void build(int l,int r,int root)
{
    if(l==r)
    {
        scanf("%I64d",&ans[root]);
        return;
    }
    int mid=(l+r)>>1;
    build(lson);
    build(rson);
    PushUp(root);
}
void update(int p,int value,int l,int r,int root)
{
    if(l==r)
    {
        ans[root]=value;
        return;
    }
    int mid=(l+r)>>1;
    if(p<=mid) update(p,value,lson);
    else update(p,value,rson);
    PushUp(root);
}
LL Query(int L,int R,int l,int r,int root)
{
    if(L<=l&&r<=R) return ans[root]%mod;
    int mid=(l+r)>>1;
    LL ret=-1;
    if(L<=mid) ret=Query(L,R,lson);
    if(R>mid)
    {
        LL rr=Query(L,R,rson);
        if(ret!=-1)
        {
            LL a=ret%mod,b=rr%mod;
            ret=(a*b)%mod;
        }
        else ret=rr;
    }
    return ret;
}
int main()
{
    //freopen("in.txt","r",stdin);
    int n,q,ll,rr,p,k,v,cmd;
    int T;scanf("%d",&T);
    while(T--)
    {
        scanf("%d",&n);
        build(1,n,1);
        scanf("%d",&q);
        while(q--)
        {
            scanf("%d",&cmd);
            if(cmd==1)
            {
                scanf("%d%d",&k,&v);
                update(k,v,1,n,1);
            }
            if(cmd==0)
            {
                scanf("%d%d",&ll,&rr);
                LL ans=Query(ll,rr,1,n,1);
                printf("%I64d
",ans%mod);
            }
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/neopenx/p/4501058.html