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

来源:互联网 发布:淘宝哪家情侣装好看 编辑:程序博客网 时间:2024/05/21 22:38

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

手推两下就可以发现长度为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 100005int 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;}


0 0
原创粉丝点击