:从 位置开始读出!
然后考虑优化,设 :
现在我们需要维护这个式子。
将 ,影响:
。。
考虑更新 :
以及
还要射出
- 。
- 。
- 。
考虑 的变化:
考虑 怎么变():
所以这里 比别的 还多一个 。
设 ,。
我们考虑超过 的部分如何更新 :
,那么:
当 时: 不变;由 的更新方式得到
所以 。
当 时, 乘上 。
考虑如何更新 :
前面 ,后面是乘 ,因此 。
:。
:。
现在我们需要维护 。
设 。
设 (由上面的公式得到)。
那么:
卡常之前
#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 卡常
#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;
}