牛客網暑期ACM多校訓練營(第三場)F 思維 + 線段樹
阿新 • • 發佈:2019-02-06
題意:
給定一個長度為的十六進位制數,和次詢問,詢問格式為,當,表示將第個數位的數字改為,當時,問區間的價值為多少,其中區間區間的價值定義為:
取出[l,r]中的所有子串(不要求連續),計算,則區間價值為:
而的定義為:
def SOD(v):
if v < 16:
return v
else:
return SOD(sum of digit of v in hexadecimal)
思路:
首先對於,由同餘定理,可以得到其等價於:
def SOD(v):
if v == 0:
return 0
elif v % 15 == 0:
return 15
else:
return v % 15
故對於區間價值的計算,只有16種取值。
故可以使用線段樹來維護區間中所有子序列的SOD值為的個數。
其中區間合併類似於歸併排序,需要考慮兩個子區間的相互影響和獨立性,故合併程式碼如下:
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;
}