跳到正文

个人记录

2

_CuteRalsei_约 34 分钟
原文
文章信息与版本

文章信息

发布
2026年11月9日
收录
2026年11月9日
更新
2026年11月18日
篇幅
约 34 分钟
获赞
1

版本

  1. v1当前

f(i)f(i):从 ii 位置开始读出!

f(i)=∑j=in+i−110sumL−∑k=ijLkajf(i)=\sum_{j=i}^{n+i-1}10^{sumL-\sum_{k=i}^jL_k}a_j

然后考虑优化,设 hj=10suml−preljajh_j=10^{suml-prel_j}a_j:

all=∑i=1n∑j=in+i−110sumL−∑k=ijLkaj=∑i=1n10prei−1∑j=in+i−110suml−preljaj=∑i=1n10prei−1∑j=in+i−1hj=∑i=1n10prei−1(preh(n+i−1)−preh(i−1))\begin{aligned}all&=\sum_{i=1}^n\sum_{j=i}^{n+i-1}10^{sumL-\sum_{k=i}^jL_k}a_j \\&=\sum_{i=1}^n{10^{pre_{i-1}}}\sum_{j=i}^{n+i-1}10^{suml-prel_j}a_j \\&=\sum_{i=1}^n10^{pre_{i-1}}\sum_{j=i}^{n+i-1}h_j \\&=\sum_{i=1}^n10^{pre_{i-1}}(preh(n+i-1)-preh(i-1)) \end{aligned}

现在我们需要维护这个式子。

将 {a,L}←{a+da,L+dL}\{a,L\}\gets \{a+da, L+dL\},影响:

prelj←prelj+dL,j≥iprel_j\gets prel_j+dL,j\ge i。suml←suml+dLsuml\gets suml +dL。

考虑更新 h[1,n]h[1,n]:

hj′:{×10dL,j<i+10suml−preida,j=i+0,j>iprehj′:{×10dL,j<i+(10dL−1)preh(i−1)+10suml−prelida,j≥ih'_j:\begin{cases}\times 10^{dL},&j<i\\+10^{suml-pre_i}da,&j=i\\+0,&j>i\end{cases} \\ preh'_j:\begin{cases}\times 10^{dL},&j<i\\+(10^{dL}-1)preh(i-1)+10^{suml-prel_i}da,&j\ge i\end{cases}

以及

hn+j=10−sumlhjpreh(n+j−1)=preh(n)+10−sumlpreh(j−1)h_{n+j}=10^{-suml}h_j\\preh(n+j-1)=preh(n)+10^{-suml}preh(j-1)

还要射出

  • G0(i)=10prei−1preh(n+i−1)=φ(i−1)(preh(n)+10−sumlpreh(i−1))G_0(i)=10^{pre_{i-1}}preh(n + i - 1)=\varphi(i-1)(preh(n)+10^{-suml}preh(i-1))。
  • G1(i)=10prei−1preh(i−1)=φ(i−1)preh(i−1)G_1(i)=10^{pre_{i-1}}preh(i-1)=\varphi(i-1)preh(i-1)。
  • φ(x)=10prex\varphi(x)=10^{pre_{x}}。

考虑 {φ}\{\varphi\} 的变化:

φ(j):{+0,j<i×10dL,j≥i\varphi(j):\begin{cases}+0,&j<i\\\times 10^{dL},&j\ge i\end{cases}

考虑 hn+j′h'_{n+j} 怎么变(suml−pre+i=−prei{suml-pre_{+i}=-pre_i}):

hn+i′=10suml′−pren+i′(ai+da)=10(suml+dL)−(suml+prei+2dL)(ai+da)=10−prei−dL(ai+da)=10−dL10−preiai+10−prei−dLda=10−dL10suml−pren+iai+10−prei−dLda=10−dLhn+i+10−prei−dLda\begin{aligned}h_{n+i}'&=10^{suml'-pre_{n+i}'}(a_i+da)\\&=10^{(suml+dL)-(suml+pre_i+2dL)}(a_i+da)\\&=10^{-pre_i-dL}(a_i+da)\\&=10^{-dL}10^{-pre_i}a_i+10^{-pre_i-dL}da\\&=10^{-dL}10^{suml-pre_{n+i}}a_i+10^{-pre_i-dL}da\\&=10^{-dL}h_{n+i}+10^{-pre_i-dL}da\end{aligned}

所以这里 hn+ih_{n+i} 比别的 hh 还多一个 10−prei−dLda10^{-pre_i-dL}da。

设 C=(10dL−1)prehi−1+10suml−preidaC=(10^{dL}-1)preh_{i-1}+10^{suml-pre_i}da,C′=C+10−prei−dLdaC'=C+10^{-pre_i-dL}da。

我们考虑超过 nn 的部分如何更新 prehpreh:

i≤k=n+r<n+ii\le k=n+r< n + i,那么:

preh(k)′=preh′(n)+10−suml′preh′(r)=preh(n)+C+10−suml−dL(10dLpreh(r))=preh(n)+C+10−sumlpreh(r)=preh(n)+10−sumlpreh(r)+C=preh(k)+C\begin{aligned}preh(k)'&=preh'(n)+10^{-suml'}preh'(r)\\&=preh(n)+C+10^{-suml-dL}(10^{dL}preh(r))\\&=preh(n)+C+10^{-suml}preh(r)\\&=preh(n)+10^{-suml}preh(r)+C\\&=preh(k)+C\end{aligned}

当 j−1<ij-1<i 时:φ(j−1)\varphi(j-1) 不变;由 prehpreh 的更新方式得到

G0(j)′=φ(j−1)preh(n+j−1)′=φ(j−1)(preh(n+j−1)+C)\begin{aligned}G_0(j)'&=\varphi(j-1)preh(n+j-1)'\\&=\varphi(j-1)(preh(n+j-1)+C)\end{aligned}

所以 G0(j)′:+φ(j−1)CG_0(j)':+\varphi(j-1)C。


当 j−1≥ij-1\ge i 时,φ(j−1)\varphi(j-1) 乘上 10dL10^{dL}。

考虑如何更新 preh(n+j−1)preh(n+j-1):

preh(n+x)=preh(n+i−1)+∑u=n+in+xhu\begin{aligned}preh(n+x)&=preh(n+i-1)+\sum_{u=n+i}^{n+x}h_u\end{aligned}

前面 +C+C,后面是乘 10−dL10^{-dL},因此 +(10−dL−1)一坨+(10^{-dL}-1) 一坨。

preh(n+x)′=preh(n+x)+C′+(10−dL−1)(preh(n+x)−preh(n+i−1))preh(n+x)'=preh(n+x)+C'+(10^{-dL}-1)(preh(n+x)-preh(n+i-1)) G0(j)′=10dLφ(j−1)preh(n+j−1)′=10dLφ(j−1)(preh(n+j−1)+C′+(10−dL−1)(preh(n+j−1))−preh(n+i−1))=10dLφ(j−1)(C′+10−dLpreh(n+j−1)−(10−dL−1)preh(n+i−1))=G0(j)+10dLφ(j−1)C′+(10dL−1)φ(j−1)preh(n+i−1)=G0(j)+φ(j−1)(10dLC′+(10dL−1)preh(n+i−1))\begin{aligned}G_0(j)'&=10^{dL}\varphi(j-1)preh(n+j-1)'\\&=10^{dL}\varphi(j-1)(preh(n+j-1)+C'+(10^{-dL}-1)(preh(n+j-1))-preh(n+i-1))\\&=10^{dL}\varphi(j-1)(C'+10^{-dL}preh(n+j-1)-(10^{-dL}-1)preh(n+i-1))\\&=G_0(j)+10^{dL}\varphi(j-1)C'+(10^{dL}-1)\varphi(j-1)preh(n+i-1)\\&=G_0(j)+\varphi(j-1)(10^{dL}C'+(10^{dL}-1)preh(n+i-1))\end{aligned}

j−1<ij-1<i:G1(j)′=10dLG1(j)G_1(j)'=10^{dL}G_1(j)。


j−1≥ij-1\ge i:G1(j)′=10dLG1(j)+10dLCφ(j−1)G_1(j)'=10^{dL}G_1(j)+10^{dL}C\varphi(j-1)。


现在我们需要维护 S0=∑j=1nG0(j),S1=∑j=1nG1(j)S_0=\displaystyle\sum_{j=1}^nG_0(j),S_1=\sum_{j=1}^nG_1(j)。

设 Lφ=∑x=0i−1φ(x),Rφ=∑x=in−1φ(x)L_\varphi=\displaystyle \sum_{x=0}^{i-1}\varphi(x),R_\varphi=\sum_{x=i}^{n-1}\varphi(x)。

设 coef0=C,coef1=10dLC′+(10dL−1)preh(n+i−1)coef_0=C,coef_1=10^{dL}C'+(10^{dL}-1)preh(n+i-1)(由上面的公式得到)。

那么:

S0←S0+Lφcoef0+Rφcoef1S1←10dLS1+10dLCRφS_0\gets S_0+L_\varphi coef_0+R_\varphi coef_1 \\ S_1\gets 10^{dL}S_1+10^{dL}CR_\varphi
卡常之前
C++
#include<bits/stdc++.h>
using namespace std;
#define fir(i, l, r) for(int i = l; i <= r; i++)
#define fpr(i, r, l) for(int i = r; i >= l; i--)
#define full(v) v.begin(), v.end()
#define endl '\n'
template <int INBUF = 1000000, int OBUF = 20000000>
struct IO{
    char *p1 = 0, *p2 = 0, buf[INBUF], obuf[OBUF], *op = obuf;
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, INBUF, stdin), p1 == p2) ? EOF : *p1++)
    __attribute__((always_inline)) int read(){
        register int x = 0; register char ch = gc();
        while(ch < '0' || ch > '9') ch = gc(); 
        while(ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
        return x;
    }
	__attribute__((always_inline)) void write_with(int _ans, char ch = '\n'){
		if(__builtin_expect(! _ans, 0)){*op++ = 48, *op++ = ch; return ;}
		static char s[10], t = 0;
		while(_ans) s[t++] = _ans % 10 + 48, _ans /= 10;
		while(t) *op++ = s[--t]; *op++ = ch;
	}
	__attribute__((always_inline)) void flush(){fwrite(obuf, 1, op - obuf, stdout);}
};
IO <> flow;
int n, q;
const int maxn = 1e6 + 10;
const int mod = 1000000007;
const int inv = 700000005;
inline int getlen(int x){
	if(x <= 9ll) return 1;
	else if(x <= 99ll) return 2;
	else if(x <= 999ll) return 3;
	else if(x <= 9999ll) return 4;
	else if(x <= 99999ll) return 5;
	else if(x <= 999999ll) return 6;
	else if(x <= 9999999ll) return 7;
	else if(x <= 99999999ll) return 8;
	else if(x <= 999999999ll) return 9;
	else return 10;
}
int L[maxn], suml = 0, a[maxn], pre[maxn], pw[maxn * 5], pi[maxn * 5], h[maxn], preh[maxn];
void init(){
	pw[0] = pi[0] = 1;
	fir(i, 1, maxn * 5 - 1){
		pw[i] = 1ll * pw[i - 1] * 10 % mod;
		pi[i] = 1ll * pi[i - 1] * inv % mod;
	}
}
#define qpow(b) ((b) >= 0 ? pw[b] : pi[-(b)])
int phi[maxn], s0, s1;
struct Tree{
	struct node{
		signed add, mul, sum;
	}t[maxn * 4];
	#define ls id << 1
	#define rs id << 1 | 1
	#define len(x) (t[x].r - t[x].l + 1)
	inline void up(int id){
		t[id].sum = t[ls].sum + t[rs].sum;
		if(t[id].sum >= mod) t[id].sum -= mod;
		else if(t[id].sum <= -mod) t[id].sum += mod;
	}
	inline void down(int id, int L, int mid, int R){
		if(t[id].mul ^ 1){
			t[ls].mul = 1ll * t[ls].mul * t[id].mul % mod;
			t[rs].mul = 1ll * t[rs].mul * t[id].mul % mod;
			t[ls].sum = 1ll * t[ls].sum * t[id].mul % mod;
			t[rs].sum = 1ll * t[rs].sum * t[id].mul % mod;
			t[id].mul = 1;
		}
		if(t[id].add){
			const int len1 = mid - L + 1, len2 = R - mid;
			t[ls].add = (t[ls].add + t[id].add) % mod;
			t[rs].add = (t[rs].add + t[id].add) % mod;
			t[ls].sum += 1ll * len1 * t[id].add % mod, t[ls].sum %= mod;
			t[rs].sum += 1ll * len2 * t[id].add % mod, t[rs].sum %= mod;
			t[id].add = 0;
		}
	}
	void build(int id, int l, int r, int vec[]){
		t[id] = {0, 1, 0};
		if(l == r) return t[id].sum = vec[l], void();
		int mid = (l + r) >> 1;
		build(ls, l, mid, vec), build(rs, mid + 1, r, vec);
		up(id);
	}
	void multiply(int id, int l, int r, int mu, int L, int R){
		if(l <= L && R <= r){
			t[id].mul = 1ll * t[id].mul * mu % mod;
			t[id].add = 1ll * t[id].add * mu % mod;
			t[id].sum = 1ll * t[id].sum * mu % mod;
			return;
		}
		int mid = (L + R) >> 1;
		down(id, L, mid, R);
		if(l <= mid) multiply(ls, l, r, mu, L, mid);
		if(r > mid)  multiply(rs, l, r, mu, mid + 1, R);
		up(id);
	}
	void update(int id, int x, int ad, int L, int R){
		int mid = (L + R) >> 1;
		if(L == R){
			t[id].add += ad; if(t[id].add >= mod) t[id].add -= mod;
			t[id].sum += 1ll * (R - L + 1) * ad % mod, t[id].sum %= mod;
			return ;
		}
		down(id, L, mid, R);
		if(x <= mid) update(ls, x, ad, L, mid);
		else update(rs, x, ad, mid + 1, R);
		up(id);
	}
	int query(int id, int l, int r, int L, int R){
		if(l <= L && R <= r) return t[id].sum;
		int mid = (L + R) >> 1; if(L ^ R) down(id, L, mid, R);
		int ans = 0;
		if(l <= mid) ans += query(ls, l, r, L, mid);
		if(mid < r) ans += query(rs, l, r, mid + 1, R);
		if(ans >= mod) ans -= mod;
		else if(ans <= -mod) ans += mod;
		return ans;
	}
}_h, _phi;
struct fenwick{
	int tr[maxn+ 50000];
	void build(int n, int vec[]){
		fir(i, 1, n) tr[i] = vec[i];
		fir(i, 1, n) tr[i + (i & -i)] += tr[i]; 
	}
	void update(int x, int d){
		for(;x < maxn; x += x & -x) tr[x] += d;
	}
	int __query(int x){
		int res = 0;
		for(; x; x -= x & -x) res += tr[x];
		return res;
	}
	int query(int l, int r){return __query(r) - __query(l - 1);}
}_L;
signed main(){
	ios :: sync_with_stdio(0);
	cin.tie(0), cout.tie(0);
	freopen("marquee.in", "r", stdin);
	freopen("marquee.out", "w", stdout);
	init();
	n = flow.read(), q = flow.read();
	fir(i, 1, n) a[i] = a[i + n] = flow.read(), L[i] = L[i + n] = getlen(a[i]), suml += L[i];
	fir(i, 1, n * 2) pre[i] = pre[i - 1] + L[i];
	fir(i, 1, n * 2) h[i] = 1ll * qpow(suml - pre[i]) * a[i] % mod, phi[i] = qpow(pre[i]);
	fir(i, 1, n * 2){
		preh[i] = preh[i - 1] + h[i];
		if(preh[i] >= mod) preh[i] -= mod;
	}
	phi[0] = 1;
	fir(i, 1, n){
		s0 += 1ll * phi[i - 1] * preh[n + i - 1] % mod; s0 %= mod;
		s1 += 1ll * phi[i - 1] * preh[i - 1] % mod, s1 %= mod;
	}
	_h.build(1, 1, n * 2, h);
	_phi.build(1, 0, n - 1, phi);
	_L.build(n * 2, L);
	flow.write_with((s0 + mod - s1) % mod, 10);
	while(q--){
		const int i = flow.read(), newa = flow.read();
		const int da = newa - a[i]; a[i] = newa;
		const int dl = getlen(a[i]) - L[i]; L[i] = getlen(a[i]);
		const int pre_ = _L.query(1, i), preh1 = 1 <= i - 1 ? _h.query(1, 1, i - 1, 1, n * 2) : 0, preh2 = _h.query(1, 1, n + i - 1, 1, n * 2);
		const int pdl = qpow(dl), ppdl = qpow(-pre_ - dl), psr = qpow(suml - pre_);
		const int L = _phi.query(1, 0, i - 1, 0, n - 1), R = _phi.t[1].sum - L;
		const int coef0 = ((1ll * pdl - 1) * preh1 % mod + 1ll * psr * da % mod) % mod;
		const int C_ = (coef0 + 1ll * ppdl * da % mod) % mod;
		const int coef1 = (1ll * pdl * C_ % mod + 1ll * (pdl - 1) * preh2 % mod) % mod;
		s0 += (1ll * L * coef0 % mod + 1ll * R * coef1 % mod) % mod; if(s0 >= mod) s0 -= mod;
		s1 = (1ll * pdl * s1 % mod + 1ll * pdl * coef0 % mod * R % mod) % mod;
		if(1 <= i - 1) _h.multiply(1, 1, i - 1, pdl, 1, n * 2);
		_h.update(1, i, 1ll * psr * da % mod, 1, n * 2);
		_h.multiply(1, n + i, n * 2, qpow(-dl), 1, n * 2);
		_h.update(1, n + i, 1ll * ppdl * da % mod, 1, n * 2);
		if(i <= n - 1) _phi.multiply(1, i, n - 1, pdl, 0, n - 1);
		_L.update(i, dl); _L.update(n + i, dl);
		suml += dl;
		if(s0 < 0) s0 += mod;
		if(s1 < 0) s1 += mod;
		flow.write_with((1ll * s0 + mod - s1) % mod, 10); 
	} 
	flow.flush();
	return 0;
}	
AI 卡常
C++
#include<bits/stdc++.h>
using namespace std;
#define fir(i, l, r) for(int i = l; i <= r; i++)
#define fpr(i, r, l) for(int i = r; i >= l; i--)
#define endl '\n'
template <int INBUF = 1000000, int OBUF = 20000000>
struct IO{
    char *p1 = 0, *p2 = 0, buf[INBUF], obuf[OBUF], *op = obuf;
    #define gc() (p1 == p2 && (p2 = (p1 = buf) + fread(buf, 1, INBUF, stdin), p1 == p2) ? EOF : *p1++)
    __attribute__((always_inline)) int read(){
        register int x = 0; register char ch = gc();
        while(ch < '0' || ch > '9') ch = gc();
        while(ch >= '0' && ch <= '9') x = (x << 1) + (x << 3) + (ch ^ 48), ch = gc();
        return x;
    }
    __attribute__((always_inline)) void write_with(int _ans, char ch = '\n'){
        if(__builtin_expect(! _ans, 0)){ *op++ = 48, *op++ = ch; return; }
        static char s[20], t = 0;
        while(_ans) s[t++] = _ans % 10 + 48, _ans /= 10;
        while(t) *op++ = s[--t]; *op++ = ch;
    }
    __attribute__((always_inline)) void flush(){fwrite(obuf, 1, op - obuf, stdout);}
};
IO <> flow;
int n, q;
const int maxn = 1e6 + 10;
const int mod = 1000000007;
const int inv = 700000005;
inline int getlen(int x){
    if(x <= 9ll) return 1;
    else if(x <= 99ll) return 2;
    else if(x <= 999ll) return 3;
    else if(x <= 9999ll) return 4;
    else if(x <= 99999ll) return 5;
    else if(x <= 999999ll) return 6;
    else if(x <= 9999999ll) return 7;
    else if(x <= 99999999ll) return 8;
    else if(x <= 999999999ll) return 9;
    else return 10;
}
inline int adj(long long x){
    x %= mod;
    return (int)(x < 0 ? x + mod : x);
}
int L[maxn], suml = 0, a[maxn], pw[maxn * 5], pi[maxn * 5], h[maxn];
void init(){
    pw[0] = pi[0] = 1;
    fir(i, 1, maxn * 5 - 1){
        pw[i] = 1ll * pw[i - 1] * 10 % mod;
        pi[i] = 1ll * pi[i - 1] * inv % mod;
    }
}
#define qpow(b) ((b) >= 0 ? pw[b] : pi[-(b)])
int phi[maxn], s0, s1;
struct Tree{
    struct node{
        int mul, imul, sum;
    }t[maxn * 4];
    #define ls id << 1
    #define rs id << 1 | 1
    inline void up(int id){
        int x = t[ls].sum + t[rs].sum;
        if(x >= mod) x -= mod;
        if(t[id].mul != 1) x = 1ll * x * t[id].mul % mod;
        t[id].sum = x;
    }
    void build(int id, int l, int r, int vec[]){
        t[id].mul = 1; t[id].imul = 1; t[id].sum = 0;
        if(l == r){ t[id].sum = vec[l]; return; }
        int mid = (l + r) >> 1;
        build(ls, l, mid, vec);
        build(rs, mid + 1, r, vec);
        up(id);
    }
    // 惰性乘法:不 down;mu 与 imu = Inv(mu) 同时传入,供 update 使用
    void multiply(int id, int l, int r, int mu, int imu, int L, int R){
        if(mu == 1) return;
        if(l <= L && R <= r){
            t[id].mul  = 1ll * t[id].mul  * mu  % mod;
            t[id].imul = 1ll * t[id].imul * imu % mod;
            t[id].sum  = 1ll * t[id].sum  * mu  % mod;
            return;
        }
        int mid = (L + R) >> 1;
        if(l <= mid) multiply(ls, l, r, mu, imu, L, mid);
        if(r > mid)  multiply(rs, l, r, mu, imu, mid + 1, R);
        up(id);
    }
    // 点加:imul 累积祖先 mul 之积的逆,叶子处一次乘法,无需 Inv
    void update(int id, int x, int ad, int L, int R, int imul = 1){
        if(ad == 0) return;
        if(L == R){
            t[id].sum += 1ll * ad * imul % mod;
            if(t[id].sum >= mod) t[id].sum -= mod;
            return;
        }
        imul = 1ll * imul * t[id].imul % mod;
        int mid = (L + R) >> 1;
        if(x <= mid) update(ls, x, ad, L, mid, imul);
        else update(rs, x, ad, mid + 1, R, imul);
        up(id);
    }
    // 区间查询:mul 累积祖先 mul,完全覆盖时只乘一次
    int query(int id, int l, int r, int L, int R, int mul = 1){
        if(l <= L && R <= r) return 1ll * t[id].sum * mul % mod;
        mul = 1ll * mul * t[id].mul % mod;
        int mid = (L + R) >> 1;
        int ans = 0;
        if(l <= mid) ans = query(ls, l, r, L, mid, mul);
        if(r > mid){
            ans += query(rs, l, r, mid + 1, R, mul);
            if(ans >= mod) ans -= mod;
        }
        return ans;
    }
}_h, _phi;
struct fenwick{
    int tr[maxn];
    void build(int n, int vec[]){
        fir(i, 1, n) tr[i] = vec[i];
        fir(i, 1, n){
            int j = i + (i & -i);
            if(j <= n) tr[j] += tr[i];
        }
    }
    inline void update(int x, int d){for(; x < maxn; x += x & -x) tr[x] += d;}
    inline int __query(int x){
        register int res = 0;
        for(; x; x -= x & -x) res += tr[x];
        return res;
    }
    inline int query(int l, int r){return __query(r) - __query(l - 1); }
}_L;
signed main(){
    ios :: sync_with_stdio(0);
    cin.tie(0), cout.tie(0);
    freopen("marquee.in", "r", stdin);
    freopen("marquee.out", "w", stdout);
    init();
    n = flow.read(), q = flow.read();
    fir(i, 1, n) a[i] = a[i + n] = flow.read(), L[i] = L[i + n] = getlen(a[i]), suml += L[i];
    // h[1..2n]
    int cur = 0;
    fir(i, 1, n * 2){
        cur += L[i];
        h[i] = 1ll * qpow(suml - cur) * a[i] % mod;
    }
    // phi[0..n-1]
    phi[0] = 1;
    int curl = 0;
    fir(i, 1, n - 1){
        curl += L[i];
        phi[i] = qpow(curl);
    }
    int H = 0;
    s0 = s1 = 0;
    fir(k, 1, n * 2){
        if(k <= n){
            s1 += 1ll * phi[k - 1] * H % mod;
            if(s1 >= mod) s1 -= mod;
        }
        H += h[k]; if(H >= mod) H -= mod;
        if(k >= n && k < n * 2){
            s0 += 1ll * phi[k - n] * H % mod;
            if(s0 >= mod) s0 -= mod;
        }
    }
    _h.build(1, 1, n * 2, h);
    _phi.build(1, 0, n - 1, phi);
    _L.build(n, L);
    int ans = s0 - s1;
    if(ans < 0) ans += mod;
    flow.write_with(ans, 10);
    while(q--){
        int i = flow.read(), newa = flow.read();
        const int da = newa - a[i]; a[i] = newa;
        const int newl = getlen(newa);
        const int dl = newl - L[i]; L[i] = newl;
        // 快速路径:无变化
        if(da == 0 && dl == 0){
            flow.write_with(adj(s0 - s1), 10);
            continue;
        }
        const int pre_ = _L.__query(i);
        const int preh1 = (i > 1) ? _h.query(1, 1, i - 1, 1, n * 2) : 0;
        const int preh2 = _h.query(1, 1, n + i - 1, 1, n * 2);
        const int L = _phi.query(1, 0, i - 1, 0, n - 1);
        const int R = adj(_phi.t[1].sum - L);
        const int pdl = qpow(dl);
        const int pidl = qpow(-dl);
        const int psr = qpow(suml - pre_);
        const int ppdl = qpow(-pre_ - dl);
        const int coef0 = adj((1ll * pdl - 1) * preh1 + 1ll * psr * da);
        const int tmp = adj(coef0 + 1ll * ppdl * da);
        const int coef1 = adj(1ll * pdl * tmp + 1ll * (pdl - 1) * preh2);
        s0 = adj(s0 + 1ll * L * coef0 + 1ll * R * coef1);
        s1 = adj(1ll * pdl * s1 + 1ll * pdl * coef0 % mod * R);
        // dl != 0 才需要 multiply(dl==0 时 pdl=pidl=1,乘 1 是空操作)
        if(dl != 0){
            if(i > 1) _h.multiply(1, 1, i - 1, pdl, pidl, 1, n * 2);
            _h.multiply(1, n + i, n * 2, pidl, pdl, 1, n * 2);
            if(i < n) _phi.multiply(1, i, n - 1, pdl, pidl, 0, n - 1);
            _L.update(i, dl);
        }
        // da != 0 才需要 update(da==0 时加 0 是空操作)
        // 注意:必须在 [n+i, 2n] 乘 pidl 之后
        if(da != 0){
            _h.update(1, i, adj(1ll * psr * da), 1, n * 2);
            _h.update(1, n + i, adj(1ll * ppdl * da), 1, n * 2);
        }
        suml += dl;
        flow.write_with(adj(s0 - s1), 10);
    }
    flow.flush();
    return 0;
}
原文