题意:
给定一个长度为
n
n
n的十六进制数,和
m
m
m次询问,询问格式为
(
o
p
,
x
,
y
)
(op, x, y)
(op,x,y),当
o
p
=
1
op = 1
op=1,表示将第
x
x
x个数位的数字改为
y
y
y,当
o
p
=
2
op = 2
op=2时,问区间
[
x
,
y
]
[x,y]
[x,y]的价值为多少,其中区间
[
l
,
r
]
[l,r]
[l,r]区间的价值定义为:
取出[l,r]中的所有子串
v
v
v(不要求连续),计算
S
O
D
(
v
)
SOD(v)
SOD(v),则区间价值为:
∑
(
n
u
m
b
e
r
o
f
S
O
D
(
v
)
=
i
)
∗
102
1
i
\sum (number \ of \ SOD(v) = i) * 1021^i
∑(number of SOD(v)=i)∗1021i
m
o
d
(
1
e
9
+
7
)
mod (1e9 + 7)
mod(1e9+7)
而 S O D ( v ) SOD(v) SOD(v)的定义为:
def SOD(v):
if v < 16:
return v
else:
return SOD(sum of digit of v in hexadecimal)
思路:
首先对于
S
O
D
(
v
)
SOD(v)
SOD(v),由同余定理,可以得到其等价于:
def SOD(v):
if v == 0:
return 0
elif v % 15 == 0:
return 15
else:
return v % 15
故对于区间价值的计算,
i
i
i只有16种取值。
故可以使用线段树来维护区间中所有子序列的SOD值为
i
i
i的个数。
其中区间合并类似于归并排序,需要考虑两个子区间的相互影响和独立性,故合并代码如下:
void push_up(int rt){
for (int i = 0; i < 16; i++) {
Tree[rt].sum[i] = (Tree[lson].sum[i] + Tree[rson].sum[i]) % mod;
}
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
int x = (i + j) % 15;
if (!i && !j) x = 0;
else if (x == 0) x = 15;
Tree[rt].sum[x] = (Tree[rt].sum[x] + Tree[lson].sum[i] * Tree[rson].sum[j] % mod) % mod;
}
}
}
其他就是经典的线段树操作了。
此题得解。
代码:
#include<algorithm>
#include<cmath>
#include<cstring>
#include<cstdio>
using namespace std;
typedef long long ll;
#define lson rt<<1
#define rson rt<<1|1
const int mod = 1e9 + 7;
const int A = 1e5 + 10;
const int B = 20;
class Seg_Tree{
public:
int l,r;
ll sum[B];
}Tree[A<<2];
char s[A], t[B];
ll fac[B], res[B];
int n,q;
void Init(){
fac[0] = 1;
for (int i = 1; i < B; i++) {
fac[i] = fac[i - 1] * 1021 % mod;
}
}
int get_val(char c){
if (c >= '0' && c <= '9') return c - '0';
return c - 'A' + 10;
}
void push_up(int rt){
for (int i = 0; i < 16; i++) {
Tree[rt].sum[i] = (Tree[lson].sum[i] + Tree[rson].sum[i]) % mod;
}
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
int x = (i + j) % 15;
if (!i && !j) x = 0;
else if (x == 0) x = 15;
Tree[rt].sum[x] = (Tree[rt].sum[x] + Tree[lson].sum[i] * Tree[rson].sum[j] % mod) % mod;
}
}
}
void build_Tree(int rt, int l, int r){
Tree[rt].l = l, Tree[rt].r = r;
for (int i = 0; i < 16; i++) Tree[rt].sum[i] = 0;
if (l == r) {
Tree[rt].sum[get_val(s[l])] = 1;
return;
}
int mid = (l + r) >> 1;
build_Tree(lson, l, mid);
build_Tree(rson, mid + 1, r);
push_up(rt);
}
void update(int rt, int pos, int c){
int l = Tree[rt].l, r = Tree[rt].r;
if (l == r) {
for (int i = 0; i < 16; i++) Tree[rt].sum[i] = 0;
Tree[rt].sum[c] = 1;
return;
}
int mid = (l + r) >> 1;
if (pos <= mid) update(lson, pos, c);
else update(rson, pos, c);
push_up(rt);
}
void Merge(ll* now){
ll tem[B] = {0};
for (int i = 0; i < 16; i++) tem[i] = (res[i] + now[i]) % mod;
for (int i = 0; i < 16; i++) {
for (int j = 0; j < 16; j++) {
int x = (i + j) % 15;
if (!i && !j) x = 0;
else if (x == 0) x = 15;
tem[x] = (tem[x] + res[i] * now[j] % mod) % mod;
}
}
for (int i = 0; i < 16; i++) res[i] = tem[i];
}
void query(int rt, int st, int ed){
int l = Tree[rt].l, r = Tree[rt].r;
if (st <= l && r <= ed) {
Merge(Tree[rt].sum);
return;
}
int mid = (l + r) >> 1;
if (st <= mid) query(lson, st, ed);
if (ed > mid) query(rson, st, ed);
}
int main(){
Init();
scanf("%d%d", &n, &q);
scanf("%s", s+1);
build_Tree(1, 1, n);
while (q--) {
int op;
scanf("%d", &op);
if (op == 1) {
int p;
scanf("%d%s",&p,t);
update(1, p, get_val(t[0]));
} else {
int l, r;
scanf("%d%d", &l, &r);
query(1, l, r);
ll ans = 0;
for (int i = 0; i < 16; i++) {
ans = (ans + fac[i] * res[i] % mod) % mod;
res[i] = 0;
}
printf("%lld\n", ans);
}
}
return 0;
}