To give you a series, insert the plus sign, minus sign or multiplication sign between two adjacent numbers.
Each time a single point of modification is supported, the sum of all the expressions obtained in this way is obtained. The membrane is 1e9 + 7.
SOL:
I am a Sb...
It can be found that if a plus sign appears at a position, there must be a minus sign to remove it, so the answer is the sum of prefix products that appear several times.
Calculate the contribution of each prefix product.
#include<bits/stdc++.h>#define int long longusing namespace std;inline int read(){ int x = 0,f = 1;char ch = getchar(); for(;!isdigit(ch);ch = getchar())if(ch == ‘-‘)f = -f; for(;isdigit(ch);ch = getchar())x = 10 * x + ch - ‘0‘; return x * f;}int n,q;const int mod = 1e9 + 7,maxn = 1e5 + 10;int a[maxn],fac[maxn];#define ls (x << 1)#define rs ((x << 1) | 1)int seg[maxn << 2],tag[maxn << 2];inline int pw(int x,int t){ int res = 1;x %= mod; while(t) { if(t & 1)res = res * x % mod; x = x * x % mod; t = t >> 1; } return res;}inline void build(int x,int l,int r){ tag[x] = 1; if(l == r)seg[x] = fac[l]; else { int mid = (l + r) >> 1; build(ls,l,mid);build(rs,mid + 1,r); seg[x] = (seg[ls] + seg[rs]) % mod; } }inline void pushdown(int x,int l,int r){ if(tag[x] != 1) { (tag[ls] *= tag[x]) %= mod;(tag[rs] *= tag[x]) %= mod; (seg[ls] *= tag[x]) %= mod;(seg[rs] *= tag[x]) %= mod; tag[x] = 1; }}inline void update(int x,int l,int r,int L,int R,int val){ if(L <= l && r <= R) { (seg[x] *= val) %= mod; (tag[x] *= val) %= mod; return; } pushdown(x,l,r); int mid = (l + r) >> 1; if(L <= mid)update(ls,l,mid,L,R,val); if(R > mid)update(rs,mid + 1,r,L,R,val); seg[x] = (seg[ls] + seg[rs]) % mod;}signed main(){ n = read(),q = read(); for(int i=1;i<=n;i++)a[i] = read(); int mul = 1; for(int i=1;i<=n;i++) { mul = (long long)mul * a[i] % mod; if(i == n)fac[i] = mul; else fac[i] = (long long)mul * 2 * pw(3, n - i - 1) % mod; }build(1,1,n); while(q--) { int p = read(),v = read(); update(1,1,n,p,n,(long long)v * pw(a[p],mod - 2) % mod); a[p] = v; printf("%lld\n",seg[1]); }}
View code
$ \ Sum _ {I = 1} ^ {n-1} sum_ I \ times 2 \ times 3 ^ {n-i-1} + sum_n $
$ Sum $ array is prefix Product
Shoi2016 random sequence