BZOJ4597 SHOI2016随机序列(线段树)

时间:2023-12-28 19:06:38

  先考虑题目所说的太简单了的问题。注意到只要把加减号相取反,就可以得到一对除了第一项都互相抵消的式子。于是得到答案即为Σf(i)g(i),其中f(i)为前缀积,g(i)为第i个数前面所有符号均填乘号,第i个数后面符号不填乘号,剩余任意填的方案数,也即g(i)=2*3n-i-1(i<n),g(n)=1。

  现在考虑修改产生的影响。显然会造成一段后缀的前缀积的改变。给他们区间乘一下维护区间和就好了。

#include<iostream>
#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 100010
#define P 1000000007
int read()
{
int x=,f=;char c=getchar();
while (c<''||c>'') {if (c=='-') f=-;c=getchar();}
while (c>=''&&c<='') x=(x<<)+(x<<)+(c^),c=getchar();
return x*f;
}
char getc(){char c=getchar();while ((c<'A'||c>'Z')&&(c<'a'||c>'z')) c=getchar();return c;}
int gcd(int n,int m){return m==?n:gcd(m,n%m);}
int n,m,a[N],f[N];
int L[N<<],R[N<<],lazy[N<<],sum[N<<];
int inv(int a)
{
int s=,k=P-;
for (;k;k>>=,a=1ll*a*a%P) if (k&) s=1ll*s*a%P;
return s;
}
void update(int k,int x){sum[k]=1ll*sum[k]*x%P;lazy[k]=1ll*lazy[k]*x%P;}
void down(int k){update(k<<,lazy[k]),update(k<<|,lazy[k]),lazy[k]=;}
void build(int k,int l,int r)
{
L[k]=l,R[k]=r;lazy[k]=;
if (l==r) {sum[k]=f[l];return;}
int mid=l+r>>;
build(k<<,l,mid);
build(k<<|,mid+,r);
sum[k]=(sum[k<<]+sum[k<<|])%P;
}
void modify(int k,int l,int r,int x)
{
if (L[k]==l&&R[k]==r) {update(k,x);return;}
if (lazy[k]!=) down(k);
int mid=L[k]+R[k]>>;
if (r<=mid) modify(k<<,l,r,x);
else if (l>mid) modify(k<<|,l,r,x);
else modify(k<<,l,mid,x),modify(k<<|,mid+,r,x);
sum[k]=(sum[k<<]+sum[k<<|])%P;
}
int main()
{
#ifndef ONLINE_JUDGE
freopen("bzoj4597.in","r",stdin);
freopen("bzoj4597.out","w",stdout);
const char LL[]="%I64d\n";
#else
const char LL[]="%lld\n";
#endif
n=read(),m=read();
for (int i=;i<=n;i++) a[i]=read();
f[n]=,f[n-]=;
for (int i=n-;i>=;i--) f[i]=3ll*f[i+]%P;
int ans=,s=;
for (int i=;i<=n;i++)
{
s=1ll*s*a[i]%P;
f[i]=1ll*s*f[i]%P;
}
build(,,n);
while (m--)
{
int x=read(),y=read();
modify(,x,n,1ll*y*inv(a[x])%P);
a[x]=y;
printf("%d\n",sum[]);
}
return ;
}