题解:P17326 【9-X】经典追求

· · 题解

题意简述

对一个序列的区间 [l,r] 进行 6 种操作或查询:

任何一个数都可以写成这些质数的若干次方相乘。那么“乘 x”就相当于给每个质因数的指数加上一个数,“除以 x”就相当于减去一个数。

这样,乘除操作就变成了区间加减。因此,区间查询 3、4 就转化为对每个质数求区间指数的最小值或最大值,然后乘起来。

因此考虑线段树,对每个质数单独开一棵线段树,维护这些指数。

状态维护

开始拆解操作 5,设该集合的最小值为 m,次小值(若存在)为 m_2,最小值出现次数为 c_m。

同理,操作 6 就是在操作 5 的基础上换成了维护区间最大值、次大值和最大值出现次数。

因此,线段树中需要维护的值如下:

struct tree {
    int l, r; // 区间左右端点
    int mn, mn2, cmn; // 最小值、次小值、最小值个数
    int mx, mx2, cmx; // 最大值、次大值、最大值个数
    int tag; // 懒标记(区间加会用到)
};

区间合并

当我们合并左儿子和右儿子的信息来得到父节点的信息时,需要分别处理最小值和最大值两组统计量,因为它们是独立对称的。

对于最小值,我们要确定父节点区间内的最小值、这个最小值出现了多少次,以及除最小值以外第二小的值(次小值)是多少。

先比较左儿子的最小值和右儿子的最小值。

最大值的合并同理。这部分代码如下:

:::success[合并部分代码]

tree push_up(const tree& a, const tree& b) {
    tree res;
    if(a.mn < b.mn) {
        res.mn = a.mn;
        res.cmn = a.cmn;
        res.mn2 = min(a.mn2, b.mn);
    } else if(a.mn > b.mn) {
        res.mn = b.mn;
        res.cmn = b.cmn;
        res.mn2 = min(b.mn2, a.mn);
    } else {
        res.mn = a.mn;
        res.cmn = a.cmn + b.cmn;
        res.mn2 = min(a.mn2, b.mn2);
    }
    if(a.mx > b.mx) {
        res.mx = a.mx;
        res.cmx = a.cmx;
        res.mx2 = max(a.mx2, b.mx);
    } else if(a.mx < b.mx) {
        res.mx = b.mx;
        res.cmx = b.cmx;
        res.mx2 = max(b.mx2, a.mx);
    } else {
        res.mx = a.mx;
        res.cmx = a.cmx + b.cmx;
        res.mx2 = max(a.mx2, b.mx2);
    }
    return res;
}

:::

建树 / 修改 / 查询

建树时需要初始化区间次小值为正无穷,次大值为负无穷,表示不存在。合并时可以减少特判。

查询操作记得返回一个完整的节点信息,通过合并左右查询结果得到。 :::success[建树 / 修改 / 查询部分代码] void build(int cnt, int id, int l, int r) { t[cnt][id].l = l; t[cnt][id].r = r; if(l == r) { t[cnt][id].mn = t[cnt][id].mx = a[l]; t[cnt][id].cmn = t[cnt][id].cmx = 1; t[cnt][id].mn2 = inf; t[cnt][id].mx2 = -inf; return; } int mid = l + r >> 1; build(cnt, lson, l, mid); build(cnt, rson, mid + 1, r); tree res = push_up(t[cnt][lson], t[cnt][rson]); t[cnt][id].mn = res.mn; t[cnt][id].mn2 = res.mn2; t[cnt][id].cmn = res.cmn; t[cnt][id].mx = res.mx; t[cnt][id].mx2 = res.mx2; t[cnt][id].cmx = res.cmx; } void add(int cnt, int id, int x) { t[cnt][id].mn += x; t[cnt][id].mx += x; t[cnt][id].tag += x; if(t[cnt][id].mn2 != inf) t[cnt][id].mn2 += x; if(t[cnt][id].mx2 != -inf) t[cnt][id].mx2 += x; } void push_down(int cnt, int id) { if(t[cnt][id].tag == 0) return; add(cnt, lson, t[cnt][id].tag); add(cnt, rson, t[cnt][id].tag); t[cnt][id].tag = 0; } void upd(int cnt, int id, int l, int r, int x) { if(t[cnt][id].l >= l && t[cnt][id].r <= r) { add(cnt, id, x); return; } push_down(cnt, id); int mid = t[cnt][id].l + t[cnt][id].r >> 1; if(l <= mid) upd(cnt, lson, l, r, x); if(r > mid) upd(cnt, rson, l, r, x); tree res = push_up(t[cnt][lson], t[cnt][rson]); t[cnt][id].mn = res.mn; t[cnt][id].mn2 = res.mn2; t[cnt][id].cmn = res.cmn; t[cnt][id].mx = res.mx; t[cnt][id].mx2 = res.mx2; t[cnt][id].cmx = res.cmx; } tree ask(int cnt, int id, int l, int r) { if(t[cnt][id].l >= l && t[cnt][id].r <= r) return t[cnt][id]; push_down(cnt, id); int mid = t[cnt][id].l + t[cnt][id].r >> 1, ans = 0; if(r <= mid) return ask(cnt, lson, l, r); if(l > mid) return ask(cnt, rson, l, r); tree ll = ask(cnt, lson, l, r), rr = ask(cnt, rson, l, r); return push_up(ll, rr); } :::

乘、除计算

对 x 分解质因数得到指数数组 cnt。

对于第 i 个质数:若 cnt_i\ne0,调用 upd(i, 1, l, r, cnt[i])(乘法)或 upd(i, 1, l, r, cnt[i])(除法)。 :::success[建树 / 修改 / 查询部分代码]

int prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
void mul(int l, int r, int x) {
    int temp = x;
    for(int i = 0; i < 10; i++) {
        int cnt = 0;
        while(temp % prime[i] == 0) temp /= prime[i], cnt++;
        if(cnt) upd(i, 1, l, r, cnt);
    }
}
void div(int l, int r, int x) {
    int temp = x;
    for(int i = 0; i < 10; i++) {
        int cnt = 0;
        while(temp % prime[i] == 0) temp /= prime[i], cnt++;
        if(cnt) upd(i, 1, l, r, -cnt);
    }
}
:::
### 查询统计
- $op=3$:对于每个质数 i,调用 `ask(i, 1, l, r)` 得到节点信息,取其最小值,用快速幂累乘。
- $op=4$:与 $op=3$ 的情况类似,取最大值。
- $op=5$:取节点信息,若 $c_m>1$,指数取最小值,否则取次小值。
- $op=6$:与 $op=5$ 的情况类似,取最大值或次大值。
### 完整代码
~~赛时花了 $40$ 分钟写出来的。~~
:::success[赛时 AC 代码]
```cpp
#include<bits/stdc++.h>
#define int long long
#define lson (id << 1)
#define rson (id << 1 | 1)
using namespace std;
const int mod = 998244353, inf = 1e9;
int n, m, a[100005], prime[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
struct tree {int l, r, mn, mn2, cmn, mx, mx2, cmx, tag;} t[15][400005];
int qpow(int x, int y) {
    int temp = y, k = x, ans = 1;
    while(temp) {
        if(temp & 1) {
            ans *= k;
            ans %= mod;
        } 
        k *= k;
        k %= mod;
        temp >>= 1;
    }
    return ans;
}
tree push_up(const tree& a, const tree& b) {
    tree res;
    if(a.mn < b.mn) {
        res.mn = a.mn;
        res.cmn = a.cmn;
        res.mn2 = min(a.mn2, b.mn);
    } else if(a.mn > b.mn) {
        res.mn = b.mn;
        res.cmn = b.cmn;
        res.mn2 = min(b.mn2, a.mn);
    } else {
        res.mn = a.mn;
        res.cmn = a.cmn + b.cmn;
        res.mn2 = min(a.mn2, b.mn2);
    }
    if(a.mx > b.mx) {
        res.mx = a.mx;
        res.cmx = a.cmx;
        res.mx2 = max(a.mx2, b.mx);
    } else if(a.mx < b.mx) {
        res.mx = b.mx;
        res.cmx = b.cmx;
        res.mx2 = max(b.mx2, a.mx);
    } else {
        res.mx = a.mx;
        res.cmx = a.cmx + b.cmx;
        res.mx2 = max(a.mx2, b.mx2);
    }
    return res;
}
void build(int cnt, int id, int l, int r) {
    t[cnt][id].l = l;
    t[cnt][id].r = r;
    if(l == r) {
        t[cnt][id].mn = t[cnt][id].mx = a[l];
        t[cnt][id].cmn = t[cnt][id].cmx = 1;
        t[cnt][id].mn2 = inf;
        t[cnt][id].mx2 = -inf;
        return;
    }
    int mid = l + r >> 1;
    build(cnt, lson, l, mid);
    build(cnt, rson, mid + 1, r);
    tree res = push_up(t[cnt][lson], t[cnt][rson]);
    t[cnt][id].mn = res.mn;
    t[cnt][id].mn2 = res.mn2;
    t[cnt][id].cmn = res.cmn;
    t[cnt][id].mx = res.mx;
    t[cnt][id].mx2 = res.mx2;
    t[cnt][id].cmx = res.cmx;
}
void add(int cnt, int id, int x) {
    t[cnt][id].mn += x;
    t[cnt][id].mx += x;
    t[cnt][id].tag += x;
    if(t[cnt][id].mn2 != inf) t[cnt][id].mn2 += x;
    if(t[cnt][id].mx2 != -inf) t[cnt][id].mx2 += x;
}
void push_down(int cnt, int id) {
    if(t[cnt][id].tag == 0) return;
    add(cnt, lson, t[cnt][id].tag);
    add(cnt, rson, t[cnt][id].tag);
    t[cnt][id].tag = 0;
}
void upd(int cnt, int id, int l, int r, int x) {
    if(t[cnt][id].l >= l && t[cnt][id].r <= r) {
        add(cnt, id, x);
        return;
    }
    push_down(cnt, id);
    int mid = t[cnt][id].l + t[cnt][id].r >> 1;
    if(l <= mid) upd(cnt, lson, l, r, x);
    if(r > mid) upd(cnt, rson, l, r, x);
    tree res = push_up(t[cnt][lson], t[cnt][rson]);
    t[cnt][id].mn = res.mn;
    t[cnt][id].mn2 = res.mn2;
    t[cnt][id].cmn = res.cmn;
    t[cnt][id].mx = res.mx;
    t[cnt][id].mx2 = res.mx2;
    t[cnt][id].cmx = res.cmx;
}
tree ask(int cnt, int id, int l, int r) {
    if(t[cnt][id].l >= l && t[cnt][id].r <= r) return t[cnt][id];
    push_down(cnt, id);
    int mid = t[cnt][id].l + t[cnt][id].r >> 1, ans = 0;
    if(r <= mid) return ask(cnt, lson, l, r);
    if(l > mid) return ask(cnt, rson, l, r);
    tree ll = ask(cnt, lson, l, r), rr = ask(cnt, rson, l, r);
    return push_up(ll, rr);
}
void mul(int l, int r, int x) {
    int temp = x;
    for(int i = 0; i < 10; i++) {
        int cnt = 0;
        while(temp % prime[i] == 0) temp /= prime[i], cnt++;
        if(cnt) upd(i, 1, l, r, cnt);
    }
}
void div(int l, int r, int x) {
    int temp = x;
    for(int i = 0; i < 10; i++) {
        int cnt = 0;
        while(temp % prime[i] == 0) temp /= prime[i], cnt++;
        if(cnt) upd(i, 1, l, r, -cnt);
    }
}
signed main() {
    cin >> n;
    for(int i = 1; i <= n; i++) cin >> a[i];
    int tp[100005][15];
    for(int i = 1; i <= n; i++) {
        int temp = a[i];
        for(int j = 0; j < 10; j++) {
            int cnt = 0;
            while(temp % prime[j] == 0) temp /= prime[j], cnt++;
            tp[i][j] = cnt;
        }
    }
    for(int i = 0; i < 10; i++) {
        for(int j = 1; j <= n; j++) a[j] = tp[j][i];
        build(i, 1, 1, n);
    }
    cin >> m;
    while(m--) {
        int op, l, r, x;
        cin >> op >> l >> r;
        if(op == 1) {
            cin >> x;
            mul(l, r, x);
        } else if(op == 2) {
            cin >> x;
            div(l, r, x);
        } else if(op == 3) {
            int ans = 1;
            for(int i = 0; i < 10; i++) {
                tree res = ask(i, 1, l, r);
                ans = ans * qpow(prime[i], res.mn) % mod;
            }
            cout << ans << '\n';
        } else if(op == 4) {
            int ans = 1;
            for(int i = 0; i < 10; i++) {
                tree res = ask(i, 1, l, r);
                ans = ans * qpow(prime[i], res.mx) % mod;
            }
            cout << ans << '\n';
        } else if(op == 5) {
            int ans = 1;
            for(int i = 0; i < 10; i++) {
                tree res = ask(i, 1, l, r);
                if(res.cmn > 1) ans = ans * qpow(prime[i], res.mn) % mod;
                else ans = ans * qpow(prime[i], res.mn2) % mod;
            }
            cout << ans << '\n';
        } else {
            int ans = 1;
            for(int i = 0; i < 10; i++) {
                tree res = ask(i, 1, l, r);
                if(res.cmx > 1) ans = ans * qpow(prime[i], res.mx) % mod;
                else ans = ans * qpow(prime[i], res.mx2) % mod;
            }
            cout << ans << '\n';
        }
    }
    return 0;
}

:::