【BZOJ4597】【Shoi2016】随机序列 线段树

本文介绍了一道极为简单的期望题目,并提供了一个利用线段树进行区间更新和查询的C++解决方案。该方法通过单点修改和区间乘法实现了高效计算。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

应该是我有史以来见过的最简单的期望题。。。。。。暂时没有之一

手推两下就可以发现长度为n的序列的答案是其中,那么我们拿一个带单点修改区间乘法区间求和的线段树来维护就好了。


/**************************************************************
    Problem: 4597
    User: RicardoWang
    Language: C++
    Result: Accepted
    Time:660 ms
    Memory:7916 kb
****************************************************************/
 
#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<queue>
#include<vector>
using namespace std;
#define mo 1000000007
#define MAXN 100005
int N,Q,A[MAXN],x,y;
long long now,ans,T[MAXN],A_[MAXN];
void _read(int &x)
{
    x=0; char ch=getchar(); bool flag=false;
    while(ch<'0' || ch>'9'){if(ch=='-')flag=true; ch=getchar();}
    while(ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} if(flag)x=-x; return ;
}
void _readLL(long long &x)
{
    x=0; char ch=getchar(); bool flag=false;
    while(ch<'0' || ch>'9'){if(ch=='-')flag=true; ch=getchar();}
    while(ch>='0' && ch<='9') {x=x*10+ch-'0'; ch=getchar();} if(flag)x=-x; return ;
}
void Init()
{
    _read(N); _read(Q);
    for(int i=1;i<=N;i++)_read(A[i]);
    return ;
}
int np,rt,chi[2*MAXN][2];
long long w[2*MAXN],down[2*MAXN];
void build(int &now,int L,int R)
{
    now=++np; w[now]=0; down[now]=1;
    if(L==R)
    {
        w[now]=A_[L]; return ;
    }
    int m=(L+R)>>1;
    build(chi[now][0],L,m); build(chi[now][1],m+1,R);
    w[now]=(w[chi[now][0]]+w[chi[now][1]])%mo;
    return ;
}
void pushdown(int now)
{
    if(down[now]!=1)
    {
        w[chi[now][0]]=(w[chi[now][0]]*down[now])%mo;
        w[chi[now][1]]=(w[chi[now][1]]*down[now])%mo;
        down[chi[now][0]]=(down[chi[now][0]]*down[now])%mo;
        down[chi[now][1]]=(down[chi[now][1]]*down[now])%mo;
        down[now]=1;
    }
    return ;
}
void update(int now,int L,int R,int x,int y,long long v)
{
    if(x<=L && R<=y)
    {
        w[now]=(w[now]*v)%mo;
        down[now]=(down[now]*v)%mo;
        return ;
    }
    pushdown(now);
    int m=(L+R)>>1;
    if(x<=m)update(chi[now][0],L,m,x,y,v);
    if(y>m)update(chi[now][1],m+1,R,x,y,v);
    w[now]=(w[chi[now][0]]+w[chi[now][1]])%mo;
    return ;
}
long long qkpower(long long a,long long x)
{
    long long now=a,ans=1;
    while(x)
    {
        if(x&1)ans=(ans*now)%mo;
        x=x/2; now=(now*now)%mo;
    }
    return ans;
}
char s[35];int cct;
void out(long long x)
{
    if(x<0){putchar('-'); x=-x;}
    if(!x)putchar('0');
    cct=0; while(x){s[++cct]=x%10+'0';x=x/10; }
    while(cct){putchar(s[cct]);cct--;}
    putchar('\n');
    return ;
}
void work0()
{
    T[N]=1; T[N-1]=2;
    for(int i=N-2;i>=1;i--)T[i]=T[i+1]*3%mo;
    now=1;
    for(int i=1;i<=N;i++)
    {
        now=now*A[i]%mo;
        A_[i]=now*T[i]%mo;
    }
    build(rt,1,N);
    for(int i=1;i<=Q;i++)
    {
        _read(x); _read(y);
        now=0;
        now=qkpower(A[x],mo-2); now=now*y%mo;
        update(rt,1,N,x,N,now);
        A[x]=y;
        out(w[1]);
    }
    return ;
}
int main()
{
//  freopen("in.txt","r",stdin);
    Init();
    work0();
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值